DMC unsigned samples?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

Let me start with square waves. This is the best music to test so far. ;)
Tell me if something's obviously wrong, please.
Attachments
Metroid_wav.zip
(60.49 KiB) Downloaded 138 times
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

Nothing's obviously wrong. No additional noise content is present, transitions look roughly consistent with a slightly overdamped interpolator.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

OK, I'm adding an highpass filtering, but one thing is bugging me. With code seems better to explain...

Code: Select all

int output_L; //L = left channel, or mono.
int previous_sample;

//at every APU clock
int value = pulse_1_volume + pulse_2_volume + ...

output_L += (value - previous_sample) << 8;
previous_sample = value;
num_of_updates++;

//on resample (PC sample generation, after appox. 37 APU updates)

//decay
output_L -= output_L >> 7;
unsigned short *wavebuffer = (output_L / num_of_updates) ^ 0x8000;  //here's my question!
The sound output isn't fine - it's not "crystal and clear", but... a bit noisy.
I had perfected the unsigned samples - no errors (listen the previous file), but on highpass, it's not fine.

What I'm doing wrong after all...!? :shock: :shock: :shock:
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

There's so many different little things...
output_L += (value - previous_sample) << 8;
previous_sample = value;
There's no need to multiply by 256 yet, you may as well skip that for now.
This is an extremely high frequency FIR high-pass filter, with a corner frequency at one sixth of the sample rate. Since the sample rate here is 1.8MHz, any audio that comes out is going to be awfully quiet... probably why you added the <<8.
unsigned short *wavebuffer = (output_L / num_of_updates) ^ 0x8000;
I think it's very likely that this varying num_of_updates is the source of the noise you had with the original resampler.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

Someone (ages ago) told me to do that for DMC/RAW PCM samples. So, I'm using that method for the APU output.
lidnariq wrote:There's so many different little things...
output_L += (value - previous_sample) << 8;
previous_sample = value;
There's no need to multiply by 256 yet, you may as well skip that for now.
This is an extremely high frequency FIR high-pass filter, with a corner frequency at one sixth of the sample rate. Since the sample rate here is 1.8MHz, any audio that comes out is going to be awfully quiet... probably why you added the <<8.
If I take out the << 8, how's made the decay >> 7 ???

Well, any suggestion? I don't know how to resample & apply the highpass filtering. That's the problem. :cry:
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

Zepper wrote:output_L -= output_L >> 7;
That line is equivalent to output_L = outputL*127/128; It's not actually related, unless you see signs of clipping or are losing audio quality by accidentally discarding bits off the little end.
Well, any suggestion?
Ok, so you have a no-obvious noise resampled version without the highpass. What changed between that version and the one you just posted? Just the (value-previous_sample) ?
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

I'm confused. What "versions" do you mean? The resample method is just summing all the generated samples and divide by the number of samples.
Plus, the "<< 8" is for 16-bit samples. :roll: I can't simply take it out.
Last edited by Zepper on Thu Jul 14, 2016 6:55 pm, edited 1 time in total.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

What is the difference in the source code that you made between the one that made the audio in this post

and the code fragment that you posted?
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

I recorded 3 wav files.
One using RockNES 5.24 signed samples.
The other two uses 5.25 - unsigned samples (perfect) and with the highpass filter (noisy).

For unsigned samples? No filtering, just output_L += pulse_1 + pulse_2 +..., then *wavebuffer = output_L / num_of_samples.
Attachments
rnes_wav.zip
(69.44 KiB) Downloaded 130 times
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

Just added high pass from wikipedia... but without your drop of magic, it wouldn't be working. :lol: :lol: :lol:

y(i) := a * (y(i-1) + x(i) - x(i-1))

y(i) is the new sample
y(i-1) is the previous sample

x(i) is the input sample
x(i-1) is the previous input sample

a is your magic, or sample*127/128; otherwise, for some reason, I was only getting unsigned samples. Care to explain? ;) Just compare the files.
Attachments
rnes_highpass.zip
(1.63 MiB) Downloaded 138 times
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

You elided enough of the code that it was hard to tell exactly what was going on.

It looked like the output -= output>>7 was happening on isolated samples after the sample rate change; in that case it's just a simple volume adjustment.

If that volume adjustment happens BEFORE the filtering is applied, then it will change the exact frequencies that are filtered.

If you used a simple first-order output-delay like this:
y[n] = a·(y[n-1] + x[n] - x[n-1]) →algebra→
y[n] = a·y[n-1] + k·(x[n]-x[n-1]) →Z transform→
Y = a·Yz¯¹ + a(X-Xz¯¹) →algebra→
Y(1-az¯¹) = a(1-z¯¹)X →algebra→
"transfer function" = Y/X = a(1-z¯¹)/(1-az¯¹)
fraction on the right is 0 when z is 1.
fraction on the right is infinite when z is 1/k.
If a is 127/128, this corresponds to a corner frequency of .000724597 times the sample rate, or 1300Hz at 1.8MHz, and 35Hz at 48kHz.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

One last thing. Anything about sample*k-1/k, with k a number power of 2? There's no audible differences as far as I can tell you - the waveform slightly changes.

- like 127/128 or 63/64 or 31/32 -
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: DMC unsigned samples?

Post by lidnariq »

The corner frequency of this highpass is very approximately samplerate÷k÷10, so you're not going to get a very noticeable difference in the range of factors you've listed here.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: DMC unsigned samples?

Post by Zepper »

Fine. Since I wasn't lucky with the low pass option (couldn't get signed samples), I'll keep this high pass filtering. Amazing and much more cleaner (and crystal) sound output.
Post Reply