Well now that i have grasped some DSP concepts i understand how Blargg's blip_buf.c SHOULD work, but i write "SHOULD" becouse i cannot get it working.
I want to know if someone uses it and maybe can help me.
Blargg's states in his .txt that:
I'm trying to make it to work in conjuntion with directsound and all i get is noisy distorded sound. I know that my DirectSound class is working well.
What i do is similar to following:
add_deltas(cpu_cc) is called after i emulate one CPU instruction and pass it to this function.
From the point of view of theory of what blip_buf.c does is great, but i cannot make it work.
I hope anyone has used this blip_buf.c and can help me a little.
I want to know if someone uses it and maybe can help me.
Blargg's states in his .txt that:
Code:
This library resamples audio waveforms from input clock rate to output
sample rate. Usage follows this general pattern:
* Create buffer with blip_new().
* Set clock rate and sample rate with blip_set_rates().
* Waveform generation loop:
- Generate several clocks of waveform with blip_add_delta().
- End time frame with blip_end_frame().
- Read samples from buffer with blip_read_samples().
* Free buffer with blip_delete().
sample rate. Usage follows this general pattern:
* Create buffer with blip_new().
* Set clock rate and sample rate with blip_set_rates().
* Waveform generation loop:
- Generate several clocks of waveform with blip_add_delta().
- End time frame with blip_end_frame().
- Read samples from buffer with blip_read_samples().
* Free buffer with blip_delete().
I'm trying to make it to work in conjuntion with directsound and all i get is noisy distorded sound. I know that my DirectSound class is working well.
What i do is similar to following:
Code:
static blip_buffer_t* blip;
void init_blip()
{
blip = blip_new( 48000/10); //Blargg's states that 1/10 it's enough for the allocated buffer
blip_set_rates( blip, 1789773.0, 48000); // NES NTSC clk and sample rate
}
void add_deltas(int cpu_cc)
{
/* Well, this is what Blargg's states: it should run on a loop and add the "deltas" */
for (int i = 0; i < cpu_cc; i++)
{
float sample = GetApuOutput(); //in float values
sample *= 0x07FFF; //i don't know this is right
blip_add_delta(blip, Sound.cpu_cc, (int)sample);
Sound.cpu_cc++;
}
}
/* He also states that at the end of a frame you should do something like this -> */
void write_buffer()
{
if (!SoundOut.IsPlaying()) // this is DSound things
SoundStartFirstTime();
blip_end_frame(blip, Sound.cpu_cc); // here i have a doubt: the second parameter, are the cc elapsed. It's a time var, i don't know
int avail = blip_samples_avail(blip); // Get the samples available
blip_read_samples(blip, Sound.buffer, avail, 0); //Copy the blarggs buffer to my DSound buffer in mono
SoundOut.WriteBuffer((char *)Sound.buffer, avail * 2); //Write to my DSound buffer
Sound.cpu_cc = 0; //reset cpu_cc cycles counter
}
void close()
{
blip_delete(blip);
}
void init_blip()
{
blip = blip_new( 48000/10); //Blargg's states that 1/10 it's enough for the allocated buffer
blip_set_rates( blip, 1789773.0, 48000); // NES NTSC clk and sample rate
}
void add_deltas(int cpu_cc)
{
/* Well, this is what Blargg's states: it should run on a loop and add the "deltas" */
for (int i = 0; i < cpu_cc; i++)
{
float sample = GetApuOutput(); //in float values
sample *= 0x07FFF; //i don't know this is right
blip_add_delta(blip, Sound.cpu_cc, (int)sample);
Sound.cpu_cc++;
}
}
/* He also states that at the end of a frame you should do something like this -> */
void write_buffer()
{
if (!SoundOut.IsPlaying()) // this is DSound things
SoundStartFirstTime();
blip_end_frame(blip, Sound.cpu_cc); // here i have a doubt: the second parameter, are the cc elapsed. It's a time var, i don't know
int avail = blip_samples_avail(blip); // Get the samples available
blip_read_samples(blip, Sound.buffer, avail, 0); //Copy the blarggs buffer to my DSound buffer in mono
SoundOut.WriteBuffer((char *)Sound.buffer, avail * 2); //Write to my DSound buffer
Sound.cpu_cc = 0; //reset cpu_cc cycles counter
}
void close()
{
blip_delete(blip);
}
add_deltas(cpu_cc) is called after i emulate one CPU instruction and pass it to this function.
From the point of view of theory of what blip_buf.c does is great, but i cannot make it work.
I hope anyone has used this blip_buf.c and can help me a little.