Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program sdl_audio_test;
- { SDL1/SDL2 imports }
- const
- SDL_INIT_AUDIO = $00000010;
- AUDIO_S16LSB = $8010;
- AUDIO_S16 = AUDIO_S16LSB;
- SDL_MIX_MAXVOLUME = 128;
- type
- UInt8 = Byte;
- PUInt8 = ^UInt8;
- UInt16 = Word;
- UInt32 = LongWord;
- SDL_Double = Double;
- TSDL_AudioSpecCallback = procedure(userdata: Pointer; stream: PUInt8; len: Integer); cdecl;
- TSDL_AudioSpec = record
- freq: Integer;
- format: UInt16;
- channels: UInt8;
- silence: UInt8;
- samples: UInt16;
- padding: UInt16;
- size: UInt32;
- callback: TSDL_AudioSpecCallback;
- userdata: Pointer;
- end;
- PSDL_AudioSpec = ^TSDL_AudioSpec;
- PSDL_AudioCVT = ^TSDL_AudioCVT;
- TSDL_AudioFilter = procedure(cvt: PSDL_AudioCVT; format: Uint16); cdecl;
- TSDL_AudioCVT = record
- needed: LongInt;
- src_format: Uint16;
- dst_format: Uint16;
- rate_incr: double;
- buf: PUint8;
- len: LongInt;
- len_cvt: LongInt;
- len_mult: LongInt;
- len_ratio: SDL_Double;
- filters: array[0..9] of TSDL_AudioFilter;
- filter_index: LongInt;
- end;
- TSDL_RWops = record
- seek: Pointer;
- read: Pointer;
- write: Pointer;
- close: Pointer;
- type_: UInt32;
- end;
- PSDL_RWops = ^TSDL_RWops;
- function SDL_Init(flags: UInt32): Integer; cdecl; external;
- procedure SDL_Delay(msec: UInt32); cdecl; external;
- procedure SDL_Quit; cdecl; external;
- function SDL_GetError: PAnsiChar; cdecl; external;
- function SDL_OpenAudio(desired, obtained: PSDL_AudioSpec): LongInt; cdecl; external;
- procedure SDL_CloseAudio; cdecl; external;
- procedure SDL_PauseAudio(pause_on: LongInt); cdecl; external;
- procedure SDL_MixAudio(dst, src: PUint8; len: Uint32; volume: LongInt); cdecl; external;
- function SDL_ConvertAudio(var cvt: TSDL_AudioCVT): LongInt; cdecl; external;
- function SDL_BuildAudioCVT(out cvt: TSDL_AudioCVT; src_format: UInt16;
- src_channels: UInt8; src_rate: Integer; dst_format: UInt16; dst_channels: UInt8;
- dst_rate: Integer): Integer; cdecl; external;
- function SDL_LoadWAV_RW(src: PSDL_RWops; freesrc: LongInt; out spec:
- TSDL_AudioSpec; out audio_buf: PUInt8; out audiolen: UInt32): PSDL_AudioSpec; cdecl; external;
- function SDL_RWFromFile(filename, mode: PAnsiChar): PSDL_RWops; cdecl; external;
- procedure SDL_FreeWAV(audio_buf: PUInt8); cdecl; external;
- procedure SDL_LockAudio; cdecl; external;
- procedure SDL_UnlockAudio; cdecl; external;
- { Macros }
- function SDL_LoadWAV(filename: PAnsiChar; out spec: TSDL_AudioSpec; out audio_buf: PUInt8;
- out audiolen: UInt32): PSDL_AudioSpec;
- begin
- Result := SDL_LoadWAV_RW(SDL_RWFromFile(filename, 'rb'), 1, spec, audio_buf, audiolen);
- end;
- { Example starts here }
- var
- Sample: record
- Data: PUint8;
- Len: LongInt;
- Pos: LongInt;
- end;
- { The AudioMixer callback plays samples and controls volume levels }
- procedure AudioMixer(userdata: Pointer; stream: PUInt8; len: LongInt); cdecl;
- var
- Amount: LongInt;
- Cursor: PUint8;
- begin
- Amount := Sample.Len - Sample.Pos;
- if Amount < 1 then
- Exit;
- if Amount > len then
- Amount := len;
- Cursor := Sample.Data;
- Inc(Cursor, Sample.Pos);
- Inc(Sample.Pos, Amount);
- SDL_MixAudio(stream, Cursor, Amount, SDL_MIX_MAXVOLUME);
- end;
- { Initialize the sound system }
- function OpenSounds: Boolean;
- var
- Spec: TSDL_AudioSpec;
- begin
- if SDL_Init(SDL_INIT_AUDIO) <> 0 then
- begin
- WriteLn('SDL failed to initialize audio: ', SDL_GetError);
- Exit(False);
- end;
- Spec.freq := 22050;
- Spec.format := AUDIO_S16;
- Spec.channels := 2;
- Spec.samples := 512;
- Spec.callback := @AudioMixer;
- Spec.userdata := nil;
- if SDL_OpenAudio(@Spec, nil) <> 0 then
- begin
- WriteLn('SDL failed to open audio: ', SDL_GetError);
- Exit(False);
- end;
- WriteLn('SDL opened audio');
- SDL_PauseAudio(0);
- end;
- { Load and convert a WAV file, then pause while the sample file plays }
- procedure PlaySound(const FileName: AnsiString);
- var
- Wave: TSDL_AudioSpec;
- Convert: TSDL_AudioCVT;
- Buf: PUint8;
- Len: Uint32;
- begin
- if SDL_LoadWAV(PAnsiChar(FileName), Wave, Buf, Len) = nil then
- begin
- WriteLn('Failed to load audio file: ', SDL_GetError);
- Exit;
- end;
- { Build our format conversion }
- SDL_BuildAudioCVT(Convert, Wave.format, Wave.channels, Wave.freq,
- AUDIO_S16, 2, 22050);
- { Allocate memory and copy buffer from the loaded wave }
- Convert.buf := GetMem(Len * Convert.len_mult);
- Move(Buf^, Convert.buf^, Len);
- { Free the original buffer }
- SDL_FreeWAV(Buf);
- Convert.len := Len;
- { Convert the loaded wave to our format }
- SDL_ConvertAudio(Convert);
- { Lock the callback and populate Sample with the converted data }
- SDL_LockAudio;
- Sample.Data := Convert.buf;
- Sample.Len := Convert.len_cvt;
- Sample.Pos := 0;
- SDL_UnlockAudio;
- { Pause while we listen to the sound }
- WriteLn('Stating delay');
- SDL_Delay(5000);
- WriteLn('Ending delay');
- end;
- { Close the sound system }
- procedure CloseSounds;
- begin
- SDL_LockAudio;
- FreeMem(Sample.Data);
- Sample.Len := 0;
- Sample.Pos := 0;
- SDL_UnlockAudio;
- SDL_CloseAudio;
- SDL_Quit;
- end;
- { Uncomment the define below to use SDL 2.0 }
- {.$define use_sdl_2}
- {$ifdef use_sdl_2}
- {$linklib SDL2}
- {$else}
- {$linklib SDL}
- {$endif}
- begin
- if OpenSounds then
- begin
- PlaySound('hello.wav');
- CloseSounds;
- end;
- WriteLn('Done');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement