Skip to content

Instantly share code, notes, and snippets.

@anoken
Created February 11, 2020 22:56
Show Gist options
  • Save anoken/35270cb97637444ade9e78308dc1cd20 to your computer and use it in GitHub Desktop.
Save anoken/35270cb97637444ade9e78308dc1cd20 to your computer and use it in GitHub Desktop.
wav file write
from Maix import GPIO, I2S, FFT
import image, lcd, math,array
from board import board_info
from fpioa_manager import fm
import audio
sample_rate = 44100
SAMPLES_PER_SECOND = 1024
BITS_PER_SAMPLE=16
RECORD_TIME_IN_SECONDS = 10
#MaixDuino
fm.register(20,fm.fpioa.I2S0_IN_D0)
fm.register(19,fm.fpioa.I2S0_WS)
fm.register(18,fm.fpioa.I2S0_SCLK)
#MaixOneDock
#fm.register(20,fm.fpioa.I2S0_IN_D0)
#fm.register(30,fm.fpioa.I2S0_WS)
#fm.register(32,fm.fpioa.I2S0_SCLK)
rx = I2S(I2S.DEVICE_0)
rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode = I2S.STANDARD_MODE)
rx.set_sample_rate(sample_rate)
def gen_wav_header(sampleRate, bitsPerSample, num_channels, num_samples):
datasize = num_samples * num_channels * bitsPerSample // 8
o = bytes("RIFF",'ascii') # (4byte) Marks file as RIFF
o += (datasize -8).to_bytes(4,'little') # (4byte) File size in bytes excluding this and RIFF marker
o += bytes("WAVE",'ascii') # (4byte) File type
o += bytes("fmt ",'ascii') # (4byte) Format Chunk Marker
o += (2).to_bytes(4,'little') # (4byte) Length of above format data
o += (1).to_bytes(2,'little') # (2byte) Format type (1 - PCM)
o += (num_channels).to_bytes(2,'little') # (2byte)
o += (sampleRate).to_bytes(4,'little') # (4byte)
o += (sampleRate * num_channels * bitsPerSample // 8).to_bytes(4,'little') # (4byte)
o += (num_channels * bitsPerSample // 8).to_bytes(2,'little') # (2byte)
o += (bitsPerSample).to_bytes(2,'little') # (2byte)
o += bytes("data",'ascii') # (4byte) Data Chunk Marker
o += (datasize).to_bytes(4,'little') # (4byte) Data size in bytes
return o
wav_header = gen_wav_header(sample_rate, BITS_PER_SAMPLE, 1, SAMPLES_PER_SECOND * RECORD_TIME_IN_SECONDS)
print(wav_header)
s = open('/sd/test.wav','wb')
s.write(wav_header)
TOTAL_BYTES = RECORD_TIME_IN_SECONDS * SAMPLES_PER_SECOND * BITS_PER_SAMPLE//8
bytes_written = 0
while bytes_written < TOTAL_BYTES:
numwrite = 0
audio = rx.record(SAMPLES_PER_SECOND )
print(audio)
numwrite = s.write(audio.to_bytes())
bytes_written += numwrite
print(numwrite,bytes_written)
s.close()
print('Done %d bytes written to SD Card' % bytes_written)
from fpioa_manager import *
from Maix import I2S, GPIO
import audio
##スピーカの初期化
AUDIO_PA_EN_PIN = 2 # Maixduino
fm.register(AUDIO_PA_EN_PIN, fm.fpioa.GPIO1, force=True)
spk_sd=GPIO(GPIO.GPIO1, GPIO.OUT)
spk_sd.value(1)
# register i2s(i2s0) pin
fm.register(34,fm.fpioa.I2S0_OUT_D1, force=True)
fm.register(35,fm.fpioa.I2S0_SCLK, force=True)
fm.register(33,fm.fpioa.I2S0_WS, force=True)
wav_dev = I2S(I2S.DEVICE_0)
##wav ファイルの再生
def play_wav(fname):
player = audio.Audio(path = fname)
player.volume(1)
wav_info = player.play_process(wav_dev)
wav_dev.channel_config(wav_dev.CHANNEL_1,
I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT,
align_mode = I2S.STANDARD_MODE)
wav_dev.set_sample_rate(wav_info[1])
while True:
ret = player.play()
if ret == None:
break
elif ret==0:
break
player.finish()
#play_wav("bird.wav")
play_wav("test.wav")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment