0
0

Sorry for an uneducated question, but I need to produce my own waveforms, and am having some problems. I don’t mean for this code to do anything but give a quick popping sound or something to let me know it’s working, but the program doesn’t even run when I add it to my other code. (If I play a .xm or such instead, my program runs fine)

define SIZEBUFF 1024

define NUMCHANS 2

define NUMBUFFS 2

class AppSound {
public:
void init();
void close();
void update();
private:
FSOUND_SAMPLE *sample;
void *data0;
void *data1;
unsigned int len0;
unsigned int len1;
};

void AppSound::init() {
FSOUND_Init(32000,64,0);

sample=FSOUND_Sample_Alloc(FSOUND_FREE,SIZEBUFFNUMBUFFS,
FSOUND_STEREO|FSOUND_16BITS|FSOUND_LOOP_OFF|
FSOUND_2D,
128,128,128,100);
short *tmp=(short
)data0;
//High-freq sawtooth
for(int i=0;i<SIZEBUFFNUMCHANSNUMBUFFS;i++) tmp[i]=(i%6)4000;
FSOUND_Sample_Lock(sample,0,SIZEBUFF
NUMCHANSNUMBUFFSsizeof(short),
&data0,&data1,&len0,&len1);
FSOUND_Sample_Upload(sample,data0,FSOUND_STEREO|FSOUND_16BITS|FSOUND_LOOP_OFF|
FSOUND_2D);
FSOUND_PlaySound(FSOUND_FREE,sample);
}

void AppSound::close() {
FSOUND_Sample_Unlock(sample,&data0,&data1,len0,len1);
FSOUND_Sample_Free(sample);
FSOUND_Close();
}

void AppSound::update() {
}

I may be doing several things wrong here, but these functions aren’t very clear to me even after reading the manual and looking for examples.

Thanks for any help!

(Btw, I’m compiling this with g++ under Eclipse on Fedora2)

  • You must to post comments
0
0

Thanks very much! This is what I’m using now.

Two quick questions:
1. I’m not checking to see if the second Sample_Lock data pointer is being used. Is there any way to ensure that the ring buffer size is a multiple of the buffer size I’m using, so that I could perhaps not have to check for this condition?
2. I’m using a polling approach to see when to fill a new buffer and to start playing new data. Is this a good way to do it?

I really appreciate the help.

class AppSound {
public:
void init();
void close();
void update();
private:
FSOUND_SAMPLE *sample;
void *data0;
void *data1;
unsigned int len0;
unsigned int len1;
};

define APPSOUND_SIZEBUFF 1024 //In samples

define APPSOUND_NUMCHANS 2 //Mono/stereo

define APPSOUND_NUMBUFFBYTES \

      (APPSOUND_SIZEBUFF*APPSOUND_NUMCHANS*sizeof(short))

void AppSound::init() {
FSOUND_Init(44100,64,0);
sample=FSOUND_Sample_Alloc(0,APPSOUND_SIZEBUFF,
FSOUND_STEREO|FSOUND_16BITS|FSOUND_LOOP_OFF|
FSOUND_2D,128,128,128,100);
}

void AppSound::close() {
FSOUND_Sample_Free(sample);
FSOUND_Close();
}

void AppSound::update() {
if(!FSOUND_IsPlaying(0)) {
FSOUND_Sample_Lock(sample,0,APPSOUND_NUMBUFFBYTES,
&data0,&data1,&len0,&len1);
short data=(short)data0;
//Sawtooth (writes across both L and R channels)
for(int i=0;i<APPSOUND_SIZEBUFFAPPSOUND_NUMCHANS;i++)
data[i]=(i
1000)%25000;
FSOUND_Sample_Unlock(sample,&data0,&data1,len0,len1);
FSOUND_PlaySound(0,sample);
}
}

  • You must to post comments
0
0

Ah, okay, I used what you said to implement double buffering.
This is what I’m using now.

Thanks again for your assistance.

<code>

class AppSound {
public:
void init();
void close();
void update();
private:
FSOUND_SAMPLE *sample;
unsigned int buffIndex,oldBuffIndex;
};

define APPSOUND_SIZEBUFF 1024 //In samples

define APPSOUND_NUMCHANS 2 //Mono/stereo

define APPSOUND_NUMBUFFBYTES \

      (APPSOUND_SIZEBUFF*APPSOUND_NUMCHANS*sizeof(short))

void AppSound::init() {
FSOUND_Init(44100,64,0);

sample=FSOUND_Sample_Alloc(0,APPSOUND_SIZEBUFF*2,
FSOUND_STEREO|FSOUND_16BITS|FSOUND_LOOP_NORMAL|
FSOUND_2D,44100,128,128,100);
oldBuffIndex=1;
FSOUND_PlaySound(0,sample);
}

void AppSound::close() {
FSOUND_Sample_Free(sample);
FSOUND_Close();
}

void AppSound::update() {
if (oldBuffIndex!=
(buffIndex=(FSOUND_GetCurrentPosition(0)>=APPSOUND_SIZEBUFF))) {
oldBuffIndex=buffIndex;

void *data0,*data1;
unsigned int len0,len1;

FSOUND_Sample_Lock(sample,oldBuffIndex*APPSOUND_NUMBUFFBYTES,
                   APPSOUND_NUMBUFFBYTES,&amp;data0,&amp;data1,&amp;len0,&amp;len1);
short *data=(short*)data0;
//Wave
for(int i=0;i&lt;APPSOUND_SIZEBUFF;i++) {
  data[2*i]=data[2*i+1]=(short)(30000*sin(i*.05*(1+oldBuffIndex*10)));
}
FSOUND_Sample_Unlock(sample,data0,data1,len0,len1);

}
}

</code>

  • You must to post comments
0
0

Well, this should give me a little more flexiblity, considering that I may be trying to emulate some of these function calls with system-specific code in the future. And just playing around with different buffering schemes would be good practice for my novice sound skills.
Thanks yet again. Best wishes.

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.