summaryrefslogtreecommitdiff
path: root/src/audio/backend/miniaudio_backend.h
blob: 435496c896633d2fbae4737b6cee0e69870a06c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// This file is part of the 64k demo project.
// It implements the production audio backend using miniaudio.
// This is the default backend for the final build.

#pragma once

#include "../audio_backend.h"
#include "miniaudio.h"

// Production audio backend using miniaudio library
// Manages real hardware audio device and playback
class MiniaudioBackend : public AudioBackend {
 public:
  MiniaudioBackend();
  ~MiniaudioBackend() override;

  void init() override;
  void start() override;
  void shutdown() override;
  float get_realtime_peak() override;
  void get_callback_state(double* out_time, int64_t* out_samples) override;

  // Get the underlying miniaudio device (for internal use)
  ma_device* get_device() {
    return &device_;
  }

 private:
  ma_device device_;
  bool initialized_;

  // Real-time peak measured at actual playback time (not pre-buffer)
  // Updated in audio_callback when samples are read from ring buffer
  static volatile float realtime_peak_;

  // Smooth playback time interpolation (updated in callback)
  static volatile double last_callback_time_;    // Absolute CLOCK_MONOTONIC time
  static volatile int64_t last_callback_samples_;

  // Static callback required by miniaudio C API
  static void audio_callback(ma_device* pDevice, void* pOutput,
                             const void* pInput, ma_uint32 frameCount);
};