Some advice of DSP here...

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Some advice of DSP here...

Post by Anes »

As we know the NES generate sound at CPU cc freq, so if i have 44100 in my hardware i take it every 40.5~ so clocks.
I have been searching the web for info of sound filters, wich lead me to a bounch of DSP info wich i don't understand very well.

My question is: wich filter is the best for the best sound??
ANes
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Some advice of DSP here...

Post by lidnariq »

The best one to use is the one that you know how to implement :P

The simplest and wrongest way to generate audio is to just natively calculate everything at the target rate. This will produce ... passable, if lackluster, results. Pitches will be right, but there will be audible aliasing, and the higher frequencies on the noise channel will be wrong.

The simplest and not-wrong way is to generate the audio at the NES's native 1.79MHz, and use a sharp lowpass starting at ≈10kHz (such as a very long FIR filter, or some IIR filter like an elliptic, Chebychev, or Butterworth).

Slightly more complex is to generate the audio at the NES's native 1.79, and use a "decimating FIR lowpass filter".

Even more complex is do something like blargg's blip_buffer, which precalculates things such that it can natively generate audio at the target rate. Going into how it works is out of scope, for now.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Some advice of DSP here...

Post by thefox »

lidnariq wrote:Even more complex is do something like blargg's blip_buffer, which precalculates things such that it can natively generate audio at the target rate. Going into how it works is out of scope, for now.
Of course you don't have to understand how it works to be able to use it. Even if you're writing an emulator, you don't have to implement absolutely everything yourself. :)
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

lidnariq wrote:The simplest and not-wrong way is to generate the audio at the NES's native 1.79MHz, and use a sharp lowpass starting at ≈10kHz (such as a very long FIR filter, or some IIR filter like an elliptic, Chebychev, or Butterworth).
I really suck about DSP. Any good advice/book/article/link to start with??
ANes
User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 569
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Some advice of DSP here...

Post by Jarhmander »

In fact, it all goes with the definition of "added value". Is there an added value to implement the audio resampling yourself? If you're genuinely interrested in DSP, then go ahead, you'll learn something. You have a neat and clever idea to test out? Go for it, maybe you'll find novel ways to do proper audio with good speed/accuracy. Otherwise, if you just want to get good audio from your emulator and spend time in things you think is more worth your time, then don't waste it uselessly and use a library that does it already (blip_buffer for instance).
((λ (x) (x x)) (λ (x) (x x)))
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Some advice of DSP here...

Post by tepples »

Anes wrote:I really suck about DSP. Any good advice/book/article/link to start with??
Did you try Bores?
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

Good source Tepples but i think i will use blip_buffer C code.

Now the problem is that i don't know even how to use the code. I took a look to the "demos" but it's greek for me.
What such a noob i am.

Any help?? at least a starting point.
ANes
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

Ok im totally lost, but i found a linear interpolation fn in wikipedia:

Code: Select all

// Precise method which guarantees v = v1 when t = 1.
float lerp(float v0, float v1, float t) {
  return (1-t)*v0 + t*v1;
}
So i have 735 samples (NTSC) is it well to do a:

Code: Select all

void linearinterpolation(float * src, int len, float * dest, int f)
{
	int i;
	float sample;
	for (i = 0; i < len - 1; i++)
	{
		sample = lerp(src[i], src[i+1], f);
		dest[i] = sample;
	}
}
I have the 735 buffer filled, and a "dest" buffer wich is then passed to directsound (of course there is an unsigned 16 bit conversion).

The sound plays, i think i don't hear any "better" sound and the worst is that there are some glitches.
ANes
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Some advice of DSP here...

Post by rainwarrior »

What you're doing does not look like useful code. What is "f" supposed to do?

In audio, linear interpolation means that you are converting a buffer of one length into a buffer of another length (or alternatively, one speed/samplerate to another). It should look more like:

Code: Select all

for (i = 0; i < len_out; ++i)
{
    // convert output position to input position
    float pos_in = float(i) * float(len_in) / float(len_out);
    // calculate blend value for linear interpolation
    float alpha = pos_in - floorf(pos_in);
    // find the two samples to interpolate
    int p0 = int(pos_in);
    int p1 = p0 + 1;
    dest[i] = lerp(src[p0], src[p1], alpha);
}
This isn't really optimal code, and it probably isn't even what you want to be doing, but I'm just trying to give an idea of what linear interpolation for resampling is. This technique is really only for upsampling, not downsampling.

If downsampling, you want something like a box filter. Basically take all the samples for a window of time in the input buffer, and average them together to make your output sample that represents that block of time (the samples at the beginning and end of the window will have reduced weight, since the window will probably begin and end partway through the sample).
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

rainwarrior wrote:If downsampling, you want something like a box filter. Basically take all the samples for a window of time in the input buffer, and average them together to make your output sample that represents that block of time (the samples at the beginning and end of the window will have reduced weight, since the window will probably begin and end partway through the sample).
Ok, im learning thanks.

You say a "window of time", that's my 735 samples.
Basically take all the samples for a window of time in the input buffer, and average them together to make your output sample that represents that block of time
Could you be more verbose about "average them together"??

Sorry being so noob.
ANes
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

The thing is that I CAN avarage 735 samples, so the sum of S0 + S1, +Sn divided by 735 leaves me with only one sample??
ANes
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Some advice of DSP here...

Post by Quietust »

Anes wrote:
rainwarrior wrote:take all the samples for a window of time in the input buffer, and average them together to make your output sample
You say a "window of time", that's my 735 samples.
No - your "window of time" is ~40.5844 samples (i.e. 1.79MHz divided by 44.1kHz).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Some advice of DSP here...

Post by Anes »

So im doing it well becouse im averageting 40.5~ so clocks and then i divide the average var by that number.
The emu doesn't sounds bad, but compared to fceux or others is poor....
What does this emulators do to sound that way?? Is there any code/lib??
ANes
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Some advice of DSP here...

Post by tepples »

Emulators might use a simple FIR lowpass like a box filter, or some sort of big FIR, or some IIR, or the simplified version of blip buffer.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Some advice of DSP here...

Post by rainwarrior »

A box filter is a FIR filter, just a really simple one. Generically, a FIR filter is a window where each sample in the window gets a specific weight. You multiply each sample in the window by that weight, and add them together to get your result. In the box filter, every sample has the same weight.

Is there a practical IIR for resampling? (I've seen the analog equivalent in ADCs, but not in a digital resampler.)

The blip buffer also uses FIRs, but the process is inverted. Instead of generating the high-samplerate signal and then downsampling, it synthesizes the result at the target samplerate by replacing discrete steps with FIRs.
Post Reply