/usr/share/libsigrokdecode/decoders/i2s/pd.py is in libsigrokdecode4 0.5.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | ##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## 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, see <http://www.gnu.org/licenses/>.
##
import sigrokdecode as srd
'''
OUTPUT_PYTHON format:
Packet:
[<ptype>, <pdata>]
<ptype>, <pdata>:
- 'DATA', [<channel>, <value>]
<channel>: 'L' or 'R'
<value>: integer
'''
class SamplerateError(Exception):
pass
class Decoder(srd.Decoder):
api_version = 3
id = 'i2s'
name = 'I²S'
longname = 'Integrated Interchip Sound'
desc = 'Serial bus for connecting digital audio devices.'
license = 'gplv2+'
inputs = ['logic']
outputs = ['i2s']
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'},
)
annotations = (
('left', 'Left channel'),
('right', 'Right channel'),
('warnings', 'Warnings'),
)
binary = (
('wav', 'WAV file'),
)
def __init__(self):
self.samplerate = None
self.oldws = 1
self.bitcount = 0
self.data = 0
self.samplesreceived = 0
self.first_sample = None
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 putb(self, data):
self.put(self.ss_block, self.samplenum, self.out_ann, data)
def report(self):
# Calculate the sample rate.
samplerate = '?'
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.ss_block -
self.first_sample))
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):
if not self.samplerate:
raise SamplerateError('Cannot decode without samplerate.')
while True:
# Wait for a rising edge on the SCK pin.
sck, ws, sd = self.wait({0: 'r'})
self.data = (self.data << 1) | sd
self.bitcount += 1
# This was not the LSB unless WS has flipped.
if ws == self.oldws:
continue
# Only submit the sample, if we received the beginning of it.
if self.ss_block is not None:
if not self.wrote_wav_header:
self.put(0, 0, self.out_binary, [0, self.wav_header()])
self.wrote_wav_header = True
self.samplesreceived += 1
idx = 0 if self.oldws else 1
c1 = 'Left channel' if self.oldws else 'Right channel'
c2 = 'Left' if self.oldws else 'Right'
c3 = 'L' if self.oldws else 'R'
v = '%08x' % self.data
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:
self.putb([2, ['Received %d-bit word, expected %d-bit '
'word' % (self.bitcount, self.wordlength)]])
self.wordlength = self.bitcount
# Reset decoder state.
self.data = 0
self.bitcount = 0
self.ss_block = self.samplenum
# Save the first sample position.
if self.first_sample is None:
self.first_sample = self.samplenum
self.oldws = ws
|