gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Announcements And Comments > Article series on sound programming

#25406 - DekuTree64 - Sun Aug 22, 2004 1:31 am

Hi all, with the new compo underway and me being unable to enter, I can still join in on the fun by helping everyone out with a new article on sound mixing. Day 1 is complete, and can be read here (please excuse the ads). It covers the basics of playing a sound and setting up a double buffering system. Nothing too advanced yet, but if you have a few minutes, give it a read and tell me what you think.
Hopefully I'll be able to keep day 2 a little shorter and more to-the-point, and get it up by tomorrow night.

Thanks!
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#25407 - dagamer34 - Sun Aug 22, 2004 2:27 am

Anxiously waiting for Day 2... :)
_________________
Little kids and Playstation 2's don't mix. :(

#25411 - DekuTree64 - Sun Aug 22, 2004 9:38 am

Ha, day 2 complete a day sooner than I planned. This one goes through the basics of writing a mixer, with code for what I hope is a fully functional one. I haven't even tried to compile the code, but theoretically it would work.

Next time I'm planning to do a basic music player, or at least get through making a converter tool in preparation for it.
What I need to know from you guys is, what music format would you like to see?
MOD is simple, but kind of limited. S3M is cool, but doesn't have envelopes.
XM has envelopes, but I remember having trouble finding sufficient info to write a player in the past.
IT is the big kahuna, but really complicated. If I do this, it will be stripped down. Particularly, no dynamic channels. Nice as they are, they complicate things pretty badly and would likely give you more bugs than the extra freedom makes up for.

Let me know what you'd like to see and I'll get started when I have time.
Keep one thing in mind though: I'm doing this for fun, so the more people reply happily from it, the more fun it will be to write, and then I'll make more^_^
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#25412 - lordmetroid - Sun Aug 22, 2004 9:45 am

I think you should go for mod as you said it's the most simple one, and so people would rather understand the concept of how to write a player and then if they decide for another format they can still write a player but would need to put some more effort in it to suit the other kinds of formats, but writing the simplest player possible makes it a less complex tutorial and so easier to get a grip of...
_________________
*Spam*
Open Solutions for an open mind, www.areta.org

Areta is an organization of coders codeing mostly open source project, but there is alot of sections like GBA dev, Language learning communities, RPG communities, etc...

#25426 - dagamer34 - Sun Aug 22, 2004 7:41 pm

I'm having a bit of trouble grasping some of the code here. Perhaps if examples were available it would be easier to learn. :)
_________________
Little kids and Playstation 2's don't mix. :(

#25430 - DekuTree64 - Sun Aug 22, 2004 8:31 pm

Hmm, I'll see if I can get a working example going. Is there anything in particular that you'd like to see more commenting on, or another paragraph to explain? I'd like to make the tutorial as useful as possible, but I've been fiddling with these things so long I don't even know what parts are tricky anymore, so the feedback really helps.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#25442 - DekuTree64 - Mon Aug 23, 2004 2:35 am

Ok, day 2 has been updated with an [url="http://www.angelfire.com/wizard/deku/program/sound2.zip"]example[/url] demonstrating how to use the mixer from the tutorial. Amazingly enough, it compiled and worked after only fixing a couple of places where I wrote tempBuf instead of tempBuffer. I added a new define, SND_MAX_CHANNELS instead of hardcoding the 4 from the tutorial. Also, there's code for all the initialization stuff, and for showing how to actually start channels up.

As always, let me know what you think. In particular, anything that doesn't make sense and needs an explanation.

Also, has lordmetroid singlehandedly thrown the vote on music players to 100% MOD, or does anyone have a different preference?
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#25447 - SmileyDude - Mon Aug 23, 2004 4:53 am

nice articles so far, deku -- I wish I would've had them last year when I was working on my mixer :) I can't wait until they are done, that's for sure.

BTW -- I toss in my vote for MOD. It's a nice simple format, it's fairly common, and it's not very resource intensive. Plus, I would like to see another implementation of it, if only for my benefit -- I have some pretty horrid effect handling in my code :D
_________________
dennis

#25448 - abilyk - Mon Aug 23, 2004 5:27 am

Just wanted to shout a thanks out to Deku for taking the time to do these articles. I consider myself a pretty competent coder but I've always viewed sound programming as a scary, foreign undertaking. Though I still plan to use a third-party library for our first game, I've already learned a lot from the first article.

Regarding which music format to focus on, I don't know enough specifics about each to give an educated suggestion. I'd definitely suggest to keep it simple, but if you deem something more complicated than MOD a better learning tool, then by all means go for that.

#25465 - isildur - Mon Aug 23, 2004 5:04 pm

Great tutorial!

One thing though, when I clicked on Day 2, the popup add that popped had a virus in it, reported by McAfee. Just wanted to let you know.

#25466 - ScottLininger - Mon Aug 23, 2004 5:31 pm

In terms of which format to use, I'd lean toward one that has a decent, stable, and free program for building new songs. I know that S3M has Modtracker, which is pretty good. I'm not familiar with any MOD tools...

Tutorials that take you from point A to point C without providing easy tools for point B are really frustrating.

Thanks for the work, dekutree!

-Scott

#25487 - NoMis - Mon Aug 23, 2004 11:41 pm

Cool ^...^

NoMis

#25488 - tepples - Mon Aug 23, 2004 11:52 pm

Modplug Tracker, a GPL'd Windows app, can save in four formats: mod, xm, s3m, it. For a simple format to interpret, I'd suggest mod if your mixer uses signed samples or s3m if it uses unsigned samples.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#25514 - DekuTree64 - Tue Aug 24, 2004 9:06 am

Sorry all, but no new article today. The one on making a MOD converter is, as I feared, getting very long and cumbersome. I'm almost done with the loading and pattern conversion though, so after that I'll show how to pull it all together into a useful program that eliminates duplicate samples and such, and how to output all the data so the player will come out nice and friendly.
Must sleep for now though.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#25532 - dagamer34 - Tue Aug 24, 2004 3:46 pm

Do you plan on adding SFX channels to your player?
_________________
Little kids and Playstation 2's don't mix. :(

#25579 - DekuTree64 - Wed Aug 25, 2004 10:13 am

isildur wrote:
One thing though, when I clicked on Day 2, the popup add that popped had a virus in it, reported by McAfee.

Sheesh. I knew the popups were bad, but I didn't think Angelfire would host viruses. I'll see if I can get a payed account soon to get rid of them.

dagamer34 wrote:
Do you plan on adding SFX channels to your player?

Yup.

Ok, day 3 started to get so long I had to chop it in half. The first half of the original day 3 is up, but it's pretty boring until you get to the pattern conversion stuff. I tried to chop out as much unnecessary info as possible and get it to a more readable size, but there's quite a bit of code that takes up some space.
I kind of wish I would have just skipped over the MOD basics and gone straight to conversion, since there are plenty of good format references already, and I don't have a lot of free time to work on it (12 hours at work today, so I'm pretty fried), but that might have been a little confusing anyway.

Day 4 should be up tomorrow night, and will cover sound effects and outputting the data tables. With luck, I will be able to finish an example program for it as well, but it may just be the article.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#26078 - DekuTree64 - Tue Sep 07, 2004 1:58 am

Ok, I'm back at it again. Last weekend was soaked up by flying home to visit my family, and the rest of the week I was either at work or sleeping, which I will be returning to tomorrow, but for now I'm free.

Day 4 is up, and right here on ad-free GBADev.org, courtesy of Simon the Great.

I haven't fully proof-read it yet, and the example project won't be up until later tonight, but hopefully the article itself can be of some use. Too bad I probably won't be able to do the next one until saturday, it'll be a lot of fun to write.

Off to grill some hot dogs for the moment, but I'll finish up the example after that.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#26081 - DekuTree64 - Tue Sep 07, 2004 8:24 am

Example complete. I sleep.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#27287 - DekuTree64 - Fri Oct 08, 2004 10:36 am

Day 5 of the series is at last complete. Only one remaining for a complete music player, and one more after that for some crazy assembly fun.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#27290 - chrisrothery - Fri Oct 08, 2004 1:43 pm

Just wanted to say that even though I'm not ready to implement what you talk about in these tutorials, I have read them and they're excellent.

Good work and thanks for demystifying something that was set to give me a load of grief when I (finally) got there (from spectrum through amiga to pc & gba, nothing I have ever written has made a single sound, but after this, my first release has a chance of having a soundtrack - thanks to a friend who's musically inclined).

Thanks again deku.

#27548 - DekuTree64 - Fri Oct 15, 2004 9:22 am

Day 5 has been tweaked with a bunch of little changes to make things work better, and a few real bugs fixed, after actually putting it all to use preparing an example project. Check the notes at the top for all the little details.

The example ROM doesn't sound much like the original song yet, but it will soon. Day 6 looms on the horizon! (except I haven't started writing it yet)
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#27618 - allenu - Sun Oct 17, 2004 8:30 am

Hey, thanks for the great tutorials, DekuTree64. I was able to add sound to my game engine with a minimum of effort. I was surprised that the sound hardware was so easy to program for. I always figured sound programming would be difficult for some reason, but your tutorials made it easy.

Allen

#27632 - ProblemBaby - Sun Oct 17, 2004 8:03 pm

yes, your tutorials are really nice!
But I think it would be nice if you could add some info about other formats also.. especially XM (because that is what iam working with=))

Ive problems to make it sound as good as it does in the tracker.
and also problems with pitching because of the 256 finetune levels and it seems like they doesnt work as they do in a MOD-file ( I dont know!)

But except for that I really like your tutorials Ive learnt a lot of stuff!
Thanks!

#27635 - Touchstone - Sun Oct 17, 2004 8:30 pm

ProblemBaby wrote:
I think it would be nice if you could add some info about other formats also.. especially XM (because that is what iam working with=))

