C SDL2 Mixer Play Wav from pointer

893 views Asked by At

So I have an archive that is loading the my .wav files into memory and I am trying to play a .WAV from a void* or handle in memory.

II cannot have all the .wav files in the directory and I have a function that is loading them into memory.

Here is some code that I am trying and I have tried many things , but a lot of it doesn't seem to work and I think that SDL libraries are looking for a physical file to load , but I cannot confirm that.

void *MusicTrack;

void Load_andPlay_music(ntrack){

OpenFileFromMPQ( MusicTracks[ntrack], &MusicTrack) // Opens Audio track from MPQ.
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
music = Mix_LoadMUS(MusicTracks[ntrack]);

// I thought that once the file is loaded it might be available as part of $PATH. This is not the case.

Mix_PlayMusic(music, -1);
}

> If I try this , ....

Mix_PlayMusic(MusicTrack, -1); 

I am met with error 'int Mix_PlayMusic(Mix_Music *,int)': cannot convert argument 1 from 'void *' to 'Mix_Music *'

If you have a better solution using SDL2 then I am willing to look at that too.

Thanks.

2

There are 2 answers

0
LUser On

I figured this out and it works fine

you can simply do something like this. You of course might have to change the Mix_openAudio params to meet your needs.

void  snd_init()
{
    printf("SND INIT\n\n");
    // Initialize SDL.
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {

        printf("ERROR : %s\n\n", SDL_GetError());
    }

    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0) {
        printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
    }
    SoundInited = 1;
}

//either some function or something to read the .WAV file into memory and have it gpbuffer point to it. 

void * gpbuffer;

void playmusic(){


Mix_Chunk * Music = Mix_QuickLoad_WAV((Uint8* )gpbuffer); 
Mix_PlayChannel(2, Music, 0);


}

EDIT I ran into Other problems with this and channels overlapping. So try this out if you need SFX and Music running at the same times.

    Mix_OpenAudio(22050, AUDIO_S8, 1, 1024);

    SDL_RWops* rw = SDL_RWFromMem(buffer, bytestoread);

    Mix_Music* Song =  Mix_LoadMUS_RW(rw,1);

    Mix_PlayMusic(Song, -1);
1
genpfault On

Use the (oddly undocumented) Mix_LoadMUS_RW() with a SDL_RWops from SDL_RWFromMem().