Audio

Introduction

The supplied examples Sound and SoundCapture show that it is not difficult to use audio in PasSFML. We make slight alterations to the code and show some of the text output. See the code of the original C++ versions of Sound and SoundCapture and the supplied Pascal versions of Sound and SoundCapture on GitHub.

Code and Output of Sound

program Sound;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  SfmlAudio in '..\..\Source\SfmlAudio.pas', SfmlSystem in '..\..\Source\SfmlSystem.pas';

procedure PlaySound;
var
  Buffer: TSfmlSoundBuffer;
  Sound: TSfmlSound;
begin
  // Load a sound buffer from a wav file
  Buffer := TSfmlSoundBuffer.Create('../Resources/Canary.wav');

  // Display sound information
  Writeln('Canary.wav:');
  Writeln(' Duration: ', Buffer.Duration.AsSeconds : 4 : 2, ' seconds');
  Writeln(' ', Buffer.SampleRate, ' samples / sec');
  Writeln(' Channels: ', Buffer.ChannelCount);

  // Create a sound instance and play it
  Sound := TSfmlSound.Create(Buffer);
  Sound.Play;

  // Loop while the sound is playing
  while Sound.Status = sfPlaying do
    begin
      // Leave some CPU time for other processes
      SfmlSleep(SfmlMilliseconds(100));

      // Display the playing position
      WriteLn('Playing... ', Sound.PlayingOffset.AsSeconds : 4 : 2, ' sec');
    end;
  WriteLn;

  Sound.Free;
end;

procedure PlayMusic;
var
  Music: TSfmlMusic;
begin
  // Load an ogg music file
  Music := TSfmlMusic.Create('../Resources/Orchestral.ogg');

  // Display music information
  Writeln('Orchestral.ogg:');
  Writeln(' Duration: ', Music.Duration.AsSeconds : 4 : 2, ' seconds');
  Writeln(' ', Music.SampleRate, ' samples / sec');
  Writeln(' Channels: ', Music.ChannelCount);

  // Play it
  Music.Play;

  // Loop while the music is playing
  while Music.Status = sfPlaying do
    begin
      // Leave some CPU time for other processes
      SfmlSleep(SfmlMilliseconds(100));

      // Display the playing position
      WriteLn('Playing... ', Music.PlayingOffset.AsSeconds : 4 : 2, ' sec');
    end;
  Writeln;

  Music.Free;
end;

begin
  // Play a sound
  PlaySound;

  // Play music
  PlayMusic;

  // Wait until the user presses 'enter' key
  WriteLn('Press enter to exit...');
  Readln;;
end.    

Copy of some of the output

Canary.wav:
 Duration: 5.76 seconds
 11025 samples / sec
 Channels: 1
Playing... 0.10 sec
Playing... 0.20 sec
Playing... 0.30 sec


Orchestral.ogg:
 Duration: 18.83 seconds
 22050 samples / sec
 Channels: 2
Playing... 0.08 sec
Playing... 0.18 sec
Playing... 0.28 sec
Playing... 0.38 sec


Press enter to exit...

Code, Input and Output of SoundCapture

program SoundCapture;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  SfmlAudio in '..\..\Source\SfmlAudio.pas', SfmlSystem in '..\..\Source\SfmlSystem.pas';

var
  SampleRate: Cardinal;
  Recorder: TSfmlSoundBufferRecorder;
  Buffer: PSfmlSoundBuffer;
  Choice: AnsiChar;
  FileName: AnsiString;
  Sound: TSfmlSound;

begin
  // Check that the device can capture audio
  if not SfmlSoundRecorderisAvailable then
    begin
      Writeln('Sorry, audio capture is not supported by your system');
      Halt;
    end;

  // Choose the sample rate
  Writeln('Please choose the sample rate for sound capture (44100 is CD quality): ');
  ReadLn(SampleRate);

  // Wait for user input...
  Writeln('Press enter to start recording audio');
  ReadLn;

  // Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer
  Recorder := TSfmlSoundBufferRecorder.Create;

  // Audio capture is done in a separate thread, so we can block the main thread while it is capturing
  Recorder.Start(SampleRate);
  Writeln('Recording... press enter to stop');
  ReadLn;
  Recorder.Stop;

  // Get the buffer containing the captured data
  Buffer := Recorder.GetBuffer;

  // Display captured sound information
  Writeln('Sound information:');
  Writeln(' ', SfmlTimeAsSeconds(SfmlSoundBufferGetDuration(Buffer)) : 4 : 2, ' seconds');
  Writeln(' ', SfmlSoundBufferGetSampleRate(Buffer), ' samples / second');
  Writeln(' Channels: ', SfmlSoundBufferGetChannelCount(Buffer));

  // Choose what to do with the recorded sound data
  Writeln('What do you want to do with captured sound (p = play, s = save) ? ');
  Readln(Choice);

  if Choice = 's' then
    begin
      // Choose the filename
      Writeln('Choose the file to create: ');
      Readln(FileName);

      // Save the buffer
      SfmlSoundBufferSaveToFile(Buffer, PAnsiChar(Filename));
    end
  else
    begin
      // Create a sound instance and play it
      Sound := TSfmlSound.Create;
      Sound.SetBuffer(Buffer);
      Sound.Play;

      // Wait until finished
      while Sound.Status = sfPlaying do
        begin
          // Display the playing position
          Writeln('Playing... ', Sound.PlayingOffset.asSeconds : 4 : 2, ' sec');

          // Leave some CPU time for other threads
          SfmlSleep(SfmlMilliseconds(100));
        end;
    end;

  // Finished!
  Writeln('Done!');

  // Wait until the user presses 'enter' key
  Writeln('Press enter to exit...');
  ReadLn;
end.    

Input and output:

Please choose the sample rate for sound capture (44100 is CD quality):
44100
Press enter to start recording audio

Recording... press enter to stop

Sound information:
 1.09 seconds
 44100 samples / second
 Channels: 1
What do you want to do with captured sound (p = play, s = save) ?
p
Playing... 0.00 sec
Playing... 0.10 sec
Playing... 0.22 sec
Playing... 0.32 sec
Playing... 0.42 sec
Playing... 0.52 sec
Playing... 0.62 sec
Playing... 0.72 sec
Playing... 0.82 sec
Playing... 0.92 sec
Playing... 1.02 sec
Done!
Press enter to exit...
Programming - a skill for life!

How to use the Simple Fast Media Library in Lazarus