SDL Change, raise/lower audio/sound pitch

Hi guys, I’m trying to raise the audio pitch when the up arrow key is
pressed and lower it when the down arrow key is pressed. I wrote a
code but I’m wondering why it doesn’t work, the reasons are probably
idiotic since it’s my first time playing with SDL.

void process_pitch(Uint8 *stream, int length) //same as audio_buf and buf_size

{

Uint32 size=length;

int i;

float pos=wave.soundpos;

Uint8 *sound_buffer=SDL_malloc(sizeof(Uint8)*length);

int factor=1;

float steps =0;

SDL_LockAudio();

for (i=0;i+(factor-1)<size;i+=factor)

{

SDL_memcpy(sound_buffer+i,wave.sound,factor);

steps+=speed;

pos=wave.soundpos+(steps*factor);

}

SDL_memcpy(stream,sound_buffer,length);

wave.soundpos=pos;

SDL_UnlockAudio();

SDL_free(sound_buffer);

}

----------------------------Here’s my callback
function:-------------------------
void audio_callback(void *unused, Uint8 *stream, int len) //used for
SDL audio callback
{
printf(“fillerup”);
Uint8 *waveptr;
int waveleft; //the amount of wave left to play XD
Uint8 *keys;

// Uint8 sound; / Pointer to wave data /
// Uint32 soundlen; /
Length of wave data /
// int soundpos; /
Current play position */

/*
int pos, i;

for(i = 0; i < len; i++){
pos = (int)wav_position;
my_sample[i] = wav_buffer[pos];
wav_position += speed;
while(wav_position >= wav_length){
wav_position -= wav_length;
}
}
*/

/* Set up the pointers, initialization */
waveptr = wave.sound + wave.soundpos;
waveleft = wave.soundlen - wave.soundpos;

/* Go! */
while ( len >= waveleft ) {
	/* Process samples */
	Uint8 process_buf[waveleft];
	SDL_memcpy(process_buf, waveptr, waveleft);
	process_audio(process_buf, waveleft);
	process_pitch(process_buf, waveleft);

	// play until the end of the audio
	SDL_memcpy(stream, process_buf, waveleft);
	stream += waveleft; //stream is the cumulative amount of waveleft XD
	len -= waveleft;

	// REPEAT
	waveptr = wave.sound;
	waveleft = wave.soundlen;
	wave.soundpos = 0;

}

/* Process samples */
Uint8 process_buf[len];
SDL_memcpy(process_buf, waveptr, len);
process_audio(process_buf, len);
process_pitch(process_buf, waveleft);

// play the processed samples
SDL_memcpy(stream, process_buf, len);
wave.soundpos += len;

}---------- Forwarded message ----------
From: @Anonymous_Incognito (Anonymous Incognito)
Date: Sun, 19 Oct 2008 12:22:38 +0700
Subject: SDL Change, raise/lower audio/sound pitch
To: sdl at lists.libsdl.org

Hi guys, I’m trying to raise the audio pitch when the up arrow key is
pressed and lower it when the down arrow key is pressed. I wrote a
code but I’m wondering why it doesn’t work, the reasons are probably
idiotic since it’s my first time playing with SDL.

I didn’t look at your code too closely, but one thing to worry about
here is buffer size. Raising the pitch and ending up with a smaller
sample will cause under-runs. (Callback system expects same amount of
data to come back, if I recall correctly.)

FWIW, I believe SDL 1.3 will have proper pitch shifting built-in.
See: http://code.google.com/soc/2008/sdl/appinfo.html?csaid=5CFE0967CB98719B

-bill!On Sun, Oct 19, 2008 at 01:04:11PM +0700, Anonymous Incognito wrote:

---------- Forwarded message ----------
From: Anonymous Incognito <st3p4an1e.8975 at gmail.com>
Date: Sun, 19 Oct 2008 12:22:38 +0700
Subject: SDL Change, raise/lower audio/sound pitch
To: sdl at lists.libsdl.org