File: openal.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 (194 lines) | stat: -rw-r--r-- 5,207 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#if defined(PLATFORM_MACOSX)
  #include <OpenAL/al.h>
  #include <OpenAL/alc.h>
#else
  #include <AL/al.h>
  #include <AL/alc.h>
#endif

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

  struct {
    ALCdevice* handle = nullptr;
    ALCcontext* context = nullptr;
    ALuint source = 0;
    ALenum format = AL_FORMAT_STEREO16;
    unsigned latency = 0;
    unsigned queueLength = 0;
  } device;

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

  struct {
    bool synchronize = true;
    unsigned frequency = 22050;
    unsigned latency = 40;
  } settings;

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

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

  auto set(const string& name, const any& value) -> bool {
    if(name == Audio::Synchronize && value.is<bool>()) {
      settings.synchronize = value.get<bool>();
      return true;
    }

    if(name == Audio::Frequency && value.is<unsigned>()) {
      settings.frequency = value.get<unsigned>();
      return true;
    }

    if(name == Audio::Latency && value.is<unsigned>()) {
      if(settings.latency != value.get<unsigned>()) {
        settings.latency = value.get<unsigned>();
        updateLatency();
      }
      return true;
    }

    return false;
  }

  auto sample(uint16_t left, uint16_t right) -> void {
    buffer.data[buffer.length++] = left << 0 | right << 16;
    if(buffer.length < buffer.size) return;

    ALuint albuffer = 0;
    int processed = 0;
    while(true) {
      alGetSourcei(device.source, AL_BUFFERS_PROCESSED, &processed);
      while(processed--) {
        alSourceUnqueueBuffers(device.source, 1, &albuffer);
        alDeleteBuffers(1, &albuffer);
        device.queueLength--;
      }
      //wait for buffer playback to catch up to sample generation if not synchronizing
      if(settings.synchronize == false || device.queueLength < 3) break;
    }

    if(device.queueLength < 3) {
      alGenBuffers(1, &albuffer);
      alBufferData(albuffer, device.format, buffer.data, buffer.size * 4, settings.frequency);
      alSourceQueueBuffers(device.source, 1, &albuffer);
      device.queueLength++;
    }

    ALint playing;
    alGetSourcei(device.source, AL_SOURCE_STATE, &playing);
    if(playing != AL_PLAYING) alSourcePlay(device.source);
    buffer.length = 0;
  }

  auto clear() -> void {
  }

  auto init() -> bool {
    updateLatency();
    device.queueLength = 0;

    bool success = false;
    if(device.handle = alcOpenDevice(nullptr)) {
      if(device.context = alcCreateContext(device.handle, nullptr)) {
        alcMakeContextCurrent(device.context);
        alGenSources(1, &device.source);

      //alSourcef (device.source, AL_PITCH, 1.0);
      //alSourcef (device.source, AL_GAIN, 1.0);
      //alSource3f(device.source, AL_POSITION, 0.0, 0.0, 0.0);
      //alSource3f(device.source, AL_VELOCITY, 0.0, 0.0, 0.0);
      //alSource3f(device.source, AL_DIRECTION, 0.0, 0.0, 0.0);
      //alSourcef (device.source, AL_ROLLOFF_FACTOR, 0.0);
      //alSourcei (device.source, AL_SOURCE_RELATIVE, AL_TRUE);

        alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
        alListener3f(AL_VELOCITY, 0.0, 0.0, 0.0);
        ALfloat listener_orientation[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
        alListenerfv(AL_ORIENTATION, listener_orientation);

        success = true;
      }
    }

    if(success == false) {
      term();
      return false;
    }

    return true;
  }

  auto term() -> void {
    if(alIsSource(device.source) == AL_TRUE) {
      int playing = 0;
      alGetSourcei(device.source, AL_SOURCE_STATE, &playing);
      if(playing == AL_PLAYING) {
        alSourceStop(device.source);
        int queued = 0;
        alGetSourcei(device.source, AL_BUFFERS_QUEUED, &queued);
        while(queued--) {
          ALuint albuffer = 0;
          alSourceUnqueueBuffers(device.source, 1, &albuffer);
          alDeleteBuffers(1, &albuffer);
          device.queueLength--;
        }
      }

      alDeleteSources(1, &device.source);
      device.source = 0;
    }

    if(device.context) {
      alcMakeContextCurrent(nullptr);
      alcDestroyContext(device.context);
      device.context = 0;
    }

    if(device.handle) {
      alcCloseDevice(device.handle);
      device.handle = 0;
    }

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

private:
  auto queryDevices() -> lstring {
    lstring result;

    const char* buffer = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
    if(!buffer) return result;

    while(buffer[0] || buffer[1]) {
      result.append(buffer);
      while(buffer[0]) buffer++;
    }

    return result;
  }

  auto updateLatency() -> void {
    if(buffer.data) delete[] buffer.data;
    buffer.size = settings.frequency * settings.latency / 1000.0 + 0.5;
    buffer.data = new uint32_t[buffer.size]();
  }
};