[SDL] [Need help] Why this code crached?

Igor Mironchick imironchick at gmail.com
Sat Jan 20 12:07:19 PST 2007


------------------- HPP ----------------------

#ifndef _CPL_AUDIO_HPP_
#define _CPL_AUDIO_HPP_

#include "SDL.h"
#include "SDL_audio.h"

namespace cpl_audio {

//
// struct sample_t
//

struct sample_t{
    SDL_AudioSpec m_spec;
    Uint8   *m_sound;
    Uint32   m_soundlen;
    Uint32   m_soundpos;
};// struct sample_t

//
// class cpl_audio_t
//

class cpl_audio_t {

public:
    cpl_audio_t();
    ~cpl_audio_t();

    void set_volume( const size_t vol );
    void play_sound( const char * file );

private:
    static size_t m_volume;
    static SDL_AudioSpec m_dev_spec;
    static SDL_AudioSpec m_desired_spec;
    static sample_t m_sample;
    static bool m_done;

    static void audio_callback( void *userdata, Uint8 *stream, int len );
};// class cpl_audio_t

}// namespace cpl_audio

#endif // _CPL_AUDIO_HPP_

------------------------ CPP ------------------------

#include "cpl_audio.hpp"

namespace cpl_audio {

SDL_AudioSpec cpl_audio_t::m_dev_spec;
SDL_AudioSpec cpl_audio_t::m_desired_spec;
sample_t cpl_audio_t::m_sample;
size_t cpl_audio_t::m_volume( SDL_MIX_MAXVOLUME );
bool cpl_audio_t::m_done( false );

cpl_audio_t::cpl_audio_t()
{
    m_desired_spec.freq = 22050;
    m_desired_spec.format = AUDIO_S16LSB;
    m_desired_spec.samples = 8192;
    m_desired_spec.callback = audio_callback;
    m_desired_spec.userdata = NULL;
    m_sample.m_spec.callback = audio_callback;

    m_sample.m_sound = 0;

    if ( SDL_OpenAudio( &m_desired_spec, &m_dev_spec ) < 0 ) {
        std::cout << SDL_GetError() << std::endl;
    }

    SDL_PauseAudio(0);
}

cpl_audio_t::~cpl_audio_t()
{
    SDL_CloseAudio();
    SDL_FreeWAV( m_sample.m_sound );
}

void
cpl_audio_t::set_volume( const size_t vol )
{
    SDL_LockAudio();
    m_volume = vol;
    SDL_UnlockAudio();
}

void
cpl_audio_t::play_sound( const char * file )
{
    SDL_AudioCVT cvt;
    SDL_AudioSpec spec;
    Uint8 *data;
    Uint32 dlen;

    if ( SDL_LoadWAV( file, &spec, &data, &dlen ) == NULL )
        return;

    if( SDL_BuildAudioCVT( &cvt, spec.format, spec.channels, spec.freq,
        m_dev_spec.format, m_dev_spec.channels, m_dev_spec.freq ) != 0 )
            return;

    cvt.buf = new Uint8[dlen * cvt.len_mult];
    memcpy( cvt.buf, data, dlen );
    cvt.len = dlen;

    if( SDL_ConvertAudio( &cvt ) != 0 )
    {
        SDL_FreeWAV( data );
        delete [] cvt.buf;
        return;
    }

    SDL_FreeWAV( data );

    SDL_LockAudio();

    free( m_sample.m_sound );

    m_sample.m_sound = cvt.buf;
    m_sample.m_soundlen = cvt.len_cvt;
    m_sample.m_soundpos = 0;
    m_done = false;

    SDL_UnlockAudio();
}

void
cpl_audio_t::audio_callback( void *userdata, Uint8 *stream, int len )
{
    if( m_sample.m_sound && !m_done )
    {
        Uint8 *waveptr;
        Uint32 waveleft;

        if( m_sample.m_soundpos < m_sample.m_soundlen )
        {
            waveptr = m_sample.m_sound + m_sample.m_soundpos;
            waveleft = m_sample.m_soundlen - m_sample.m_soundpos;

            SDL_MixAudio( stream, waveptr, waveleft, m_volume );
            m_sample.m_soundpos += len;

            if( m_sample.m_soundpos >= m_sample.m_soundlen )
                m_done = true;
        }
    }
}// void audio_callback(...)

-------------------- MAIN ------------------------


#include "cpl_audio.hpp"

#include "SDL.h"


int main(int argc, char *argv[])
{
    if ( SDL_Init( SDL_INIT_AUDIO ) < 0 )
        return 1;

    cpl_audio::cpl_audio_t audio;

    audio.play_sound( "test.wav" );

    while ( SDL_GetAudioStatus() == SDL_AUDIO_PLAYING )
        SDL_Delay(1000);

    SDL_Quit();

    return 0;
}


}// namespace cpl_audio

-- 
Regards,
Igor Mironchick,
Intervale (c)
#ICQ 492-597-570
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070120/43bc4cf3/attachment.html 


More information about the SDL mailing list