Berry example: wave file recorder (#21315)

This commit is contained in:
Christian Baars 2024-05-02 13:36:28 +02:00 committed by GitHub
parent fb78c3a796
commit 08c3416255
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,73 @@
#-
- WAV recorder driver Berry - mono only
- used from command `wavrec<duration in seconds> <file_name_without_wav_suffix>`
-#
class WAVREC
var in, f
var fast
var targetSize
def init(seconds, name)
self.in=AudioInputI2S()
self.in.begin()
self.in.set_gain(60)
var r = self.in.get_rate()
var bps = self.in.get_bits_per_sample()
log(f"WAV: sample rate {r} with {bps} bits")
self.f = open(f"{name}.wav", "w")
self.targetSize = r * seconds * (bps/8)
self.writeHeader(self.targetSize,r, bps)
log(f"WAV: will record {seconds} seconds to {name}.wav")
self.fast = /->self.record()
tasmota.add_fast_loop(self.fast)
tasmota.add_driver(self)
end
def record()
var b = self.in.read_bytes()
if b == nil
return
end
self.targetSize -= size(b)
if self.targetSize > 0
self.f.write(b)
else
tasmota.remove_fast_loop(self.fast)
self.f.close()
log("WAV: closing file")
tasmota.remove_driver(self)
end
end
def writeHeader(fsize, rate, bps)
var header = bytes(44)
header.."RIFF"
header.add(fsize + 44,4)
header.."WAVE"
header.."fmt "
header.add(bps,4) #sample_bits
header..1 # PCM
header..0
header..1 # mono
header..0
header.add(rate,4) #sample_bits
header.add(rate * 2,4) #(Sample Rate * BitsPerSample * Channels) / 8
header..2 # BlockAlign
header..0
header..bps # sample bits
header..0
header.."data"
header.add(fsize,4) #sample_bits
self.f.write(header)
end
end
def wav_recorder(cmd, idx, payload, payload_json)
tasmota.resp_cmnd_done()
return WAVREC(idx, payload)
end
tasmota.add_cmd('wavrec', wav_recorder)
# use like: waverec5 /test - > record 5 seconds to /test.wav