File: pulseaudiosimple.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (95 lines) | stat: -rw-r--r-- 2,372 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <pulse/simple.h>
#include <pulse/error.h>

struct AudioPulseAudioSimple : Audio {
  ~AudioPulseAudioSimple() { term(); }

  struct {
    pa_simple* handle = nullptr;
    pa_sample_spec spec;
  } device;

  struct {
    uint32_t* data = nullptr;
    unsigned offset = 0;
  } buffer;

  struct {
    unsigned frequency = 22050;
  } settings;

  auto cap(const string& name) -> bool {
    if(name == Audio::Frequency) return true;
    return false;
  }

  auto get(const string& name) -> any {
    if(name == Audio::Frequency) return settings.frequency;
    return {};
  }

  auto set(const string& name, const any& value) -> bool {
    if(name == Audio::Frequency && value.is<unsigned>()) {
      settings.frequency = value.get<unsigned>();
      if(device.handle) init();
      return true;
    }

    return false;
  }

  auto sample(uint16_t left, uint16_t right) -> void {
    if(!device.handle) return;

    buffer.data[buffer.offset++] = left + (right << 16);
    if(buffer.offset >= 64) {
      int error;
      pa_simple_write(device.handle, (const void*)buffer.data, buffer.offset * sizeof(uint32_t), &error);
      buffer.offset = 0;
    }
  }

  auto clear() -> void {
  }

  auto init() -> bool {
    device.spec.format   = PA_SAMPLE_S16LE;
    device.spec.channels = 2;
    device.spec.rate     = settings.frequency;

    int error = 0;
    device.handle = pa_simple_new(
      0,                         //default server
      "ruby::pulseaudiosimple",  //application name
      PA_STREAM_PLAYBACK,        //direction
      0,                         //default device
      "audio",                   //stream description
      &device.spec,              //sample format
      0,                         //default channel map
      0,                         //default buffering attributes
      &error                     //error code
    );
    if(!device.handle) {
      fprintf(stderr, "ruby::pulseaudiosimple failed to initialize - %s\n", pa_strerror(error));
      return false;
    }

    buffer.data = new uint32_t[64];
    buffer.offset = 0;
    return true;
  }

  auto term() -> void {
    if(device.handle) {
      int error;
      pa_simple_flush(device.handle, &error);
      pa_simple_free(device.handle);
      device.handle = nullptr;
    }

    if(buffer.data) {
      delete[] buffer.data;
      buffer.data = nullptr;
    }
  }
};