# SDL sound help

**URL:** <https://discourse.libsdl.org/t/sdl-sound-help/13274>\
**Category:** SDL Development\
**Created:** [April 23, 2006, 2:49am UTC](https://discourse.libsdl.org/t/sdl-sound-help/13274 "2006-04-23T02:49:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![norco\_at\_ten-arc.net](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@norco\_at\_ten-arc.net](https://discourse.libsdl.org/u/norco_at_ten-arc.net)\
**Post date:** [April 23, 2006, 2:49am UTC](https://discourse.libsdl.org/t/sdl-sound-help/13274/1 "2006-04-23T02:49:53Z")

</div>

Could anyone please link me to other SDL sound tutorials. I can’t get the  
one on the sdl site to work. I get no error messages but theres no sound.  
heres the code:

struct sounds {  
Uint8 \*data;  
Uint32 dpos;  
Uint32 dlen;  
};

sounds sndwelcome;

void LoadSounds()  
{  
int index;  
SDL\_AudioSpec wave;  
Uint8 \*data;  
Uint32 dlen;  
SDL\_AudioCVT cvt;

```
char file[60];
/* Load the sound file and convert it to 16-bit stereo at 22kHz */
sprintf(file, "chimes.wav");

```

if ( SDL\_LoadWAV(file, &wave, &data, &dlen) == NULL ) {  
fprintf(stderr, “Couldn’t load %s: %s\n”, file, SDL\_GetError());  
return;  
}

```
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,

```

AUDIO\_S16, 2, 22050);  
cvt.buf = (Uint8\*)malloc(dlen_cvt.len\_mult); // I had to add (Uint8_)  
or else it wouldnt compile. If this is the problem would anyone know  
how to fix it?

```
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
SDL_ConvertAudio(&cvt);
SDL_FreeWAV(data);

/* Put the sound data in the slot (it starts playing immediately) */
if ( sndwelcome.data ) {
    free(sndwelcome.data);
}
SDL_LockAudio();
sndwelcome.data = cvt.buf;
sndwelcome.dlen = cvt.len_cvt;
sndwelcome.dpos = 0;
SDL_UnlockAudio();

```

}

---

<div class="post-metadata">

**Author:** ![Peter\_Mulholland](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Peter\_Mulholland](https://discourse.libsdl.org/u/Peter_Mulholland)\
**Post date:** [April 23, 2006, 4:35pm UTC](https://discourse.libsdl.org/t/sdl-sound-help/13274/2 "2006-04-23T16:35:05Z")

</div>

Hello norco,

Sunday, April 23, 2006, 3:49:53 AM, you wrote:

ntan\> Could anyone please link me to other SDL sound tutorials. I can’t get the  
ntan\> one on the sdl site to work. I get no error messages but theres no sound.  
ntan\> heres the code:

Check how you are opening the sound library for a start.

[http://www.libsdl.org/intro/usingsound.html](http://www.libsdl.org/intro/usingsound.html)

Also, if what you want is to play multiple sounds or play sound in a  
"fire and forget" manner, you should use SDL\_mixer instead.

The re-cast on malloc() is normal - C++ specifies that all void \*  
returning functions must now have an appropriate cast.–  
Best regards,  
Peter mailto:@Peter_Mulholland
