Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411624 Posts in 69391 Topics- by 58447 Members - Latest Member: sinsofsven

May 11, 2024, 06:40:50 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Sample-based audio synthesis
Pages: [1]
Print
Author Topic: Sample-based audio synthesis  (Read 1402 times)
Martin 2BAM
Level 10
*****


@iam2bam


View Profile WWW
« on: January 18, 2010, 03:19:02 PM »

Hi there, I got interested in audio synthesis lately, for a little game.
Mostly due to "keeping it simple" (on the authoring) I thought of sample-based synth...

Do you know any tutorials on theory and/or code about this topic?
I could try google but I'm completly lost, so I prefer to ask some directions first to avoid losing time.

Thanks in advance Smiley
Logged

Working on HeliBrawl
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #1 on: January 18, 2010, 04:11:02 PM »

What do you mean by sample based? Do you mean applying effects to existing samples, or generating completely new samples?

I'm making a synthesizer that does the latter... which I used to make Assemblee sounds...
« Last Edit: January 18, 2010, 04:19:04 PM by John Nesky » Logged
Martin 2BAM
Level 10
*****


@iam2bam


View Profile WWW
« Reply #2 on: January 18, 2010, 06:02:19 PM »

More like the former:
Having some pre-recorded samples (e.g. drums, gongs, guitar plucks, ...), applying some basic effects to them real-timeish (pitch/volume envelopes) and using them to make loops and eventually a simple song.

http://en.wikipedia.org/wiki/Sample-based_synthesis

Logged

Working on HeliBrawl
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #3 on: January 18, 2010, 06:07:13 PM »

and using them to make loops and eventually a simple song

Ah, that's an important bit of information that was missing from the first post. You're making beats. And that is an area in which I have no expertise, so I shall bow out now.

P.S. More wikipedia:
http://en.wikipedia.org/wiki/Sampling_(music)
Logged
Martin 2BAM
Level 10
*****


@iam2bam


View Profile WWW
« Reply #4 on: January 18, 2010, 08:30:40 PM »

Thanks for the link Smiley

Well, to clarify a bit more, I'm looking for algorithms (any language) that actually do the processing, anti-aliasing, etc, but oriented specially to pre-recorded sampling.
For example, I want to be able to change the pitch in order to play diferent notes... but I don't need to know how to make a trumpet-like sound by adding/subtracting signals.

In "hardware" it would be a sort of sequencer with presets but that in turn you could set the frequency/note of the sample on each step.

Thanks again
Best regards

Logged

Working on HeliBrawl
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #5 on: January 18, 2010, 09:47:30 PM »

I want to be able to change the pitch in order to play diferent notes

Ah, well pitch shifting is pretty simple. You need to change the rate of the sound. For example, if you've got the wave:
0, 1, 0, -1, 0, 1, 0, -1

You can cut the speed in half by playing each sample twice:
0, 0, 1, 1, 0, 0, -1, -1, 0, 0, 1, 1, 0, 0, -1, -1,

Although stretching it like this can result in quantization errors... a fancy name for the "stair-step" aliasing effects that you get when scaling an image. The same thing happens with audio and it's usually best to interpolate the wave when you stretch it:
0, 0.5, 1, 0.5, 0, -0.5, -1, -0.5, 0, 0.5, 1, 0.5, 0, -0.5, -1, -0.5

Anyway, by doubling the length of the wave, I have cut the frequency in half. This is equivalent to moving down one octave, aka 12 semitones. Doubling the frequency is going up an octave.

Here's some code that can shift the pitch of a sample:
Code:
int sample[] = {0, 1, 0, -1, 0, 1, 0, -1};
int sampleLength = 8;

float semitoneShift = -12; // one octave down

float samplePosition = 0;
float sampleSpeed = pow(2.0f, (float) semitoneShift / 12.0f);
while (samplePosition < sampleLength - 1) {
  int firstSampleIndex = (int) samplePosition;
  int secondSampleIndex = firstSampleIndex + 1;
  float secondScale = samplePosition - firstSampleIndex;
  float firstScale = 1.0f - secondScale;
  
  out[i++] = sample[firstSampleIndex] * firstScale +
             sample[secondSampleIndex] * secondScale;
  
  samplePosition += sampleSpeed;
}

