Quote Originally Posted by KidPaddle
Paul,

have you any working sample with playing music/sound und timer? And if, coud you send me this sample?

Best Regards
Thomas
Hi Thomas,
The only sound stuff I could get working on the GP2X (and Win32) was using .wav files only and the code below (hacked together from C examples off the web). I still can't get SDL_Mixer working under the GP2X

The following code is not complete, but at least it plays .wav sounds...
The StopSound and all music methods don''t do anything yet (I was going to see if I could stream a large .wav file in chunks as music).

You use it like so:

First, you 'register' any sounds you want to load and play later:

Code:
FAudioModule.RegisterSound_WAV('miss_sound'         ,FDataPath + 'miss.wav');
then later on when you want to play the sound:
Code:
FAudioModule.PlaySound_WAV('miss_sound');
Code:
Unit EngineAudio_sdl;
{$IFDEF fpc}
{$mode delphi}{$H+}
{$ENDIF}
Interface

Uses
    SDL,
    Classes;

Type
{..............................................................................}
    TAudioModule_sdl = Class
    Private
        FSoundNames       : TStringList;
        FAudioInitialized : Boolean;
    Public
        Constructor Create;                                          
        Destructor  Destroy;                                         Override;
        Function  InitAudio: Boolean;                                
        Procedure RegisterSound_WAV(AName,AFileName: AnsiString);    
        Function  PlaySound_WAV    (AName : AnsiString): Boolean;    
        Procedure StopSound_WAV    (AChannel: Integer);              
        Procedure RegisterMusic_WAV(AName,AFileName: AnsiString);    
        Function  PlayMusic_WAV    (AName : AnsiString): Boolean;    
        Procedure StopMusic_WAV;                                     
        Procedure CloseAudio;                                        
    End;
{..............................................................................}

Implementation

Uses
    SDL_Mixer;
    
Const
{..............................................................................}
    cMaxSounds = 40;
{..............................................................................}

Type
{..............................................................................}
    TSound = Class
    Public
        channel : Integer;
        data    : PUint8;
        dpos    : Uint32;
        dlen    : Uint32;
        Constructor Create;
        Destructor  Destroy;           Override;
        Procedure Update(stream: PUint8; len: Integer); Virtual;
        Function  Playing : Boolean;
    End;
{..............................................................................}

{..............................................................................}
    TMusic = Class(TSound)
    Public
        Procedure Update(stream: PUint8; len: Integer); Override;
    End;
{..............................................................................}

Var
    Sounds : Array[0..cMaxSounds - 1] Of TSound;
    Music  : TMusic;

{..............................................................................}

{..............................................................................}
Constructor TSound.Create;
Begin
    Inherited Create;
    data := Nil;
End;
{..............................................................................}

{..............................................................................}
Destructor  TSound.Destroy;
Begin
    If data <Nil> len Then
        amount &#58;= len;
    SDL_MixAudio&#40;stream, @PByteArray&#40;data&#41;^&#91;dpos&#93;, amount, SDL_MIX_MAXVOLUME&#41;;
    Inc&#40;dpos,amount&#41;;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Function  TSound.Playing &#58; Boolean;
Begin
    Result &#58;= dpos < dlen;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TMusic.Update&#40;stream&#58; PUint8; len&#58; Integer&#41;;
Begin
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure MixAudioProc&#40;unused&#58; Pointer; stream&#58; PUint8; len&#58; Integer&#41;; cdecl;
Var
    i &#58; Integer;
Begin
    For  i &#58;= 0 To cMaxSounds - 1 Do
    Begin
        If Sounds&#91;i&#93; <> Nil Then
        Begin
            Sounds&#91;i&#93;.Update&#40;stream,len&#41;;
            If Not Sounds&#91;i&#93;.Playing Then
            Begin
                Sounds&#91;i&#93;.Free;
                Sounds&#91;i&#93; &#58;= Nil;
            End;
        End;
    End;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Constructor TAudioModule_sdl.Create;
Begin
    Inherited Create;
    FSoundNames       &#58;= TStringList.Create;
    FAudioInitialized &#58;= False;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Destructor TAudioModule_sdl.Destroy;
Begin
    FSoundNames.Free;
    Inherited Destroy;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Function  TAudioModule_sdl.InitAudio&#58; Boolean;