If you download FastTracker 2 you get a text file explaining the XM format. You can download it from here: http://www.pouet.net/prod.php?which=13350
_________________
You can't beat our meat

#27636 - DekuTree64 - Sun Oct 17, 2004 9:01 pm

Thanks for your comments guys^^ I'm working on day 6 (effects) right now. I had a bit of difficulty deciding how I wanted to do it, but it's going good now. Should be much more understandable than the 'easy' way of handling effects, just putting ifs all over to handle them where needed (that's what I tried when first writing a player and I never did get it working quite right).

That's a good idea scattering some info around for concerns with other formats. I'll see if I can squeeze some tidbits into the previous issues wherever they would make the most sense to explain.

I've never actually worked with XM before, but upon closer examination of the MOD frequency table, the finetune values are just linearly interpolated between the periods/frequencies. XM is probably the same, so you can just store the basic note frequencies and calculate the finetunes mathematically. Like a finetune of 96 would be 3/4 of the way between the current note and the note above it, or -128 would be completely one note below.

As for making it sound as good as a PC, it probably won't. Try running it in MODPlug with the frequency set to 19KHz or whatever its lowest is, 8-bit mono, and resampling off to get a better comparison, but that still sounds slightly filtered to me, to smooth out the high-frequency aliasing screechyness of chip sounds played with nearest neighbor sampling.
You could also try playing the song with Krawall to get an idea what kind of quality is possible on the GBA itself.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#27640 - ProblemBaby - Sun Oct 17, 2004 10:22 pm

The freeware version of krawall only supports skm=/
but it was a nice I idea to change the mixing setup in ModPlug
Never thought about that.
It doesnt sound that good anymore but my player still sounds more crap.
And some samples sounds louder then it does in my player even cause I use clipping.

Touchstone: Ive read it and it is nice, without it I wouldnt even had a XM loaded. But some stuff is still hard to.

Dekutree64: Yes I also thought it was like that, but this line confuse me:
FineTune = -128..+127 (-128 = -1 halftone, +127 = +127/128 halftones)[/code]

#27645 - DekuTree64 - Mon Oct 18, 2004 3:59 am

Ah, forgot about that little detail of Krawall. Well you can still get an idea what general sound quality to expect by listening to songs on other players.

And for the volume, if just some sounds are too quiet, the problem is probably in your effect processing more than the mixer. Clipping will only let you use higher final volume without getting serious distortion, but shouldn't have any effect on individual channels. Especially when you go adding in the volume-column effects, it gets really confusing handling them all properly.

As for the finetunes, that line you copied is basically what I was guessing at. That 127/128 thing just means that going one more finetune level up would be a full note above the original. What IS important though, is wether the finetune frequencies go up linearly or exponentially. Notes go exponentially by 2^(note/12), which is why you have to put them in a table. The finetunes, seem to be linear between notes though, so just multiply by finetune and divide by 128 type of thing.
For a hint, the standard interpolation formula is a*(1-x)+b*x, where a and b are the 'start' and 'end' values, and x is the distance between them (0-1). It'll be a little different when x (the finetune) is negative (x-1 instead of 1-x, I think) but that formula is always a good place to start.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#27653 - ProblemBaby - Mon Oct 18, 2004 12:43 pm

Okey I thought it also were expontially
but if it is linear it is trivial
I think this will do the job.
But which one is correct in the postive case

Code:

if (finetune < 0)
{
   ftune = ((FreqTable[Note - 1] - FreqTable[Note]) * fintune) >> 7;
   freq = FreqTable[Note] - ftune;

}
else
{
   // which one is correct
   ftune = ((FreqTable[Note + 1] - FreqTable[Note]) * fintune) >> 7;
   ftune = ((FreqTable[Note + 1] - FreqTable[Note]) * (fintune+1)) >> 7;
   freq = FreqTable[Note] + ftune;
}


And about the volumes, I think it has something to do with the volume envelopes. I dont have any effects and the volume envelope is just a straight line in the middle.
But....
if I push at the VOL button in the Instrument editor in ModPlugTracker
it sounds like it does in my player, but if I draw a straight line at the top, it sounds like it does when the VOL button is unmarked.
Ive no idea of why it is like that...

#27721 - ProblemBaby - Wed Oct 20, 2004 12:56 am

I solved it!
Ive always thought that my sounds, sounds a bit crappy and the volume to low, Well now its solved I made a stupid mistake to always think that the problem was my mixer. I didnt read the XM spec carefully.
The sampledata is stored as delta values=)

