esp-idf API examples?

Moderators: mdrjr, odroid

esp-idf API examples?

Unread postby briansm » Mon Jul 09, 2018 10:07 am

Hello again.
Can I ask for more help please? I've migrated my little ZX Spectrum emulator from Arduino-based to the esp-idf framework now, but am struggling again.

Are there are small 'demo' programs using the ODROID library, similar to the small 'hello world' style examples provided with the Arduino library? It is hard to figure out the exact API usage purely from looking at the source of the emulators. I've figured out the graphics side, but help with the format of the audio data going into the I2S bus in odroid_audio.c would be appreciated... at the moment I can only make the emulator make rough clicking noises only, I'm doing something wrong.

I compiled the gnuboy-go emulator but can't get it running, (it needs a ROM file as an argument perhaps?) I was hoping to capture the data being fed to the I2S bus in the audio of a running game to try and figure out the correct format for sound data, but it's a long-winded way of doing it, thought I might ask here first to save some effort. I've got 625 samples of 8-bit mono PCM audio data to play 25 times per second as generated by the emulator, which is wildly different to the 16 bit stereo data coming from the Gameboy.

(I'll get it figured out myself eventually anyway, but any help is appreciated. )

Thanks,
Brian.
briansm
 
Posts: 43
Joined: Wed Jun 27, 2018 3:35 am
languages_spoken: english
ODROIDs: Ordoid GO, Ordoid-W

Re: esp-idf API examples?

Unread postby crashoverride » Mon Jul 09, 2018 11:18 am

For esp-idf, you can use the same APIs all the emulators share:
https://github.com/OtherCrashOverride/go-play/tree/master/odroid-go-common/components/odroid

You can use just the audio component by initializing it:
https://github.com/OtherCrashOverride/go-play/blob/329b31c7833f94b0ac04b825d96f5cf8b51863d2/odroid-go-common/components/odroid/odroid_audio.h#L21
Code: Select all
void odroid_audio_init(int sample_rate);


And submitting your audio data (stereo, 16bit, interleaved frames):
https://github.com/OtherCrashOverride/go-play/blob/329b31c7833f94b0ac04b825d96f5cf8b51863d2/odroid-go-common/components/odroid/odroid_audio.h#L23
Code: Select all
void odroid_audio_submit(short* stereoAudioBuffer, int frameCount);


Alternatively, you can look at the code to see how its done and implement your own solution:
https://github.com/OtherCrashOverride/go-play/blob/master/odroid-go-common/components/odroid/odroid_audio.c
crashoverride
 
Posts: 3918
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1

Re: esp-idf API examples?

Unread postby davidknight247 » Mon Jul 09, 2018 8:01 pm

I’m personally very interested in getting a working spectrum emulator on this device. If you upload your code to github I’ll happily help to get this running. I have previously successfully ported a range of emulators to handhelds but my favourite has always been the speccy!
davidknight247
 
Posts: 29
Joined: Sun Jul 01, 2018 8:35 am
languages_spoken: english
ODROIDs: Odroid go

Re: esp-idf API examples?

Unread postby briansm » Mon Jul 09, 2018 8:43 pm

Got it working now, just had to remove the DC bias from the (unsigned) 8-bit audio data in the buffer (i.e. subtract 128 from each sample) and the sound works fine now, yay. It's not perfect but it works and doesn't slow the emulator down at all, runs at 100% no problem.

Next I will try and get loading and saving snapshots to SD card and some basic keyboard mapping implemented and then I'll put what I've got on github, but not stuck now at least.

David, I can email you a zip or tar.gz of what I have just now if you want to look at it though, it's still messy and full of traces etc but build-able, I'd be too embarrassed to put it on github at this point :)
briansm
 
Posts: 43
Joined: Wed Jun 27, 2018 3:35 am
languages_spoken: english
ODROIDs: Ordoid GO, Ordoid-W

Re: esp-idf API examples?

Unread postby rooted » Tue Jul 10, 2018 1:42 am

Definitely you two should get together and work on it, even if through email until you get the code to a place where you feel comfortable publishing.

Good stuff.
User avatar
rooted
 
Posts: 5473
Joined: Fri Dec 19, 2014 9:12 am
Location: Gulf of Mexico, US
languages_spoken: english
ODROIDs: C1, C1+, C2
XU3 Lite, XU4
N1
VU7+
HiFi Shield 2
Smart Power (original)

Re: esp-idf API examples?

Unread postby piku » Fri Aug 24, 2018 11:52 pm

crashoverride wrote:For esp-idf, you can use the same APIs all the emulators share:
https://github.com/OtherCrashOverride/go-play/tree/master/odroid-go-common/components/odroid

