]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2s_dump/pd.py
i2s_dump: Fix bug in the WAV output format generation.
[libsigrokdecode.git] / decoders / i2s_dump / pd.py
CommitLineData
f54ffe55
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21import sigrokdecode as srd
22import os
23import sys
24
25class Decoder(srd.Decoder):
26 api_version = 1
27 id = 'i2s_dump'
28 name = 'I2S dump'
29 longname = 'I2S dump'
30 desc = 'Output decoded I2S data to a file.'
31 license = 'gplv2+'
32 inputs = ['i2s']
33 outputs = []
34 probes = []
35 optional_probes = []
36 options = {
37 'format': ['File format for the output data', 'wav'],
38 'filename': ['File name for the output data', '-'],
39 }
40 annotations = []
41
42 def __init__(self, **kwargs):
43 self.wrote_header = False
44 self.f = None
45
46 def file_open(self, filename):
47 if filename == 'none':
48 return None
49 elif filename == '-':
50 return sys.stdout
51 else:
52 return open(filename, 'wb')
53
54 def start(self, metadata):
55 # A filename of 'none' is not allowed (has special meaning). A filename
56 # of '-' means 'stdout'.
57 self.f = self.file_open(self.options['filename'])
58
59 def report(self):
60 pass
61
62 # TODO: Lots of hard-coded fields in here.
63 def write_wav_header(self):
64 # Chunk descriptor
65 self.f.write(b'RIFF')
66 self.f.write(b'\x24\x80\x00\x00') # Chunk size (2084)
67 self.f.write(b'WAVE')
68
69 # Fmt subchunk
70 self.f.write(b'fmt ')
71 self.f.write(b'\x10\x00\x00\x00') # Subchunk size (16 bytes)
72 self.f.write(b'\x01\x00') # Audio format (0x0001 == PCM)
73 self.f.write(b'\x02\x00') # Number of channels (2)
6fd278ba
UH
74 self.f.write(b'\x80\x3e\x00\x00') # Samplerate (16000)
75 self.f.write(b'\x00\x7d\x00\x00') # Byterate (32000)
f54ffe55
UH
76 self.f.write(b'\x04\x00') # Blockalign (4)
77 self.f.write(b'\x10\x00') # Bits per sample (16)
78
79 # Data subchunk
80 self.f.write(b'data')
81 self.f.write(b'\xff\xff\x00\x00') # Subchunk size (65535 bytes) TODO
82
83 self.f.flush()
84
85 def decode(self, ss, es, data):
86 ptype, pdata = data
87
88 if ptype != 'DATA':
89 return
90
91 channel, sample = pdata
92
93 if self.wrote_header == False:
94 self.write_wav_header()
95 self.wrote_header = True
96
97 # Output the next sample to 'filename'.
98 # TODO: Data: first left channel, then right channel.
99 if self.f != None:
100 # TODO: This currently assumes U32 samples, and converts to S16.
101 s = sample >> 16
102 if s >= 0x8000:
103 s -= 0x10000
104 lo, hi = s & 0xff, (s >> 8) & 0xff
6fd278ba 105 self.f.write(bytes([lo, hi]))
f54ffe55
UH
106 self.f.flush()
107