#27820 - Gene Ostrowski - Thu Oct 21, 2004 8:08 pm

Dekutree, thanks for the excellent articles on this topic. I am at a point where I will be needing to add MOD and SFX support to my game (actually, removing my reliance on Krawall), and this is coming at a good time.

I'm having difficulty in getting the example compiled-- what environment are you using? There seem to be some missing headers such as data.h, irq.h and misc.h, and a bunch of defines such as SOUNDA_LOUT, SOUNDA_FIFORESET, etc., not to mention the div() function and a div_t datatype definition...

I'm using GCC that was installed with HAMDEVKIT (not using HAM, though), and I searched through the files for these defines to no avail. I don't see them in my devkitadv-r5 install either, so I'm wondering where they came from.

Once I get this to compile and run correctly, I'll be more than happy to help you get this optimized and bug-free before you convert the mixer to asm. I'll be more than happy to donate some time to help you out with this, and we'll all benefit.

One optimization that should speed up the mixer significantly would be to perform a check before anything is mixed for a channel which determines if the end of the sample will be reached at all during the loop:

Code:

// first, check and see if it will reach the end or not
if (chnPtr->pos+(samplesToMix*chnPtr->inc) >= chnPtr->length)
{
   // yes, at some point it will hit the end
   // (insert the original "for i = 0 to samplesToMix" loop here)
   // which can also be optimized, since we can calculate the sample
   // will actually cause the overflow...

}
else
{
   // nope, so just mix them all, avoiding all "if" checks, since we will
   // never reach the end anyway
   for(i = 0; i < samplesToMix; i++)
   {
      // mix a sample into the intermediate buffer
      tempBuffer[i] += chnPtr->data[ chnPtr->pos>>12 ] * chnPtr->vol;
      chnPtr->pos += chnPtr->inc;
   }   
}