You can use just the audio component by initializing it:
https://github.com/OtherCrashOverride/go-play/blob/329b31c7833f94b0ac04b825d96f5cf8b51863d2/odroid-go-common/components/odroid/odroid_audio.h#L21
Code: Select all
void odroid_audio_init(int sample_rate);


And submitting your audio data (stereo, 16bit, interleaved frames):
https://github.com/OtherCrashOverride/go-play/blob/329b31c7833f94b0ac04b825d96f5cf8b51863d2/odroid-go-common/components/odroid/odroid_audio.h#L23
Code: Select all
void odroid_audio_submit(short* stereoAudioBuffer, int frameCount);


Alternatively, you can look at the code to see how its done and implement your own solution:
https://github.com/OtherCrashOverride/go-play/blob/master/odroid-go-common/components/odroid/odroid_audio.c


Your emulator code is really useful, I've been using it to try and make my own little game library for my Odroid-GO (http://www.ncot.uk/blog/posts/getting-down-to-the-metal/ and http://www.ncot.uk/blog/posts/graphics-programming-on-microcontrollers/). I'm not entirely sure how the two cores in the ESP32 interact with each other (the xQueueSend/Receive bits are kind of confusing), and occasionally I'll get a watchdog timeout. It also feels like my code is running at whatever speed the ESP is managing to process my code at, rather than a specific frame rate.

What I'm really stuck on though is how to produce sound. I've copied and pasted some of the code from the gameboy emulator and my Odroid can now produce pretty awful high pitched noises, but I've no idea how to make it do something a bit more useful. I'd really like play a WAV (or raw uncompressed audio loaded off the SD card) but what do I supply to
Code: Select all
void odroid_audio_submit(short* stereoAudioBuffer, int frameCount);
? I guess the "stereoAudioBuffer" is the sound data, but what is "frameCount"?

I was having a poke around the Gameboy emulation code, but I got lost trying to separate playing audio from gameboy emulation generating audio :)
piku
 
Posts: 2
Joined: Tue Jul 24, 2018 8:07 pm
languages_spoken: english
ODROIDs: Odroid GO

Re: esp-idf API examples?

Unread postby crashoverride » Sat Aug 25, 2018 10:21 am

piku wrote:I guess the "stereoAudioBuffer" is the sound data, but what is "frameCount"?

stereoAudioBuffer is pointer (array) of 16bit signed audio samples. "frameCount" is the count of frames where a frame is a left sample and a right sample. The parameter is required to know how much data to process from the stereoAudioBuffer pointer (array).

As an example, one (1) frame of audio is two (2) samples (left + right) which is four (4) bytes of data (16bit sample is 2 bytes * 2 samples for left + right).
crashoverride
 
Posts: 3918
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1

Re: esp-idf API examples?

Unread postby piku » Sat Aug 25, 2018 10:49 pm

crashoverride wrote:
piku wrote:I guess the "stereoAudioBuffer" is the sound data, but what is "frameCount"?

stereoAudioBuffer is pointer (array) of 16bit signed audio samples. "frameCount" is the count of frames where a frame is a left sample and a right sample. The parameter is required to know how much data to process from the stereoAudioBuffer pointer (array).

As an example, one (1) frame of audio is two (2) samples (left + right) which is four (4) bytes of data (16bit sample is 2 bytes * 2 samples for left + right).


I've managed to get what I think is audio data into the test program I'm writing, but I get an i2s error saying I'm trying to write too much data. Am I right in thinking that since you've written an emulator, your code is designed to feed small chunks of audio constantly, rather than what I want which is to play pre-made audio samples?

I did find the i2s_adc_dac example and this sort of works. I can hear the test audio, but it cuts out and stutters. Did you try that example project and get similar results too?
piku
 
Posts: 2
Joined: Tue Jul 24, 2018 8:07 pm
languages_spoken: english
ODROIDs: Odroid GO

Re: esp-idf API examples?

Unread postby crashoverride » Tue Aug 28, 2018 4:23 pm

piku wrote:I get an i2s error saying I'm trying to write too much data. Am I right in thinking that since you've written an emulator, your code is designed to feed small chunks of audio constantly, rather than what I want which is to play pre-made audio samples?

The size and count of data buffers are specified when initializing I2S. You will need to pick size appropriate for your intended use:
https://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/i2s.html
https://github.com/espressif/esp-idf/blob/51a4b4ba2716e7b57aeafa804c48f927d8d3895a/components/driver/include/driver/i2s.h#L139-L140
crashoverride
 
Posts: 3918
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1


Return to Arduino Coding Camp

Who is online

Users browsing this forum: No registered users and 3 guests