SFML community forums

Help => Audio => Topic started by: GarrickW on June 25, 2013, 03:00:22 am

Title: Modifying length of a sound sample?
Post by: GarrickW on June 25, 2013, 03:00:22 am
I am currently writing a program in which the user can play a simple virtual piano.  I load a sample representing a middle C, and I change the pitch of that sound to get the other notes the user requests.  However, I also want the player to be able to play notes of different lengths - quarter notes, half notes, etc.

Is there a way to dynamically alter the length of the sample?  I've been mucking around in Audacity trying to change the sample length to have multiple samples (I only really want five - staccato, quarter, half, three-quarter and full), but doing so makes the sample sound very scratchy.

If there is no way to do this, does anybody know what other approaches I may try?
Title: Re: Modifying length of a sound sample?
Post by: Laurent on June 25, 2013, 07:54:01 am
I'd generate the sounds myself programmatically. Since each of your sounds is made of a unique frequency, the corresponding audio samples are basically just a regular sine. This way you can control the frequency and the length very easily.
Title: Re: Modifying length of a sound sample?
Post by: GarrickW on June 25, 2013, 03:54:21 pm
That's an interesting suggestion.  I assume I would be making use of sf::SoundBuffer::loadFromSamples(), after creating my own array of samples, right?

I suppose I'll have to read up about the structure of audio samples, but this may be even better than using sample files.  Is there anything special I should know about the structure of the sample array to be fed into the SoundBuffer?
Title: Re: Modifying length of a sound sample?
Post by: Laurent on June 25, 2013, 04:31:39 pm
Quote
That's an interesting suggestion.  I assume I would be making use of sf::SoundBuffer::loadFromSamples(), after creating my own array of samples, right?
Right.

Quote
Is there anything special I should know about the structure of the sample array to be fed into the SoundBuffer?
No, it's very simple.

You must generate a sine wave with the following characteristics:
- its frequency must match the desired sound's frequency (for example, ~262 Hz for a middle C)
- its amplitude will be the sound's volume, so this parameter is entirely up to you -- but you should use maximum amplitude and then use SFML functions to lower it in real-time if needed

You must fill your array of audio samples with values from this sine function ; the number of points depend on the desired sound length and sampling rate. For example, if you want 10 seconds at 44100 Hz, you must generate 44100 points in total.
Title: Re: Modifying length of a sound sample?
Post by: cpolymeris on June 28, 2013, 01:43:22 pm
If there is no way to do this, does anybody know what other approaches I may try?

This is a very interesting topic. One way sample based synths do this is having 2 or more samples for each sound. E.g.

Then, by varying the time the sustain part is looped, you can vary the overall playing time of the note. You might be able to do without the decay, just fading the sustain out.
Also keep in mind there is only so much you can do with one sampleset. For lower or higher tones the whole timbre of the note changes. So you might want a sample set for bass notes and one for treble notes in addition to your middle C. This is known as "multisampling".
Title: Re: Modifying length of a sound sample?
Post by: zsbzsb on June 28, 2013, 03:09:30 pm
I was also considering suggesting that, but it can get very complex very quickly. Hauptwerk is a well known virtual pipe organ program that uses this method. Take a look here (http://www.hauptwerk.com/faq/product/#8) for some more information.

In simplest terms you need to loop predefined parts of a sample over and over until you want to stop playing that sound.
Title: Re: Modifying length of a sound sample?
Post by: GarrickW on June 28, 2013, 09:08:00 pm
Thanks for the extra information!  This might come in handy.

In getting the basics down pat, though, I'm having some trouble.  I'm trying to figure out the basic construction of a decent sample array, and I'm getting scratchy sounds.  Here is what I have right now, trying to obtain a 1-second sound:

//For storing the samples
sf::Int16 Samples[44100];

//I want 44100 samples, since I want 44100 per second and one second of sound
for (int sample = 0; sample < 44100; ++sample)
{
    //This is essentially the time into the sound
    double Position = (double)sample / 44100.0;

    //Frequency of the curve is 262 Hz
    sf::Int16 Sample = sin(262*Position);
}

sf::SoundBugger CodeNote;
CodeNote.loadFromSamples(Samples, 44100, 1, 44100);

Given the scratchy, glitchy-sounding result, that's clearly not quite what I should be doing.  What am I misunderstanding?
Title: Re: Modifying length of a sound sample?
Post by: Laurent on June 28, 2013, 10:20:04 pm
Two mistakes:

1. The frequency of sin(x) is 2*pi, so if you want 262 the formula would be
sin(262 * Position * 2 * pi)

2. The amplitude of sin(x) is [-1, 1], whereas the range of sf::Int16 is [-32768, 32767]. With [-1, 1] you will hardly hear something.
Title: Re: Modifying length of a sound sample?
Post by: GarrickW on June 28, 2013, 11:51:37 pm
Ah, okay, so something more like this?

//Create a note from code
sf::Int16 Samples[44100];
//Frequency, in herz
for (int sample = 0; sample < 44100; ++sample)
{
    //This is essentially the value of X
    double Position = (double)sample / 44100.0;
    sf::Int16 Sample = 32767 * sin(262 * Position * 2 * PI);
}

CodeNote.loadFromSamples(Samples, 44100, 1, 44100);

This still gives me scratchy stuff, though it is different-sounding scratchy stuff. 
Title: Re: Modifying length of a sound sample?
Post by: Hiura on June 29, 2013, 12:25:50 am
You should increase compilation warning level because you're not using `Sample` (capitale S and no S at the end... not the two others `sample` & `Samples` variables..). Your array's elements are not initialised.

I admit it's hard to find good variable names but here it's too confusing.  ;)
Title: Re: Modifying length of a sound sample?
Post by: GarrickW on June 29, 2013, 01:59:13 am
@Hiura

D'oh!  Of course you're right.  I was making all those samples and not putting them in the array.  :o

Now it works.  Thanks!