Open
Description
CircuitPython version
Adafruit CircuitPython 9.0.5 on 2024-05-22; Adafruit Feather M4 Express with samd51j19
Board ID:feather_m4_express
Code/REPL
import time
import array
import math
import board
import audiobusio
import usb_cdc
import struct
# Main program
mic = audiobusio.PDMIn(board.TX, board.D12, sample_rate=24000, bit_depth=16)
samples = array.array('H', [0] * 160)
print("Program started. Collecting and transmitting data...")
while True:
mic.record(samples, len(samples))
data = struct.pack('<' + 'H' * len(samples), *samples)
usb_cdc.data.write(data)
Behavior
I am trying to collect PDM microphone data and send to my PC. I am using Feather M4 express. However, I am unable to send data through the code above. By adding a timesleep, I am able to send but having a different sample rate.
Description
No response
Additional information
def write_wav_data(raw_sound, filename, sample_rate):
logging.info("Writing data to WAV file.")
with wave.open(filename, 'wb') as wav_file:
wav_file.setnchannels(1) # mono
wav_file.setsampwidth(2) # 16 bits = 2 bytes
wav_file.setframerate(sample_rate)
wav_file.writeframes(raw_sound)
def main(args):
ser = serial.Serial(args.port, args.baud_rate, timeout=10)
logging.info(f'Connected to device on {args.port}. Ready to record.')
while True:
input("Press Enter to start recording...")
logging.info('Recording started...')
raw_sound = bytearray()
bytes_per_sample = 2
samples_per_chunk = 160
bytes_per_chunk = samples_per_chunk * bytes_per_sample
total_bytes_to_read = args.sample_rate * args.duration * bytes_per_sample
total_chunks = total_bytes_to_read // bytes_per_chunk
for _ in range(total_chunks):
data = ser.read(bytes_per_chunk)
if len(data) == bytes_per_chunk:
# Unpack the data as it was packed on the microcontroller side
samples = struct.unpack('<' + 'H' * samples_per_chunk, data)
# Repack the data for WAV format (16-bit signed integers)
raw_sound.extend(struct.pack('<' + 'h' * samples_per_chunk, *samples))
else:
logging.warning(f"Expected {bytes_per_chunk} bytes, received {len(data)} bytes.")
filename = f"{args.filename}.wav"
write_wav_data(raw_sound, filename, args.sample_rate)
logging.info(f'Recording saved as {filename}.')
break
Having this code to receive data from my PC side