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