Recent

Author Topic: Problem with Windows API sound  (Read 1202 times)

Tomi

  • Jr. Member
  • **
  • Posts: 97
Problem with Windows API sound
« on: August 17, 2023, 02:19:04 pm »
Hello everybody!

I made the remake my first Lazarus game: this is the "Space Defender v.2.0". It is a small space shooter game and you can download it from my website:
https://itsmprog.000webhostapp.com/lazjatekok/spacedef.zip
Everything is OK with it, except the sound.
Because I use the normal Windows API to making sounds. The two main problems with it that:
1. only one sound can be heard at a time,
2. I can't change the sound volume.
I use this code to play sound:
Code: Pascal  [Select][+][-]
  1. uses MMSystem;
  2. (...)
  3. sndPlaySound('sounds\a_sound.wav',snd_Async or snd_NoDefault);
and this code for set the sound volume:
Code: Pascal  [Select][+][-]
  1. var sndvolume: byte;
  2. (...)
  3. sndvolume:=25;
  4. waveOutSetVolume(0,sndvolume);
But I can hear nothing with waveOutSetVolume(), therefore I must set to comment this line. In fact, the game doesn't give sound since I used this command - maybe I maked worse something?
So, my question is that these problems can be solved with the simple Windows API or I have to search a better solution, e.g. a particular modul, such as FMOD or similar?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1720
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Problem with Windows API sound
« Reply #1 on: August 17, 2023, 03:09:59 pm »
To play as many times as you want a WAVE file, feel free to use my approach.
The thing with the volume... it is not working on all SoundCards... in that case user must set it manual in their SoundMixer.
Code: Pascal  [Select][+][-]
  1. uses
  2.   MMSystem...
  3.  
  4.  
  5. function PlayWavFile(const fileName: string; const aliasName: string; const volume: Integer = 100): Boolean;
  6. const
  7.   MCI_STRING_SIZE = 255;
  8. var
  9.   command: string;
  10.   buffer: array [0..MCI_STRING_SIZE] of Char;
  11.   maxVolume: Integer;
  12.   function GetMaxVolume(const aliasName: string): Integer;
  13.   begin
  14.     command := 'status ' + aliasName + ' volume';
  15.     if mciSendString(PChar(command), buffer, MCI_STRING_SIZE, 0) <> 0 then
  16.     begin
  17.       Result := -1;
  18.       Exit;
  19.     end;
  20.     Result := StrToIntDef(buffer, -1);
  21.   end;
  22. begin
  23.   Result := False;
  24.  
  25.   command := 'open "' + fileName + '" alias ' + aliasName;
  26.   if mciSendString(PChar(command), buffer, MCI_STRING_SIZE, 0) <> 0 then
  27.     Exit;
  28.  
  29.   maxVolume := GetMaxVolume(aliasName);
  30.  
  31.   if ((maxVolume <> -1) and (volume <= maxVolume)) then
  32.     begin
  33.       command := 'setaudio ' + aliasName + ' volume to ' + IntToStr(volume);
  34.       if mciSendString(PChar(command), buffer, MCI_STRING_SIZE, 0) <> 0 then
  35.         Exit;
  36.     end;
  37.  
  38.   command := 'play ' + aliasName;
  39.   if mciSendString(PChar(command), buffer, MCI_STRING_SIZE, 0) <> 0 then
  40.     Exit;
  41.  
  42.   Result := True;
  43. end;

Example to play 2 sounds at same time:
Code: Pascal  [Select][+][-]
  1.   PlayWavFile('.\Sound_A.WAV', IntToStr(Random(High(Integer))), Random(100));
  2.   PlayWavFile('.\Sound_B.WAV', IntToStr(Random(High(Integer))), Random(100));

Just give each time a random alias name (2nd argument)

I hope it helps,
enjoy!
« Last Edit: August 17, 2023, 03:17:06 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Tomi

  • Jr. Member
  • **
  • Posts: 97
Re: Problem with Windows API sound
« Reply #2 on: August 18, 2023, 10:30:48 am »
Thank you very much, KodeZwerg!
I will try your code.
Although I don't know MCI commands, but maybe these will be the appropriate solution.

cpicanco

  • Hero Member
  • *****
  • Posts: 541
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Problem with Windows API sound
« Reply #3 on: September 23, 2023, 04:16:54 pm »
For multiple sounds at once (and some other stuff), there is a SDL2 example here:

https://github.com/cpicanco/stimulus-control-sdl2/blob/hanna/src/sdl.app.audio.pas

 

TinyPortal © 2005-2018