Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.






Sound wave generator - quick update

Posted by Trapper Zoid, 19 August 2012 · 616 views

I tried implementing the sound wave generation looping through hard-coded samples rather than algorithmically, but I have issues with the current implementation. The basic approach is straightforward but it took a couple of days to debug and the result still doesn't sound right. Apart from the output sounding lower quality, it is clearly out of tune. Not sure what is going on there - whether it's a discretisation thing, something with converting floats or merely another bug.

I decided to go with samples because it's very easy to add new wave forms. Create an array of floats for one wave period, plug it in, and you're done. AFAIK its the way sine waves were done in hardware due to how computationally expensive they are to calculate. Plus if I get around to adding normal non-looping sound files to this system it's done in the same way.

My gut feeling is that it is better to go back to implementing the core of this system as if it was on a hardware chip - using simple hard-coded algorithms, different for each tone, based on bit registers with different purpose per waveform - and then add a layer on top of that to convert instructions like "channel 1, triangle wave, 440Hz, half volume" into the right register values.

Eventually I want to play around with phase modulation, like what was used on the Yamaha chips used in Adlib sound cards or the Sega Genesis/Mega Drive, once I get my head around how the operators worked in hardware.




These days it might be better to just use the sin() function unless you are using huge amounts of wave generators. For a 44.1 kHz sampling rate, that means only 44100 sin() calls per second. This is a small fraction (much less than 1%) of what is possible in real time on current hardware. Unless you're developing for embedded or underpowered systems, I'd stick with generating the waves from scratch, rather than having to generate and then interpolate a wave table.
The OPL2 chip in Adlib cards used 18 oscillators, each representing a sine wave, and output at nearly 50kHz, so that's approaching a million trig calls a second.

I'm not sure what the speed is of the trig function calls on today's hardware; I'll make a note to check. I don't think it would be a bottleneck if I did use a million trig function calls on today's hardware, but I'd like to compare it with lookup tables regardless to test whether there is a noticeable difference to how it sounds.

November 2014 »

S M T W T F S
      1
2345678
9101112131415
16171819202122
23 24 2526272829
30      

Recent Entries

Recent Comments

Recent Entries

Recent Comments

PARTNERS