2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 import sigrokdecode as srd
25 class Decoder(srd.Decoder):
30 desc = 'Output decoded I2S data to a file.'
37 'format': ['File format for the output data', 'wav'],
38 'filename': ['File name for the output data', '-'],
42 def __init__(self, **kwargs):
43 self.wrote_header = False
46 def file_open(self, filename):
47 if filename == 'none':
52 return open(filename, 'wb')
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'])
62 # TODO: Lots of hard-coded fields in here.
63 def write_wav_header(self):
66 self.f.write(b'\x24\x80\x00\x00') # Chunk size (2084)
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)
74 self.f.write(b'\x80\x3e\x00\x00') # Samplerate (16000)
75 self.f.write(b'\x00\x7d\x00\x00') # Byterate (32000)
76 self.f.write(b'\x04\x00') # Blockalign (4)
77 self.f.write(b'\x10\x00') # Bits per sample (16)
81 self.f.write(b'\xff\xff\x00\x00') # Subchunk size (65535 bytes) TODO
85 def decode(self, ss, es, data):
91 channel, sample = pdata
93 if self.wrote_header == False:
94 self.write_wav_header()
95 self.wrote_header = True
97 # Output the next sample to 'filename'.
98 # TODO: Data: first left channel, then right channel.
100 # TODO: This currently assumes U32 samples, and converts to S16.
104 lo, hi = s & 0xff, (s >> 8) & 0xff
105 self.f.write(bytes([lo, hi]))