2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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
30 - 'DATA', [<channel>, <value>]
36 class Decoder(srd.Decoder):
40 longname = 'Integrated Interchip Sound'
41 desc = 'Serial bus for connecting digital audio devices.'
46 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
47 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
48 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
53 ['left', 'Left channel'],
54 ['right', 'Right channel'],
55 ['warnings', 'Warnings'],
61 def __init__(self, **kwargs):
62 self.samplerate = None
67 self.samplesreceived = 0
68 self.first_sample = None
69 self.start_sample = None
71 self.wrote_wav_header = False
74 self.out_python = self.register(srd.OUTPUT_PYTHON)
75 self.out_bin = self.register(srd.OUTPUT_BINARY)
76 self.out_ann = self.register(srd.OUTPUT_ANN)
78 def metadata(self, key, value):
79 if key == srd.SRD_CONF_SAMPLERATE:
80 self.samplerate = value
82 def putpb(self, data):
83 self.put(self.start_sample, self.samplenum, self.out_python, data)
85 def putbin(self, data):
86 self.put(self.start_sample, self.samplenum, self.out_bin, data)
89 self.put(self.start_sample, self.samplenum, self.out_ann, data)
93 # Calculate the sample rate.
95 if self.start_sample != None and \
96 self.first_sample != None and \
97 self.start_sample > self.first_sample:
98 samplerate = '%d' % (self.samplesreceived *
99 self.samplerate / (self.start_sample -
102 return 'I²S: %d %d-bit samples received at %sHz' % \
103 (self.samplesreceived, self.wordlength, samplerate)
105 def wav_header(self):
108 h += b'\x24\x80\x00\x00' # Chunk size (2084)
112 h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes)
113 h += b'\x01\x00' # Audio format (0x0001 == PCM)
114 h += b'\x02\x00' # Number of channels (2)
115 h += b'\x80\x3e\x00\x00' # Samplerate (16000)
116 h += b'\x00\x7d\x00\x00' # Byterate (32000)
117 h += b'\x04\x00' # Blockalign (4)
118 h += b'\x10\x00' # Bits per sample (16)
121 h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO
124 def wav_sample(self, sample):
125 # TODO: This currently assumes U32 samples, and converts to S16.
129 lo, hi = s & 0xff, (s >> 8) & 0xff
130 return bytes([lo, hi])
132 def decode(self, ss, es, data):
133 if self.samplerate is None:
134 raise Exception("Cannot decode without samplerate.")
135 for self.samplenum, (sck, ws, sd) in data:
137 # Ignore sample if the bit clock hasn't changed.
138 if sck == self.oldsck:
142 if sck == 0: # Ignore the falling clock edge.
145 self.data = (self.data << 1) | sd
148 # This was not the LSB unless WS has flipped.
152 # Only submit the sample, if we received the beginning of it.
153 if self.start_sample != None:
155 if not self.wrote_wav_header:
156 self.put(0, 0, self.out_bin, (0, self.wav_header()))
157 self.wrote_wav_header = True
159 self.samplesreceived += 1
161 idx = 0 if self.oldws else 1
162 c1 = 'Left channel' if self.oldws else 'Right channel'
163 c2 = 'Left' if self.oldws else 'Right'
164 c3 = 'L' if self.oldws else 'R'
165 v = '%08x' % self.data
166 self.putpb(['DATA', [c3, self.data]])
167 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
168 '%s: %s' % (c3, v), c3]])
169 self.putbin((0, self.wav_sample(self.data)))
171 # Check that the data word was the correct length.
172 if self.wordlength != -1 and self.wordlength != self.bitcount:
173 self.putb([2, ['Received %d-bit word, expected %d-bit '
174 'word' % (self.bitcount, self.wordlength)]])
176 self.wordlength = self.bitcount
178 # Reset decoder state.
181 self.start_sample = self.samplenum
183 # Save the first sample position.
184 if self.first_sample == None:
185 self.first_sample = self.samplenum