[SOLVED] 4PCM_ENV popping sounds?

SGDK only sub forum

Moderator: Stef

Post Reply
KillaMaaki
Very interested
Posts: 84
Joined: Sat Feb 28, 2015 9:22 pm

[SOLVED] 4PCM_ENV popping sounds?

Post by KillaMaaki » Wed Jan 18, 2017 8:20 pm

Hello,
I've been messing about with SGDK in my spare time. One of the things I was trying to do was come up with a more flexible resource system for myself. The idea is that the resource system reads in a sort of manifest file, a JSON file which contains references to different files as well as metadata, and compiles those files into a few BIN files which are compiled into the game using Rescomp. The idea being each group of files, for instance audio, has a table in the output BIN file which can be used to map an index into an offset which can be used to get a pointer to the actual data. And since it stores the audio as an array in the manifest JSON file, it can somewhat easily map an audio path to an index, so that a data file which refers to an audio path can have that path transformed into a simple index in the resulting BIN file.

So, right now, I'm just working on the PCM sound. The layout of the file goes like this:

Code: Select all

u32 numSounds; // number of sounds which have been compiled
u32[] soundOffsets; // offsets from start of BIN blob to each sound's raw data
u16[] soundLengths; // length of each sound's raw data.

// header zero-padded to a multiple of 256 bytes so that raw audio data can be aligned on 256-byte address boundaries

s8[] rawSoundData; // just a big dump of all sounds' raw data. Use soundOffsets to find start of each sound, and soundLengths to find size of each sound. Each sound is also zero-padded to be a multiple of 256 bytes in length.
Some simple lookups and arithmetic are then used at runtime to turn an index into actual sound data (via a struct AudioClip, which holds u16 length and u8* sound data). The BIN itself is also 256-byte address aligned (no need to size align it, since my code already ensures that) in my resource.res file.

The thing is, this works almost perfectly except for one detail: every single sound has a pop at the end of it. It's the exact same pop for all sounds if I look at it in Audacity (recording emulator via WASAPI loopback), so it's probably not something funky like accidentally playing the next sound's data (otherwise each one, being at a different location in memory, would theoretically have a different pop). Even a purely silent sound exhibits this pop, despite verifying with a hex editor that the output file has all zero bytes, and in Audacity the levels don't change whatsoever until the pop (so it's not a DC offset issue or anything).

I can work on other stuff in the meantime since the sounds *do* play like they're supposed to and sound just like they're supposed to, but that popping is really damn irritating and I'd love to get any help on resolving it. It seems for all the world like a bug in SGDK, but I can also try compiling the WAV file with Rescomp and playing that to see if it has the same issue there too.
Last edited by KillaMaaki on Thu Jan 19, 2017 9:53 pm, edited 1 time in total.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: 4PCM_ENV popping sounds?

Post by Stef » Thu Jan 19, 2017 3:49 pm

I guess you are using raw PCM data so you don't convert your sample with rescomp, you just put the raw data inside your rawSoundData[] array and you align them on 256 bytes. The problem is about the alignment, in rescomp i do take care of doing a linear fade from the last sample value to 0 in the alignment data. If you don't do that and your sample isn't ending on a 0 value then you will hear a pop because of the fast change from "last sample value" to 0 with the 256 bytes alignment.

Edit: You said that even silent data pop, so it cannot be the problem i was speaking about... I need to check that, i don't remember the sound driver test rom exhibiting this pop.

KillaMaaki
Very interested
Posts: 84
Joined: Sat Feb 28, 2015 9:22 pm

Re: 4PCM_ENV popping sounds?

Post by KillaMaaki » Thu Jan 19, 2017 8:00 pm

Once this mental fog I'm having right now lifts (just caught some sickness, super fun stuff), I'm gonna try and use Rescomp to convert the WAV itself instead of my custom BIN file. If it doesn't pop, chances are I've done something dumb in my own code.

EDIT: OK I said I'd wait for my mind fog to clear but then I couldn't help myself.

So, compiling the same Silence.wav file with Rescomp results in a sound that is much noisier than my own converter, but doesn't pop at the end! Just as a matter of curiosity, I also manually initialized a 1024 u8 array with zeros - despite not being 256 address aligned, it still exhibits far less popping than the data from my BIN file. Something is very funky here...

EDIT 2:
Oh for god's sake.
I think I spotted it. Going through the file again, selecting between bytes 256-16383 (a selection 16128 bytes long starting at byte 256, as specified by the file header), I suddenly noticed a bunch of non-zero data at the end of it when actually it should all be zero bytes. I have no idea how those ended up in there, but I'm going to need to do some investigation here. Either my tool is spitting out wrong values, or it's spitting out the wrong length.

EDIT 3:
Yep, figured it out.
My WAV loading code in my .NET project is just dumb. I thought I could get away with just using something off of StackOverflow for quick prototyping but ofc that came back to bite me in the ass in that this code doesn't actually read any headers (in particular, the data size header), instead reading to the end of the file. Which works just fine and dandy until you've got a program, say, Audacity, which appends metadata to the end of your WAV file. Which explains why all of my sounds had the exact same pop - they had the exact same metadata appended.
Welp, might be time to throw in NAudio for some proper WAV loading.

KillaMaaki
Very interested
Posts: 84
Joined: Sat Feb 28, 2015 9:22 pm

Re: [SOLVED] 4PCM_ENV popping sounds?

Post by KillaMaaki » Thu Jan 19, 2017 10:02 pm

OK, it's been solved. Naturally, a basic law of the universe has been proven once again...

"It's probably just user error."

Ended up tossing NAudio into my resource compiler and using it to load the WAV files instead. My sounds now work like an absolute charm, no annoying popping or anything (also, interesting tidbit, converting samples to float and THEN to sbyte seems to result in far less noise than short to sbyte - going from short to sbyte results in the output alternating between 0x00 and 0xFF for some reason).

Anyway, case closed!

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: [SOLVED] 4PCM_ENV popping sounds?

Post by Stef » Fri Jan 20, 2017 9:37 am

Glad you fixed it :)
About the noise issue in conversion, it all depends from rounding law used. I know rescomp doesn't really take care of that. Also the resampling code in rescomp is dumb, making sound quality really bad. All audio processing definitely need to be done before if possible ;)

Post Reply