Var
    fmt &#58; TSDL_AudioSpec;
Begin
    Result &#58;= False;
    // Set 16-bit stereo audio at 22Khz
    fmt.freq &#58;= 22050;
    fmt.format &#58;= AUDIO_S16;
    fmt.channels &#58;= 2;
    fmt.samples &#58;= 512;        // A good value for games
    fmt.callback &#58;= MixAudioProc;
    fmt.userdata &#58;= Nil;

    // Open the audio device
    If SDL_OpenAudio&#40;@fmt, Nil&#41; < 0 Then
    Begin
        //fprintf&#40;stderr, "Unable to open audio&#58; %s\n", SDL_GetError&#40;&#41;&#41;;
        Exit;
    End;
    SDL_PauseAudio&#40;0&#41;;
    Result &#58;= True;
    FAudioInitialized &#58;= True;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TAudioModule_sdl.CloseAudio;
Begin
    If FAudioInitialized Then
        SDL_CloseAudio;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TAudioModule_sdl.RegisterSound_WAV&#40;AName,AFileName&#58; AnsiString&#41;;
Begin
    FSoundNames.Add&#40;AName + '=' + AFileName&#41;;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Function  TAudioModule_sdl.PlaySound_WAV&#40;AName&#58; AnsiString&#41; &#58; Boolean;
Var
    index    &#58; Integer;
    wave     &#58; TSDL_AudioSpec;
    data     &#58; PUint8;
    dlen     &#58; Uint32;
    cvt      &#58; TSDL_AudioCVT;
    FileName &#58; AnsiString;
Begin
    Result &#58;= False;
    If Not FAudioInitialized Then Exit;
    // Look for an empty &#40;or finished&#41; sound slot
    index &#58;= 0;
    While index < cMaxSounds Do
    Begin
        If sounds&#91;index&#93; = Nil Then Break;
        Inc&#40;index&#41;;
    End;
    If index = cMaxSounds Then Exit;
    FileName &#58;= FSoundNames.Values&#91;AName&#93;;
    // Load the sound file and convert it to 16-bit stereo at 22kHz
    If SDL_LoadWAV&#40;PChar&#40;FileName&#41;, @wave, @data, @dlen&#41; = Nil Then
    Begin
//        fprintf&#40;stderr, "Couldn't load %s&#58; %s\n", file, SDL_GetError&#40;&#41;&#41;;
//        return;
        Exit;
    End;
    SDL_BuildAudioCVT&#40;@cvt,
                      wave.format,
                      wave.channels,
                      wave.freq,
                      AUDIO_S16,
                      2,
                      22050&#41;;
    GetMem&#40;cvt.buf,dlen * cvt.len_mult&#41;;
    Move&#40;data^,cvt.buf^,dlen&#41;;
    cvt.len &#58;= dlen;
    SDL_ConvertAudio&#40;@cvt&#41;;
    SDL_FreeWAV&#40;data&#41;;

    // Put the sound data in the slot &#40;it starts playing immediately&#41;
    SDL_LockAudio;
    sounds&#91;index&#93; &#58;= TSound.Create;
    sounds&#91;index&#93;.channel &#58;= index;
    sounds&#91;index&#93;.data    &#58;= cvt.buf;
    sounds&#91;index&#93;.dlen    &#58;= cvt.len_cvt;
    sounds&#91;index&#93;.dpos    &#58;= 0;
    SDL_UnlockAudio;
    Result &#58;= True;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TAudioModule_sdl.StopSound_WAV&#40;AChannel &#58; Integer&#41;;
Begin
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TAudioModule_sdl.RegisterMusic_WAV&#40;AName,AFileName&#58; AnsiString&#41;;
Begin
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Function  TAudioModule_sdl.PlayMusic_WAV&#40;AName &#58; AnsiString&#41;&#58; Boolean;
Begin
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure TAudioModule_sdl.StopMusic_WAV;
Begin
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Procedure InitSounds;
Var
    i &#58; Integer;
Begin
    For i &#58;= 0 To cMaxSounds - 1 Do
        Sounds&#91;i&#93; &#58;= Nil;
End;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
Initialization
    InitSounds;
&#123;..............................................................................&#125;

&#123;..............................................................................&#125;
End.
I hope this helps,
cheers,
Paul