Getting raw audio Samples

audioguy
Posts: 8
Joined: Fri Dec 07, 2018 10:22 am

Getting raw audio Samples

Postby audioguy » Fri Dec 07, 2018 10:39 am

What is the best way to obtain raw audiosamples from a pipeline or a streamreader ?
I want to do Spektrum analysis on the Samples with some fft library (se here http://www.robinscheibler.org/2017/12/12/esp32-fft.html )
My hopes are that i can make a audiopipline element that can do this .
i need to get the raw Wav samples form an

Code: Select all

i2s_stream_reader
or a

Code: Select all

 bt_stream_reader
created with

Code: Select all

bluetooth_service_create_stream();
Thanks in advance

water117
Posts: 3
Joined: Fri Dec 14, 2018 9:28 am
Location: BeiJing China

Re: Getting raw audio Samples

Postby water117 » Fri Dec 14, 2018 10:14 am

Use i2s_stream_reader as example.
i2s_stream_reader = i2s_stream_init(&i2s_cfg_read);
audio_element_set_write_cb(i2s_stream_reader, i2s_input_write_cb, NULL);
audio_pipeline_link(pipeline, (const char *[]) {"i2s_read"}, 1);

short pcm_fft[128];
int pcm_wptr=0;

// You can get pcm data in write callback.
int i2s_input_write_cb(audio_element_handle_t el, char *buf, int len, TickType_t wait_time, void *ctx)
{
// use 48KHz, 2ch, 16bits as example

// the buf is pcm raw data
short *pcm2 = (short*)buf;

// mix stereo as mono and do fft
len = (len>>2)<<2; // 2*2 align
int wlen = len;
while (wlen > 0) {
pcm_fft[pcm_wptr] = (pcm2[0]>>1) + (pcm2[1]>>1); // Mix L & R
pcm2 += 2;
wlen -= 4;
pcm_wptr++;
if (pcm_wptr >= 128) {
fft_128_s16(pcm_fft, image, real)
pcm_wptr = 0;
}
}

return len;
}

audioguy
Posts: 8
Joined: Fri Dec 07, 2018 10:22 am

Re: Getting raw audio Samples

Postby audioguy » Sun Dec 16, 2018 9:10 pm

Thank you so much for showing me this method!
I'm currently experimenting with this callbackmethod and I got it to work somewhat.
I'm using this library for the fft https://github.com/fakufaku/esp32-fft
(blogpost:http://www.robinscheibler.org/2017/12/12/esp32-fft.html)
But you are using this

Code: Select all

fft_128_s16(pcm_fft, image, real)
function .
Is that some native library function or just a placeholder for where I should use my implementation.
Also im experiencing the issue, that when applying this callbackfunction to a pipeline it
only seems to work with the last element of the pipeline like referenced in the documentation .
This API allows the application to set a write callback for the last audio_element in the pipeline
but then this element (in my case a

Code: Select all

i2s_stream_writer = i2s_stream_init(&i2s_cfg);
)
does not forward the sound to the heatphonejack anymore.
Maybe you know something about that otherwise I will investigate this further myself.

In any case I will experiment more with this as I find time to do so and I hope I can
come back and ask you for help if I have problems I can't crack myself.

scruffynerf
Posts: 9
Joined: Sat Mar 30, 2019 5:04 pm

Re: Getting raw audio Samples

Postby scruffynerf » Wed Apr 10, 2019 2:46 pm

Did you make progress on this? I'm still trying to understand ways to manipulate the streams, and this seems like a good example.

drewandre
Posts: 5
Joined: Sat Mar 14, 2020 4:28 pm

Re: Getting raw audio Samples

Postby drewandre » Thu Mar 19, 2020 2:39 pm

I'm also curious if any progress has been made on this!

audioguy
Posts: 8
Joined: Fri Dec 07, 2018 10:22 am

Re: Getting raw audio Samples

Postby audioguy » Sat Apr 04, 2020 5:26 pm

At what part are you stuck ? in case your problem is that the sound isnt getting forwarded to the output anymore: i think my soulution was to modify the i2s library so that you can call the function _i2s_write(el,buf,len,wait_time,ctx); at the end of the callback and return its value. I think the function must be added to the header file i2s_stream.h or you need to inport i2s_stream.c instead i dont remember.
Let me know if you need more detailed information then ill look more into what i did there .

I myself am stuck because the Bluetooth audio stuff has some side-effect that prevents my led library from working while it does the bluetooth in the background and i dont have the necessary c skills to debug this :(

Who is online

Users browsing this forum: No registered users and 3 guests