X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fi2s%2Fpd.py;h=91801b4e98f9c0c6a62dcb8b92852907513f6092;hp=b921011589bd3592824efb6d68ff5ad9b3b76561;hb=10aeb8ea8b183394cebc0033f048f49f4262b57d;hpb=24c74fd30fb161837c5f8b01baf3c0fe2dfa4ed5 diff --git a/decoders/i2s/pd.py b/decoders/i2s/pd.py index b921011..91801b4 100644 --- a/decoders/i2s/pd.py +++ b/decoders/i2s/pd.py @@ -1,5 +1,5 @@ ## -## This file is part of the sigrok project. +## This file is part of the libsigrokdecode project. ## ## Copyright (C) 2012 Joel Holdsworth ## @@ -14,77 +14,125 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## -# I2S protocol decoder - import sigrokdecode as srd +import struct + +''' +OUTPUT_PYTHON format: + +Packet: +[, ] + +, : + - 'DATA', [, ] + +: 'L' or 'R' +: integer +''' -# Annotation formats -ANN_HEX = 0 +class SamplerateError(Exception): + pass class Decoder(srd.Decoder): - api_version = 1 + api_version = 3 id = 'i2s' - name = 'I2S' + name = 'I²S' longname = 'Integrated Interchip Sound' desc = 'Serial bus for connecting digital audio devices.' license = 'gplv2+' inputs = ['logic'] outputs = ['i2s'] - probes = [ + channels = ( {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, - ] - optional_probes = [] - options = {} - annotations = [ - ['Hex', 'Annotations in hex format'], - ] - - def __init__(self, **kwargs): - self.oldsck = 1 + ) + annotations = ( + ('left', 'Left channel'), + ('right', 'Right channel'), + ('warnings', 'Warnings'), + ) + binary = ( + ('wav', 'WAV file'), + ) + + def __init__(self): + self.reset() + + def reset(self): + self.samplerate = None self.oldws = 1 self.bitcount = 0 self.data = 0 self.samplesreceived = 0 self.first_sample = None - self.start_sample = None - self.samplenum = -1 + self.ss_block = None self.wordlength = -1 + self.wrote_wav_header = False + + def start(self): + self.out_python = self.register(srd.OUTPUT_PYTHON) + self.out_binary = self.register(srd.OUTPUT_BINARY) + self.out_ann = self.register(srd.OUTPUT_ANN) + + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value + + def putpb(self, data): + self.put(self.ss_block, self.samplenum, self.out_python, data) + + def putbin(self, data): + self.put(self.ss_block, self.samplenum, self.out_binary, data) - def start(self, metadata): - self.samplerate = metadata['samplerate'] - self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s') - self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s') + def putb(self, data): + self.put(self.ss_block, self.samplenum, self.out_ann, data) def report(self): # Calculate the sample rate. samplerate = '?' - if self.start_sample != None and \ - self.first_sample != None and \ - self.start_sample > self.first_sample: + if self.ss_block is not None and \ + self.first_sample is not None and \ + self.ss_block > self.first_sample: samplerate = '%d' % (self.samplesreceived * - self.samplerate / (self.start_sample - + self.samplerate / (self.ss_block - 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 decode(self, ss, es, data): - for samplenum, (sck, ws, sd) in data: - - # Ignore sample if the bit clock hasn't changed. - if sck == self.oldsck: - continue - - self.oldsck = sck - if sck == 0: # Ignore the falling clock edge. - continue + 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\xfa\x00\x00' # Byterate (64000) + h += b'\x04\x00' # Blockalign (4) + h += b'\x20\x00' # Bits per sample (32) + # Data subchunk + h += b'data' + h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO + return h + + def wav_sample(self, sample): + return struct.pack('