_________________
------------------
Gene Ostrowski

#33399 - ProblemBaby - Sun Jan 02, 2005 10:03 pm

Have you stopped writing the article series?=/
I hope you haven't cause it is really good!
and I look forward to read the two last days.

#33401 - DekuTree64 - Sun Jan 02, 2005 11:37 pm

Nope, I haven't declared them entirely dead yet. I may split the part on effects into two though. I didn't like how it was going, due to the large amount of code needed even with my idea for a relatively clean function table-based way of handling effects.
I was just looking at it the other day though and it's really not that bad, just a little cluttered to handle a few tricky effects. So, I'll see if I can get back going on it and make the current one the prep-work with a few simple effects, and then do the rest next time.

Thanks for the encouragement, makes it much easier to write when I know someone will actually learn from it :)
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#33797 - DekuTree64 - Sun Jan 09, 2005 6:21 am

Yay!
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#42122 - DekuTree64 - Fri May 06, 2005 10:41 am

I finally got around to putting the last few finishing touches on these, and I'm happy to say the main series is finished with a fully functional MOD player. Actually ended up doing 2 new parts. Day 7, which has a bunch of little changes to preemtively fix problems that showed up at the end, as well as adding proper SFX handling, and day 8, which does all the effects.

Now, a question for all of you folks. What would you like to see next, assembly mixer optimizations, or a DS version? Or something different?
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#42128 - isildur - Fri May 06, 2005 2:24 pm

DekuTree64 wrote:
I finally got around to putting the last few finishing touches on these, and I'm happy to say the main series is finished with a fully functional MOD player. Actually ended up doing 2 new parts. Day 7, which has a bunch of little changes to preemtively fix problems that showed up at the end, as well as adding proper SFX handling, and day 8, which does all the effects.

Now, a question for all of you folks. What would you like to see next, assembly mixer optimizations, or a DS version? Or something different?


Optimizations would be nice ;-)

#42131 - tepples - Fri May 06, 2005 2:36 pm

What about (converted) S3M support, so that I can get volume effects and other effects at the same time?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.