]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2s_dump/pd.py
Drop obsolete report() method.
[libsigrokdecode.git] / decoders / i2s_dump / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 import os
23 import sys
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'i2s_dump'
28     name = 'I2S dump'
29     longname = 'I2S dump'
30     desc = 'Output decoded I2S data to a file.'
31     license = 'gplv2+'
32     inputs = ['i2s']
33     outputs = []
34     probes = []
35     optional_probes = []
36     options = {
37         'format': ['File format for the output data', 'wav'],
38         'filename': ['File name for the output data', '-'],
39     }
40     annotations = []
41
42     def __init__(self, **kwargs):
43         self.wrote_header = False
44         self.f = None
45
46     def file_open(self, filename):
47         if filename == 'none':
48             return None
49         elif filename == '-':
50             return sys.stdout
51         else:
52             return open(filename, 'wb')
53
54     def start(self):
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'])
58
59     # TODO: Lots of hard-coded fields in here.
60     def write_wav_header(self):
61         # Chunk descriptor
62         self.f.write(b'RIFF')
63         self.f.write(b'\x24\x80\x00\x00') # Chunk size (2084)
64         self.f.write(b'WAVE')
65
66         # Fmt subchunk
67         self.f.write(b'fmt ')
68         self.f.write(b'\x10\x00\x00\x00') # Subchunk size (16 bytes)
69         self.f.write(b'\x01\x00')         # Audio format (0x0001 == PCM)
70         self.f.write(b'\x02\x00')         # Number of channels (2)
71         self.f.write(b'\x80\x3e\x00\x00') # Samplerate (16000)
72         self.f.write(b'\x00\x7d\x00\x00') # Byterate (32000)
73         self.f.write(b'\x04\x00')         # Blockalign (4)
74         self.f.write(b'\x10\x00')         # Bits per sample (16)
75
76         # Data subchunk
77         self.f.write(b'data')
78         self.f.write(b'\xff\xff\x00\x00') # Subchunk size (65535 bytes) TODO
79
80         self.f.flush()
81
82     def decode(self, ss, es, data):
83         ptype, pdata = data
84
85         if ptype != 'DATA':
86             return
87
88         channel, sample = pdata
89
90         if self.wrote_header == False:
91             self.write_wav_header()
92             self.wrote_header = True
93
94         # Output the next sample to 'filename'.
95         # TODO: Data: first left channel, then right channel.
96         if self.f != None:
97             # TODO: This currently assumes U32 samples, and converts to S16.
98             s = sample >> 16
99             if s >= 0x8000:
100                 s -= 0x10000
101             lo, hi = s & 0xff, (s >> 8) & 0xff
102             self.f.write(bytes([lo, hi]))
103             self.f.flush()
104