Page 1 of 1

sound: live audio streaming

Posted: Fri Oct 22, 2010 8:54 pm
by ZakMcKracken
Hello,

i'm new in NDS programming, but not new at C / C++ programming.

I want to transform my Nintendo DS in a sort of microphone for live streaming.
That is, while i'm talking into the MIC, the sound buffer should come out to the speakers or (better!) to the hearphones if connected through the jack.

Maybe not really in real-time: a second's fraction is tolerated :roll:

I tried the following:

Code: Select all

#include <nds.h>
#include <stdio.h>

//the record sample rate
#define sample_rate  8000

//buffer to hold sound data for playback
u16* sound_buffer = 0;

//buffer which is written to by the arm7
u16* mic_buffer = 0;

//the length of the current data
u32 data_length = 0;

//enough hold 5 seconds of 16bit audio
u32 sound_buffer_size = sample_rate * 2 * 5;

//the mic buffer sent to the arm7 is a double buffer
//every time it is half full the arm7 signals us so we can read the
//data.  I want the buffer to swap about once per frame so i chose a
//buffer size large enough to hold two frames of 16bit mic data
u32 mic_buffer_size = sample_rate * 2 / 30;

//------ live ------
void live_micHandler(void* data, int length)
{
        if(!sound_buffer || data_length > sound_buffer_size) return;
        DC_InvalidateRange(data, length);
        dmaCopy(data, ((u8*)sound_buffer) + data_length, length);
        iprintf("+");
        soundPlaySample(sound_buffer, SoundFormat_16Bit, length, sample_rate, 127, 64, false, 0);
}

void live_start()
{
	data_length = 0;
	soundEnable();
	soundMicRecord(mic_buffer, mic_buffer_size, MicFormat_12Bit, sample_rate, live_micHandler);
}

void live_stop()
{
	soundMicOff();
}

//----------------------------

int main(void) 
{
	int key;
	bool recording = false;

	sound_buffer = (u16*)malloc(sound_buffer_size);
	mic_buffer = (u16*)malloc(mic_buffer_size);

	consoleDemoInit();

	iprintf("Press B for live streaming\n");

	while(1) 
	{
		scanKeys();
		key = keysDown();
		
		if(key & KEY_B)
		{
			recording ? live_stop() : live_start();
			recording = !recording;
			iprintf("LIVE %s\n", recording ? "start" : "stop");
		}

		swiWaitForVBlank();

	}
}

the "live_micHandler" should manage the output on the speakers, for every data recorded.
Intead, it stops at first block of data.

Someone could tell me why?

Thank you!
Regards

Re: sound: live audio streaming

Posted: Sat Oct 23, 2010 6:08 am
by zeromus
may not be safe to play that sound from the mic callback (some problem with it happening from a timer interrupt, perhaps? this is just a guess). better queue it from there and play it from your main loop. not sure how well playing multiple sounds with the buffers you receive from the mic callback will work... if I were you, I would setup a maxmod stream and feed it from your intermediate buffer as you receive it from the mic.

but that's all just a guess.

Re: sound: live audio streaming

Posted: Sat Oct 23, 2010 9:14 am
by ZakMcKracken
Hello zeromus,

thank you for your suggestion.

I don't have idea what "maxmod" is, but I'll do some researches :roll:

Maybe, if I dmaCopy the data in 2 buffers, then an interrupt will play my second buffer... it could be better?

Sorry for my questions, but I don't have any documentation of the libs, so I am trying to imagine how it could work.

SEE YA!

Re: sound: live audio streaming

Posted: Sat Oct 23, 2010 4:08 pm
by zeromus
youve got documentation for the libs in the header files