biemster / pico-serialmic

Raspberry Pico INMP411 MEMS mic with serial output for C-SDK
4 stars 3 forks source link

Support for SPH0645LM4H #3

Open dodgekaze opened 2 months ago

dodgekaze commented 2 months ago

I'm trying to read data from the SPH0645LM4H.

The output received is :

f8d98000
f8d74000
f8d54000
f8e84000
f8e60000
f8e50000
f8e24000
f8e2c000
f8e48000
f8dfc000
f8e0c000
f91ac000
f91b8000
f9224000
f9288000
f92fc000
f9384000
f93c8000
f9414000
f8d24000
f8cf8000
f8d18000
f8d54000
f8d30000
f8d78000
f8d9c000
f8da0000
f8d00000

However when I transfer this into a bin file:

import binascii

def hex_to_bin(hex_file, bin_file):
    with open(hex_file, 'r') as f:
        hex_data = f.read().replace('\n', '').replace(' ', '')

    # Convert hex to bytes
    bin_data = binascii.unhexlify(hex_data)

    with open(bin_file, 'wb') as f:
        f.write(bin_data)

hex_to_bin('data.txt', 'data.bin')

and then use the following to convert it to .wav I only hear static / no sound... Does my initial data in hexadecimal look fine?

ffmpeg -f s32be -ar 16000 -ac 1 -i data.bin output.wav

Microphone used: https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/overview

Code:

#include "pico/stdlib.h"
#include "machine_i2s.c"

#define SCK 4
#define WS 5 // needs to be SCK +1
#define SD 3
#define BPS 32 
#define RATE 16000

int main() {
    stdio_init_all();
    machine_i2s_obj_t* i2s0 = machine_i2s_make_new(0, SCK, WS, SD, RX, BPS, MONO, /*ringbuf_len*/SIZEOF_DMA_BUFFER_IN_BYTES, RATE);
    int32_t buffer[I2S_RX_FRAME_SIZE_IN_BYTES / 4];

    while (true) {
        machine_i2s_stream_read(i2s0, (void*)&buffer[0], I2S_RX_FRAME_SIZE_IN_BYTES);

        printf("%.8x\n", buffer[0]);
    }
}
biemster commented 2 months ago

Thank you for keeping track of your progress here, seems like you are on the right track. It'll probably just require a couple more rounds of experimentation and going over the datasheet, pay special attention to the data format. Big endian or little endian is a pain, and 24 bit samples come in all kinds of varieties so they are even worse. Also i2s transport mono/stereo and things like that have to be taken into account. I can definitely accept a PR when you get this working!

dodgekaze commented 1 month ago

@biemster

The datasheet contains the following info:

The SPH0645LM4H microphone operates as an I2S slave. The master must provide the BCLK and WS signals. The Over Sampling Rate is fixed at 64 therefore the WS signal must be BCLK/64 and synchronized to the BCLK. Clock frequencies from 2.048Mhz to 4.096MHz are supported so sampling rates from 32KHz to 64KHz can be had by changing the clock frequency. The Data Format is I2S, 24 bit, 2’s compliment, MSB first. The Data Precision is 18 bits, unused bits are zeros.

I have tried changing the rate to 32000 and using the following command, but still no luck

ffmpeg -f s24be -ar 32000 -ac 1 -i data.bin output.wav

Would appreciate if you can help me in the right direction!

biemster commented 1 month ago

It's very difficult for me to give some pointers without having the device on hand. But seeing it's from adafruit it must have some support community around it, I'd suggest to take your question there?

dodgekaze commented 1 month ago

@biemster I appreciate your response! I will have a look there. Just to verify you used this one if I'm correct? https://www.tinytronics.nl/en/sensors/sound/inmp441-mems-microphone-i2s

I might order this one to see if it works.

biemster commented 1 month ago

@biemster I appreciate your response! I will have a look there. Just to verify you used this one if I'm correct? https://www.tinytronics.nl/en/sensors/sound/inmp441-mems-microphone-i2s

I might order this one to see if it works.

Die lijken heel veel op wat ik had besteld, ik heb ze hier vandaan: https://www.aliexpress.com/item/32960945048.html Je kan de middle man er tussen uit halen als je een maandje kan wachten, maar de tinytronics zijn hoogstwaarschijnlijk dezelfde.

dodgekaze commented 1 month ago

@biemster I appreciate your response! I will have a look there. Just to verify you used this one if I'm correct? https://www.tinytronics.nl/en/sensors/sound/inmp441-mems-microphone-i2s I might order this one to see if it works.

Die lijken heel veel op wat ik had besteld, ik heb ze hier vandaan: https://www.aliexpress.com/item/32960945048.html Je kan de middle man er tussen uit halen als je een maandje kan wachten, maar de tinytronics zijn hoogstwaarschijnlijk dezelfde.

Bedankt! Ik heb die van Tinytronics besteld. Hopen dat daar direct wat bruikbare data uitkomt zodat ik verder kan gaan met PCM data gebruiken voor tone recognition (matchen van een toon / patroon met het ander) vervolgens zal ik weer terug gaan naar de SPH0645LM4H. Ik vermoed zoals je aangaf dat het o.a om de verschillende sampling rate / data format gaat van de SPH0645LM4H.