Originally Posted by
DarknessX
The open_audio doesn't work with his method though... Open audio uses obtained.blah and and desired.blah which does not work in non-oop, while I was under the impression his example was non-oop.
EDIT: nevermind. I was thinking sdl_openaudio
Now it works, sort of.
And, .mp3 files don't work
The original SDL supported MP3's too... Is there anything specific I need to change to make it work with MP3's?
it only supports mp3 (and ogg format) if you use smpeg and Mixer_LoadMUS(), etc. instead of Mixer_LoadWAV().
like this:
Code:
Var
bAudioOpen : Boolean;
music : PMix_Music;
Initialization
Code:
bAudioOpen := False;
Log('Initializing...');
If SDL_Init(SDL_INIT_AUDIO Or SDL_INIT_VIDEO Or SDL_INIT_JOYSTICK) < 0 Then
Begin
Log('Could not initialize SDL');
Exit;
End
I open the audio
Code:
audio_rate := 22050;//MIX_DEFAULT_FREQUENCY;
audio_format := AUDIO_S16SYS;
audio_channels := MIX_DEFAULT_CHANNELS;
audio_buffers := 4096;
// Open the audio device
Log('Opening audio...');
If Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0 Then
Begin
Log(Format('Could not open audio: %s', [SDL_GetError]));
Exit;
End
I then play the music if that went ok:
Code:
Procedure PlayMusic(AFileName: AnsiString; loops: Integer);
Begin
Log('Loading music...');
music := Mix_LoadMUS(PChar(AFileName));
If (music = Nil) Then
Begin
Log(Format('Could not load "%s": %s', [AFileName, SDL_GetError()]));
Exit;
End;
Log('Music loaded');
If loops <> -1 Then
Begin
Log(Format('Playing: "%s"( looping )',[AFileName]));
End
Else
Log(Format('Playing: "%s"( not looping ) ',[AFileName]));
Mix_PlayMusic(music, loops);
Log('Playing');
End;
After everything is finished I cleanup
Code:
Procedure Cleanup;
Begin
If music <> Nil Then
Begin
Log('Freeing music...');
Mix_FreeMusic(music);
Log('Music freed');
music := Nil;
End;
If bAudioOpen Then
Begin
bAudioOpen := False;
Log('Closing audio...');
Mix_CloseAudio;
Log('Audio closed');
End;
SDL_Quit;
End;
My complete example program (ignore gp2x bits) that works on windows.
Code:
Program gp2x_sdl_mixer;
{$IFDEF fpc}
{$MODE Delphi} {$H+}
{$ENDIF}
Uses
sdl_mixer,
sdl,
SysUtils;
Var
bAudioOpen : Boolean;
music : PMix_Music;
screen : PSDL_Surface;
{..............................................................................}
{..............................................................................}
Procedure Log(AString: AnsiString);
Var
FileName : AnsiString;
LogFile : Text;
Begin
FileName := ExtractFilePath(ParamStr(0)) + 'LogFile.txt';
AssignFile(LogFile,FileName);
If FileExists(FileName) Then
Append(LogFile)
Else
Rewrite(LogFile);
WriteLn(LogFile,AString);
Flush(LogFile);
CloseFile(LogFile);
End;
{..............................................................................}
{..............................................................................}
Function Initialize: Boolean;
Var
audio_rate : integer;
audio_format : Uint16;
audio_channels : integer;
audio_buffers : integer;
Begin
Result := False;
bAudioOpen := False;
Log('Initializing...');
If SDL_Init(SDL_INIT_AUDIO Or SDL_INIT_VIDEO Or SDL_INIT_JOYSTICK) < 0 Then
Begin
Log('Could not initialize SDL');
Exit;
End
Else
Begin
screen := SDL_SetVideoMode(320,240,16,SDL_SWSURFACE);
If screen = Nil Then
Begin
Log(Format('Could not create window: %s',[SDL_GetError]));
Exit;
End;
Log('Window created');
SDL_ShowCursor(0);
audio_rate := 22050;//MIX_DEFAULT_FREQUENCY;
audio_format := AUDIO_S16SYS;
audio_channels := MIX_DEFAULT_CHANNELS;
{$IFDEF gp2x}
audio_buffers := 512;
{$ELSE}
audio_buffers := 4096;
{$ENDIF}
// Open the audio device
Log('Opening audio...');
If Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) <0> 1) Then
Log(Format('Opened audio at %d Hz %d bit %s', [audio_rate,
(audio_format and $FF), 'stereo']))
Else
Log(Format('Opened audio at %d Hz %d bit %s', [audio_rate,
(audio_format and $FF), 'mono']));
End;
End;
Result := True;
bAudioOpen := True;
Log('Initialized');
End;
{..............................................................................}
{..............................................................................}
Procedure PlayMusic(AFileName: AnsiString; loops: Integer);
Begin
Log('Loading music...');
music := Mix_LoadMUS(PChar(AFileName));
If (music = Nil) Then
Begin
Log(Format('Could not load "%s": %s', [AFileName, SDL_GetError()]));
Exit;
End;
Log('Music loaded');
If loops <> -1 Then
Begin
Log(Format('Playing: "%s"( looping )',[AFileName]));
End
Else
Log(Format('Playing: "%s"( not looping ) ',[AFileName]));
Mix_PlayMusic(music, loops);
Log('Playing');
End;
{..............................................................................}
{..............................................................................}
Procedure Cleanup;
Begin
If music <> Nil Then
Begin
Log('Freeing music...');
Mix_FreeMusic(music);
Log('Music freed');
music := Nil;
End;
If bAudioOpen Then
Begin
bAudioOpen := False;
Log('Closing audio...');
Mix_CloseAudio;
Log('Audio closed');
End;
{$IFNDEF gp2x}
SDL_Quit;
{$ELSE}
chdir ('/usr/gp2x');
ExecuteProcess('/usr/gp2x/gp2xmenu',['/usr/gp2x/gp2xmenu']);
{$ENDIF}
End;
{..............................................................................}
{..............................................................................}
Procedure WaitForKeyUp(AKey: Uint32);
Var
event : TSDL_Event;
done : Boolean;
Begin
done := False;
While Not done Do
Begin
SDL_Delay(1);
While SDL_PollEvent(@event) = 1 Do
Begin
If event.type_ = SDL_KEYUP Then
If event.key.keysym.sym = AKey Then done := True;
End;
End;
End;
{..............................................................................}
{..............................................................................}
Procedure WaitForJoystickButtonUp(AButton: Uint32);
Var
event : TSDL_Event;
done : Boolean;
Begin
done := False;
While Not done Do
Begin
SDL_Delay(1);
While SDL_PollEvent(@event) = 1 Do
Begin
If event.type_ = SDL_JOYBUTTONUP Then
If event.jbutton.button = AButton Then done := True;
End;
End;
End;
{..............................................................................}
{..............................................................................}
Begin
Try
DeleteFile(ExtractFilePath(ParamStr(0)) + 'LogFile.txt');
If Initialize Then
Begin
PlayMusic(ExtractFilePath(ParamStr(0)) + 'Pet Shop Boys - 02 - West End Girls.mp3',0);
{$IFNDEF gp2x}
WaitForKeyUp(SDLK_Escape);
{$ELSE}
WaitForJoystickButtonUp(SDLK_GP2X_START);
{$ENDIF}
End
Else
Log('Initializing failed.');
Except
On E:Exception Do Log(E.Message);
End;
Log('closing...');
Cleanup;
End.
I hope this helps
cheers,
Paul.
Bookmarks