This should do the same thing as I demonstrated above, but it will work with any pitch shift, up or down. (I think... I haven't compiled it as-is)

All that's left is to figure out the original pitch of your sample. I don't have any good algorithms for that off the top of my head, but basically a wave that looks like it roughly repeats 440 times per second is known as A440. If your sampling rate is 44100 samples per second (the standard) then a wave with a pitch of middle A will roughly repeat every 100 samples.

Ugh, the word "sample" is overloaded to refer to the entire wave, and to the values of the wave recorded at various points in time.  Huh?
« Last Edit: January 18, 2010, 09:54:58 PM by John Nesky » Logged
kometbomb
Level 0
***


View Profile WWW
« Reply #6 on: January 19, 2010, 02:28:45 AM »

Anti-aliasing sound samples is exactly the same as interpolating a 2D image, but in one dimension only. So try googling for that if you need more examples. The above linearly interpolation works but you can also use cubic interpolation and so on.

However, about the example, it doesn't work if the pitch is shifted up since you need to average the samples between the two steps. Again, it's exactly the same as it is with images, think mip-mapping.

Btw, linear interpolation should be done like this

Code:
X = (second - first) * i + first


It's both faster (you save one mul and also one div if not using floats) and actually the correct formula if you look it up in a math book.
Logged

LemonScented
Level 7
**



View Profile
« Reply #7 on: January 19, 2010, 03:58:07 PM »

http://www.musicdsp.org/archive.php has a whole bunch of cool stuff for audio processing like this. Generally speaking, Mr. Nesky described pitch-shifting pretty well. I think of it in terms of old analogue tape reels - if your trumpet sound (or whatever) digital waveform is the "tape" then the pitch is controlled by how quickly you move the playhead over it. Half the speed, and you go down an octave, double it and you go up an octave. By changing the playhead speed by various fractions you can go up or down in tones or semitones. In my experience, linear interpolation as described by kometbomb works just fine, and gives more than passable audio quality. Although in general it's not such a great idea to pitch sounds TOO far above or below their normal frequency, because they start to sound a bit weird, and any small inaccuracies in the pitch you calculate for the original sample will get bigger and bigger, which can make things sound pretty out of tune.

Mixing channels together is pretty easy as well. Assuming that your sample is stored as a bunch of normalised floats (i.e in the range 0.0 to 1.0, like in Mr. Nesky's example), you can just multiply all of those values by a "volume control" (which is just another number between 0 and 1) to balance individual channels, and then just add all of the channels together. You might want a final "master" volume control to turn the final mixed sound down to fit it back in the 0-1 range after you've added the channels together, because depending on format you use to send the sound to the speakers, it might end up getting clipped and distorted.

For what it's worth, I've built software synths and mixing programs like this before, and whilst it's really fun to do, it's definitely not "keeping it simple" from the standpoint of a game audio engine. Coding up some custom volume/pan controls, mixers and filters is one thing (although most audio APIs do that for you anyway), but building a whole sequencer can be very fiddly - not least because once you've done it you've then got to build some kind of interface that allow you to actually compose the tunes for your game! You also find yourself dealing with all kinds of nonsense to do with clicks, clipping, envelopes, optimisations involving managing various audio buffers, aliasing, quantization, Nyquist frequencies... It can get quite complex quite quickly. Unless you're planning on something with very specific algorithmic audio or musical effects for your game which absolutely requires this kind of runtime control, I'd say it's much simpler to find an audio program you like to compose the tunes, and then just export those as OGG or MP3 or WAV or whatever, and have your audio engine just play them as they've been composed.
Logged

Martin 2BAM
Level 10
*****


@iam2bam


View Profile WWW
« Reply #8 on: January 19, 2010, 05:42:35 PM »

Wow, this read has been really cool. Thank you, guys Beer!

Even with the warnings, I'll give it a shot... I'm also interested in learning, not just the final effect, so perhaps I'll be able to endure it hehe


So, thanks for the info and links! Good stuff Smiley

Cheers!
-Martín
Logged

Working on HeliBrawl
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic