]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2s/pd.py
i2s: Add WAV dump support, drop obsolete 'i2s_dump' PD.
[libsigrokdecode.git] / decoders / i2s / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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
21# I²S protocol decoder
22
23import sigrokdecode as srd
24
25'''
26Protocol output format:
27
28Packet:
29[<ptype>, <pdata>]
30
31<ptype>, <pdata>:
32 - 'DATA', [<channel>, <value>]
33
34<channel>: 'L' or 'R'
35<value>: integer
36'''
37
38class Decoder(srd.Decoder):
39 api_version = 1
40 id = 'i2s'
41 name = 'I²S'
42 longname = 'Integrated Interchip Sound'
43 desc = 'Serial bus for connecting digital audio devices.'
44 license = 'gplv2+'
45 inputs = ['logic']
46 outputs = ['i2s']
47 probes = [
48 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
49 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
50 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
51 ]
52 optional_probes = []
53 options = {}
54 annotations = [
55 ['left', 'Left channel'],
56 ['right', 'Right channel'],
57 ['warnings', 'Warnings'],
58 ]
59 binary = (
60 ('wav', 'WAV file'),
61 )
62
63 def __init__(self, **kwargs):
64 self.samplerate = None
65 self.oldsck = 1
66 self.oldws = 1
67 self.bitcount = 0
68 self.data = 0
69 self.samplesreceived = 0
70 self.first_sample = None
71 self.start_sample = None
72 self.wordlength = -1
73 self.wrote_wav_header = False
74
75 def start(self):
76 self.out_proto = self.register(srd.OUTPUT_PYTHON)
77 self.out_bin = self.register(srd.OUTPUT_BINARY)
78 self.out_ann = self.register(srd.OUTPUT_ANN)
79
80 def metadata(self, key, value):
81 if key == srd.SRD_CONF_SAMPLERATE:
82 self.samplerate = value
83
84 def putpb(self, data):
85 self.put(self.start_sample, self.samplenum, self.out_proto, data)
86
87 def putbin(self, data):
88 self.put(self.start_sample, self.samplenum, self.out_bin, data)
89
90 def putb(self, data):
91 self.put(self.start_sample, self.samplenum, self.out_ann, data)
92
93 def report(self):
94
95 # Calculate the sample rate.
96 samplerate = '?'
97 if self.start_sample != None and \
98 self.first_sample != None and \
99 self.start_sample > self.first_sample:
100 samplerate = '%d' % (self.samplesreceived *
101 self.samplerate / (self.start_sample -
102 self.first_sample))
103
104 return 'I²S: %d %d-bit samples received at %sHz' % \
105 (self.samplesreceived, self.wordlength, samplerate)
106
107 def wav_header(self):
108 # Chunk descriptor
109 h = b'RIFF'
110 h += b'\x24\x80\x00\x00' # Chunk size (2084)
111 h += b'WAVE'
112 # Fmt subchunk
113 h += b'fmt '
114 h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes)
115 h += b'\x01\x00' # Audio format (0x0001 == PCM)
116 h += b'\x02\x00' # Number of channels (2)
117 h += b'\x80\x3e\x00\x00' # Samplerate (16000)
118 h += b'\x00\x7d\x00\x00' # Byterate (32000)
119 h += b'\x04\x00' # Blockalign (4)
120 h += b'\x10\x00' # Bits per sample (16)
121 # Data subchunk
122 h += b'data'
123 h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO
124 return h
125
126 def wav_sample(self, sample):
127 # TODO: This currently assumes U32 samples, and converts to S16.
128 s = sample >> 16
129 if s >= 0x8000:
130 s -= 0x10000
131 lo, hi = s & 0xff, (s >> 8) & 0xff
132 return bytes([lo, hi])
133
134 def decode(self, ss, es, data):
135 if self.samplerate is None:
136 raise Exception("Cannot decode without samplerate.")
137 for self.samplenum, (sck, ws, sd) in data:
138
139 # Ignore sample if the bit clock hasn't changed.
140 if sck == self.oldsck:
141 continue
142
143 self.oldsck = sck
144 if sck == 0: # Ignore the falling clock edge.
145 continue
146
147 self.data = (self.data << 1) | sd
148 self.bitcount += 1
149
150 # This was not the LSB unless WS has flipped.
151 if ws == self.oldws:
152 continue
153
154 # Only submit the sample, if we received the beginning of it.
155 if self.start_sample != None:
156
157 if not self.wrote_wav_header:
158 self.put(0, 0, self.out_bin, (0, self.wav_header()))
159 self.wrote_wav_header = True
160
161 self.samplesreceived += 1
162
163 idx = 0 if self.oldws else 1
164 c1 = 'Left channel' if self.oldws else 'Right channel'
165 c2 = 'Left' if self.oldws else 'Right'
166 c3 = 'L' if self.oldws else 'R'
167 v = '%08x' % self.data
168 self.putpb(['DATA', [c3, self.data]])
169 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
170 '%s: %s' % (c3, v), c3]])
171 self.putbin((0, self.wav_sample(self.data)))
172
173 # Check that the data word was the correct length.
174 if self.wordlength != -1 and self.wordlength != self.bitcount:
175 self.putb([2, ['Received %d-bit word, expected %d-bit '
176 'word' % (self.bitcount, self.wordlength)]])
177
178 self.wordlength = self.bitcount
179
180 # Reset decoder state.
181 self.data = 0
182 self.bitcount = 0
183 self.start_sample = self.samplenum
184
185 # Save the first sample position.
186 if self.first_sample == None:
187 self.first_sample = self.samplenum
188
189 self.oldws = ws
190