Core engine (wauvio/core)

io.hpp — WAV export and playback

Namespace wauvio.

See it in action: Your first tone

WAV export

save_wav(buf, path, sr) writes 16-bit PCM mono; save_wav_stereo(buf, path, sr) writes interleaved stereo. Both throw std::runtime_error if the file cannot be opened. Samples are clamped to [-1, 1] on conversion.

Playback

  • play(buf, sr) — blocking; mono or stereo.
  • play_async(buf, sr) → PlaybackHandle — returns immediately; audio runs on a worker thread. Discarding the handle does not stop playback.
  • stop_all_playback() stops every active stream.

PlaybackHandle

Copyable, thread-safe: stop(), pause(), resume(), is_playing(), set_volume(v) (0–2), position_seconds(), duration_seconds(), seek(s) (best-effort between chunks), wait(), and explicit operator bool.

Platform back ends

PlatformMechanism
WindowsWaveOut with two alternating buffers of 4096 frames (link winmm.lib).
LinuxPCM (s16le) is streamed in 4096-frame chunks through popen to a shell pipeline that tries ffmpeg … -f alsa default, then ffmpeg … -f pulse default, then ffplay. FFmpeg must therefore be installed for sound. WAV export has no such requirement.

Internals (wauvio::detail::PlaybackInstance and PlaybackRuntime) are an implementation detail: a singleton registry holds weak references to running streams.

Full API listing

wauvio/core/io.hpp

wauvio::Free functions, constants and aliases

//  §28  WAV FILE EXPORT  (mono and stereo)
    /// Write a standard 16-bit PCM WAV header and samples to an open file.
    /// `channels` must be 1 (mono) or 2 (stereo).
    inline void write_wav_impl(FILE* f, const std::vector<int16_t>& pcm,
                                int sample_rate, int channels)

/// Save a mono buffer as a 16-bit PCM WAV file.
inline void save_wav(const Buffer& buf, const std::string& path, int sample_rate = 0)
/// Save a stereo buffer as an interleaved L/R 16-bit PCM WAV file.
inline void save_wav_stereo(const StereoBuffer& buf, const std::string& path,
                             int sample_rate = 0)

wauvio::detail::class PlaybackInstance

   //  §29  PLAYBACK  v1.3.3
       //  Internal state of one playback stream
       class PlaybackInstance
       public:
           // Construct with a pre-converted PCM buffer + metadata.
           // The PCM data is moved in - this instance owns it exclusively.
           PlaybackInstance(std::vector<int16_t> pcm, int sample_rate, int channels)
               : pcm_(std::move(pcm))
               , sample_rate_(sample_rate)
               , channels_(channels)
               , stopped_(false)
               , paused_(false)
               , playing_(true)
               , volume_(1.0f)
{}
           // Non-copyable, non-movable (shared_ptr is the indirection layer).
           PlaybackInstance(const PlaybackInstance&)            = delete;
           PlaybackInstance& operator=(const PlaybackInstance&) = delete;
           void stop()
           void pause()
           void resume()
           bool is_playing() const noexcept
           void set_volume(float v) noexcept
           float volume() const noexcept
           /// Returns the current playback position in seconds.
           double position_seconds() const noexcept
           /// Total duration of the buffer in seconds.
           double duration_seconds() const noexcept
           /// Seek to `seconds` into the buffer.  Clamped to [0, duration].
           void seek(double seconds) noexcept
           // Called once from the worker thread spawned by PlaybackRuntime.
           void run()

wauvio::detail::class PlaybackRuntime

//  PlaybackRuntime - singleton registry of active instances
class PlaybackRuntime
public:
    static PlaybackRuntime& instance()
    // Launch a new PlaybackInstance on its own thread.
    // Returns a shared_ptr so the caller can build a PlaybackHandle.
    std::shared_ptr<PlaybackInstance> launch(
            std::vector<int16_t> pcm, int sample_rate, int channels)

    // Request all active streams to stop (useful for clean shutdown).
    void stop_all()
    ~PlaybackRuntime() { stop_all(); }

wauvio::class PlaybackHandle

//  PUBLIC API - PlaybackHandle
/// Lightweight, copyable handle to an in-flight playback stream.
/// All methods are thread-safe.
/// Discarding the handle does NOT stop playback (fire-and-forget).
class PlaybackHandle
public:
    PlaybackHandle() = default;
    explicit PlaybackHandle(std::shared_ptr<detail::PlaybackInstance> inst)
        : inst_(std::move(inst)) {}
    /// Stop playback immediately and release resources.
    void stop()   { if (inst_) inst_->stop();   }
    /// Pause playback (resume() to continue).
    void pause()  { if (inst_) inst_->pause();  }
    /// Resume after pause().
    void resume() { if (inst_) inst_->resume(); }
    /// Returns true while the worker thread is still active.
    bool is_playing() const noexcept
    /// Set output volume multiplier [0.0, 2.0].  Thread-safe.
    void set_volume(float v) { if (inst_) inst_->set_volume(v); }
    /// Current playback position in seconds.
    double position_seconds() const noexcept
    /// Total buffer duration in seconds.
    double duration_seconds() const noexcept
    /// Seek to `seconds`.  Best-effort: the worker honours it between chunks.
    void seek(double seconds) { if (inst_) inst_->seek(seconds); }
    /// Block the calling thread until playback finishes (or is stopped).
    void wait() const
    /// True if this handle refers to a real stream.
    explicit operator bool() const noexcept { return inst_ != nullptr; }
//  play_async  -  non-blocking launch
/// Launch mono buffer playback asynchronously.
/// Returns immediately; audio runs on a background thread.
inline PlaybackHandle play_async(const Buffer& buf, int sample_rate = 0)
/// Launch stereo buffer playback asynchronously.
/// Returns immediately; audio runs on a background thread.
inline PlaybackHandle play_async(const StereoBuffer& buf, int sample_rate = 0)
//  play  -  blocking (backward-compatible)
/// Blocking playback of a mono buffer.
/// Internally delegates to play_async() and waits for completion.
inline void play(const Buffer& buf, int sample_rate = 0)
/// Blocking playback of a stereo buffer.
/// Internally delegates to play_async() and waits for completion.
inline void play(const StereoBuffer& buf, int sample_rate = 0)
/// Stop every active playback stream immediately.
inline void stop_all_playback()