Problems with sound

Post Reply
HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Problems with sound

Post by HeavyDude » Mon Jul 25, 2011 12:22 pm

Right first the disclaimer. I know very little about sound and I'm just experimenting.

I'm playing around with the sound registers and FIFO, just seeing what I can do.

The code below runs fine and produces sound but when I remove the iprintf I just get silence.

Can someone please explain why and how to rectify this?

Thanks.

Ok, here's the code.

ARM7

Code: Select all

#include <nds.h>
#include <dswifi7.h>
#include <nds/bios.h>

#define SAMPLE_RATE 22050

unsigned int buffer_size;
unsigned int snd_buffer[110];
 
void PlaySound(void)
{
 unsigned int t1;

  t1=fifoGetValue32(FIFO_USER_01);
  snd_buffer[buffer_size++]=t1;
  
  if(buffer_size > 100)
  {
   SCHANNEL_TIMER(0) = SOUND_FREQ(SAMPLE_RATE);
   SCHANNEL_SOURCE(0) = (uint32)snd_buffer;
   SCHANNEL_LENGTH(0) = buffer_size >> 2;
   SCHANNEL_CR(0) = SCHANNEL_ENABLE | SOUND_ONE_SHOT | SOUND_FORMAT_16BIT | SOUND_VOL(0x7F);

   SCHANNEL_TIMER(1) = SOUND_FREQ(SAMPLE_RATE);
   SCHANNEL_SOURCE(1) = (uint32)snd_buffer;
   SCHANNEL_LENGTH(1) = buffer_size >> 2;
   SCHANNEL_CR(1) = SCHANNEL_ENABLE | SOUND_ONE_SHOT | SOUND_FORMAT_16BIT | SOUND_VOL(0x7F);

   SCHANNEL_TIMER(2) = SOUND_FREQ(SAMPLE_RATE);
   SCHANNEL_SOURCE(2) = (uint32)snd_buffer;
   SCHANNEL_LENGTH(2) = buffer_size >> 2;
   SCHANNEL_CR(2) = SCHANNEL_ENABLE | SOUND_ONE_SHOT | SOUND_FORMAT_16BIT | SOUND_VOL(0x7F);

   SCHANNEL_TIMER(3) = SOUND_FREQ(SAMPLE_RATE);
   SCHANNEL_SOURCE(3) = (uint32)snd_buffer;
   SCHANNEL_LENGTH(3) = buffer_size >> 2;
   SCHANNEL_CR(3) = SCHANNEL_ENABLE | SOUND_ONE_SHOT | SOUND_FORMAT_16BIT | SOUND_VOL(0x7F);

   buffer_size=0;
 }
}

void VblankHandler(void)
{
 Wifi_Update();
}

void VcountHandler(void)
{
 inputGetAndSend();
}

int main()
{
 powerOn(POWER_SOUND);
 
 readUserSettings();
	
 irqInit();
 fifoInit();
	
 initClockIRQ();
 
 SetYtrigger(80);

 installWifiFIFO();
 installSoundFIFO();

 installSystemFIFO();

 irqSet(IRQ_VCOUNT, VcountHandler);
 irqSet(IRQ_VBLANK, VblankHandler);
 
 irqEnable(IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK); 

 REG_SOUNDCNT = SOUND_ENABLE | SOUND_VOL(0x7F);
 REG_SOUNDBIAS = 0x200;
 
 buffer_size=0;

 while(1)
 {
  if(fifoCheckValue32(FIFO_USER_01))PlaySound();
 }
 
 return 0;
}
ARM9

Code: Select all

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

int main(void)
{
 consoleDemoInit();
 
 int b=0;
  
 while(1)
 {
  if(fifoSendValue32(FIFO_USER_01, b * 8192))iprintf("On it's way");
  b++; 
  if(b > 1)b=0;
 }

 return 0;
}

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Problems with sound

Post by elhobbs » Mon Jul 25, 2011 12:59 pm

you may be overflowing the fifo buffer - are you sure that the ds is not crashing or the fifo is dead?

also, it is a little ambiguous what you mean by removing the iprintf. did you remove the if statement altogether? are you still modifying b to get a change in the sound buffer you are streaming? also, why are you playing the sound on four channels?

if you are trying to stream music/sounds you would probably be better off using the custom streaming ability of maxmod. it is a little tricky getting everything to flow smoothly without clicking/popping noises - maxmod handles all this for you and it is already optimized.

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Problems with sound

Post by HeavyDude » Mon Jul 25, 2011 1:25 pm

Thanks for the quick reply.

When I say removing iprintf I mean just deleting it and recompiling. Which leads me to think that iprintf was slowing things down allowing the FIFO buffer to empty before being refilled so I could be overflowing the FIFO buffer, filling it faster than it can empty itself.
also, why are you playing the sound on four channels?
As I said just experimenting.

I did look at maxmod but fancied doing it myself, just for fun. I may re-look at maxmod but would still like to produce sound this way just for my own interest. There is very little info on the net regarding sound reproduction using the sound registers so it's all trial and error.

Finally if it's not too daft a question, how do I ensure the FIFO buffer doesn't overflow?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Problems with sound

Post by elhobbs » Mon Jul 25, 2011 2:17 pm

sending/waiting for a response on both processors is one way. I think the fifo buffer is only 256 bytes. sending data a byte or a two at a time is going to be a huge overhead - lots of iterrupts are going to firing constantly. most sound libraries like maxmod usually use an uncached region of main system memory to transfer data between arm7 and arm9. then use fifo to send a pointer.

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Problems with sound

Post by HeavyDude » Mon Jul 25, 2011 2:21 pm

Ok, that makes sense.

Any chance of a bit of code to point me in the right direction?

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Problems with sound

Post by HeavyDude » Mon Jul 25, 2011 2:30 pm

I assume I use fifoSendAddress and fifoGetAddress but the bit I don't get is the uncached region of main system memory, (a) how to access it and (b) how to write to it.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Problems with sound

Post by elhobbs » Mon Jul 25, 2011 2:55 pm

use memUncached on the arm9 for a main memory pointer - either from malloc or a global - does't work for stack or other regions. send the original pointer to the arm7. use the result of memUncached on the arm9.

one other thing about fifo. there are fifoCheck* and fifoGet* functions for checking/waiting for fifo messages. you can use a data handler or these function on a channel per processor, but not both. it is probably obvious to most people - but, it had me baffled for a while as I tried to use both.

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Problems with sound

Post by HeavyDude » Mon Jul 25, 2011 3:16 pm

Ok, thanks for the advice.

I suspect I will be back to ask more daft questions.

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Problems with sound

Post by HeavyDude » Wed Jul 27, 2011 11:58 am

Just to say thanks to elhobbs for his invaluable advice and assistance. I managed to produce noise and now it's full steam ahead with my little project (which will probably end up as vapourware :lol: ).

Thanks again elhobbs.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests