Skip to main content
Skip table of contents

Vsdk Audio

The vsdk-audio-portaudio is an audio Player and recorder made with PortAudio for VSDK.

Playing audio

There is two way to play an audio buffer:

  • Sync mode: Play an audio buffer and wait till finish.

    CODE
    #include <vsdk/audio/consumers/PaPlayer.hpp>
    
    ...
    Vsdk::Audio::Consumer::PaPlayer player;
    
    player.play(buffer.data(), buffer.sampleRate(), buffer.channelCount());
    // Or more simply
    player.play(buffer);
    ...
  • Async mode: Play an audio buffer in another thread.
    You can use pause, resume and stop methods to control the player (You can use them only in async mode).

    CODE
    #include <vsdk/audio/consumers/PaPlayer.hpp>
    
    ...
    Vsdk::Audio::Consumer::PaPlayer player;
    
    player.start(buffer.data(), buffer.sampleRate(), buffer.channelCount());
    // Or more simply
    player.start(buffer);
    
    ...
    player.pause(buffer); // Pause the played audio
    
    ...
    player.resume(buffer); // Continue playing the audio
    
    ...
    player.stop(buffer); // Stop the played audio
    
    ...
    player.start(buffer); 
    player.wait(); // Wait until buffer finish playing

When using a pointer to play the audio buffer in async mode, the data must exist during the playing life cycle and you have to delete the buffer by your self after stopping the player.

Changing the volume

You can use setVolume to change the player volume the value must be a double between 0.0 and 1.0.

CODE
#include <vsdk/audio/consumers/PaPlayer.hpp>

...
Vsdk::Audio::Consumer::PaPlayer player;

...
player.setVolume(0.5); // set the volume to 50%

Changing the audio output device

You can use setDevice to change the audio output device name. When creating the player, it use the default output device. You need to choose a name from the available devices (printAvailableDevices) or default to use default device. You can use deviceName() to get current device name.

CODE
#include <vsdk/audio/consumers/PaPlayer.hpp>

...
Vsdk::Audio::Consumer::PaPlayer player;

...
player.printAvailableDevices();
player.setDevice("myDeviceName");

Player must be stopped before changing the device name.

Changing the audio position

There is two ways to change the player position:

  • Using the sample position

  • Using the time position

CODE
#include <vsdk/audio/consumers/PaPlayer.hpp>

...
Vsdk::Audio::Consumer::PaPlayer player;

...
player.seek(30000);                     // Seek to sample position 30,000
player.seek(3500ms);                    // Seek to time position 3.5 seconds
player.seek(player.position() + 30000); // Advance the player of 30,000 samples
player.seek(player.time() + 3500ms);    // Advance the player of 3.5 seconds

Wait until player finish playing

There is two ways to wait the player until the audio finish playing:

  • By calling the wait function.

CPP
/// Wait until audio finish playing
/// @param timeout The time limit, to wait for audio finish playing. If this parameter is
///                omitted, a value of 0 is used, meaning wait until audio finish playing.
bool wait(std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
  • By changing the finish callback.

CPP
/// Set the @p callback to be called when finishes playing audio
/// @note It's not possible to use PaPlayer methods in this callback
void setFinishedCallback(Callback callback);

It’s not possible to control the player in the finish callback.

Check the player state

If you want to check if the player is playing you can use player.isPlaying().

To get the player state you can use player.state().

State

Description

Stopped

The audio stream is closed.

Idle

The audio stream is open and waiting for audio.

Playing

The output device is playing the audio.

Paused

The audio stream is open and waiting to resume.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.