X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fi2s%2Fpd.py;h=1d1a7a43e0008830e9a014ab45cc17092ed80f9e;hp=ea147dec0369f11446468dc929a00e5bc4b20ee9;hb=780770f1295b7fdeb4481eb42623bad5da1e19a7;hpb=be465111b552c7c2a2262ac49758a30a8bf1b1d5 diff --git a/decoders/i2s/pd.py b/decoders/i2s/pd.py index ea147de..1d1a7a4 100644 --- a/decoders/i2s/pd.py +++ b/decoders/i2s/pd.py @@ -18,12 +18,10 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -# I2S protocol decoder - import sigrokdecode as srd ''' -Protocol output format: +OUTPUT_PYTHON format: Packet: [, ] @@ -38,7 +36,7 @@ Packet: class Decoder(srd.Decoder): api_version = 1 id = 'i2s' - name = 'I2S' + name = 'I²S' longname = 'Integrated Interchip Sound' desc = 'Serial bus for connecting digital audio devices.' license = 'gplv2+' @@ -49,13 +47,14 @@ class Decoder(srd.Decoder): {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, ] - optional_probes = [] - options = {} annotations = [ ['left', 'Left channel'], ['right', 'Right channel'], ['warnings', 'Warnings'], ] + binary = ( + ('wav', 'WAV file'), + ) def __init__(self, **kwargs): self.samplerate = None @@ -67,9 +66,11 @@ class Decoder(srd.Decoder): self.first_sample = None self.start_sample = None self.wordlength = -1 + self.wrote_wav_header = False def start(self): - self.out_proto = self.register(srd.OUTPUT_PYTHON) + self.out_python = self.register(srd.OUTPUT_PYTHON) + self.out_bin = self.register(srd.OUTPUT_BINARY) self.out_ann = self.register(srd.OUTPUT_ANN) def metadata(self, key, value): @@ -77,7 +78,10 @@ class Decoder(srd.Decoder): self.samplerate = value def putpb(self, data): - self.put(self.start_sample, self.samplenum, self.out_proto, data) + self.put(self.start_sample, self.samplenum, self.out_python, data) + + def putbin(self, data): + self.put(self.start_sample, self.samplenum, self.out_bin, data) def putb(self, data): self.put(self.start_sample, self.samplenum, self.out_ann, data) @@ -93,9 +97,36 @@ class Decoder(srd.Decoder): self.samplerate / (self.start_sample - self.first_sample)) - return 'I2S: %d %d-bit samples received at %sHz' % \ + return 'I²S: %d %d-bit samples received at %sHz' % \ (self.samplesreceived, self.wordlength, samplerate) + def wav_header(self): + # Chunk descriptor + h = b'RIFF' + h += b'\x24\x80\x00\x00' # Chunk size (2084) + h += b'WAVE' + # Fmt subchunk + h += b'fmt ' + h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes) + h += b'\x01\x00' # Audio format (0x0001 == PCM) + h += b'\x02\x00' # Number of channels (2) + h += b'\x80\x3e\x00\x00' # Samplerate (16000) + h += b'\x00\x7d\x00\x00' # Byterate (32000) + h += b'\x04\x00' # Blockalign (4) + h += b'\x10\x00' # Bits per sample (16) + # Data subchunk + h += b'data' + h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO + return h + + def wav_sample(self, sample): + # TODO: This currently assumes U32 samples, and converts to S16. + s = sample >> 16 + if s >= 0x8000: + s -= 0x10000 + lo, hi = s & 0xff, (s >> 8) & 0xff + return bytes([lo, hi]) + def decode(self, ss, es, data): if self.samplerate is None: raise Exception("Cannot decode without samplerate.") @@ -118,6 +149,11 @@ class Decoder(srd.Decoder): # Only submit the sample, if we received the beginning of it. if self.start_sample != None: + + if not self.wrote_wav_header: + self.put(0, 0, self.out_bin, (0, self.wav_header())) + self.wrote_wav_header = True + self.samplesreceived += 1 idx = 0 if self.oldws else 1 @@ -128,6 +164,7 @@ class Decoder(srd.Decoder): self.putpb(['DATA', [c3, self.data]]) self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v), '%s: %s' % (c3, v), c3]]) + self.putbin((0, self.wav_sample(self.data))) # Check that the data word was the correct length. if self.wordlength != -1 and self.wordlength != self.bitcount: