File: wasapi.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 (169 lines) | stat: -rw-r--r-- 5,281 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
#include <avrt.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <audiopolicy.h>
#include <devicetopology.h>
#include <endpointvolume.h>

#include <nall/dsp.hpp>

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

  struct {
    bool exclusive = false;
    bool synchronize = false;
    uint frequency = 44100;
  } settings;

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

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

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

    if(name == Audio::Synchronize && value.is<bool>()) {
      settings.synchronize = value.get<bool>();
      return true;
    }

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

    return false;
  }

  auto sample(uint16 left, uint16 right) -> void {
    int samples[] = {(int16)left, (int16)right};
    dsp.sample(samples);
    while(dsp.pending()) {
      dsp.read(samples);
      write(samples);
    }
  }

  auto clear() -> void {
    audioClient->Stop();
    renderClient->GetBuffer(bufferFrameCount, &bufferData);

    renderClient->ReleaseBuffer(bufferFrameCount, 0);
    audioClient->Start();
  }

  auto init() -> bool {
    if(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator) != S_OK) return false;
    if(enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device) != S_OK) return false;
    if(device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void**)&audioClient) != S_OK) return false;

    if(settings.exclusive) {
      if(device->OpenPropertyStore(STGM_READ, &propertyStore) != S_OK) return false;
      if(propertyStore->GetValue(PKEY_AudioEngine_DeviceFormat, &propVariant) != S_OK) return false;
      waveFormat = (WAVEFORMATEX*)propVariant.blob.pBlobData;
      if(audioClient->GetDevicePeriod(nullptr, &devicePeriod) != S_OK) return false;
      if(audioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, 0, devicePeriod, devicePeriod, waveFormat, nullptr) != S_OK) return false;
      taskHandle = AvSetMmThreadCharacteristics(L"Pro Audio", &taskIndex);
    } else {
      if(audioClient->GetMixFormat(&waveFormat) != S_OK) return false;
      if(audioClient->GetDevicePeriod(&devicePeriod, nullptr)) return false;
      if(audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, devicePeriod, 0, waveFormat, nullptr) != S_OK) return false;
    }

    if(audioClient->GetService(IID_IAudioRenderClient, (void**)&renderClient) != S_OK) return false;
    if(audioClient->GetBufferSize(&bufferFrameCount) != S_OK) return false;

    switch(((WAVEFORMATEXTENSIBLE*)waveFormat)->SubFormat.Data1) {
    case 1: ieee = false; break;  //fixed point
    case 3: ieee = true; break;   //floating point
    default: return false;        //unknown format; abort
    }

    dsp.setChannels(2);
    dsp.setPrecision(16);
    dsp.setFrequency(settings.frequency);

    dsp.setResampler(DSP::ResampleEngine::Linear);
    dsp.setResamplerFrequency(waveFormat->nSamplesPerSec);
    dsp.setChannels(waveFormat->nChannels);
    dsp.setPrecision(waveFormat->wBitsPerSample);

    print("[WASAPI]\n");
    print("Channels:  ", waveFormat->nChannels, "\n");
    print("Precision: ", waveFormat->wBitsPerSample, "\n");
    print("Frequency: ", waveFormat->nSamplesPerSec, "\n");
    print("IEEE-754:  ", ieee, "\n");
    print("Exclusive: ", settings.exclusive, "\n\n");

    audioClient->Start();
    return true;
  }

  auto term() -> void {
    if(audioClient) {
      audioClient->Stop();
    }

    if(taskHandle) {
      AvRevertMmThreadCharacteristics(taskHandle);
      taskHandle = nullptr;
    }
  }

private:
  auto write(int samples[]) -> void {
    while(true) {
      uint32 padding = 0;
      audioClient->GetCurrentPadding(&padding);
      if(bufferFrameCount - padding < 1) {
        if(!settings.synchronize) return;
        continue;
      }
      break;
    }

    renderClient->GetBuffer(1, &bufferData);

    if(ieee) {
      auto buffer = (float*)bufferData;
      buffer[0] = (int16)samples[0] / 32768.0;
      buffer[1] = (int16)samples[1] / 32768.0;
    } else {
      auto buffer = (int16*)bufferData;
      buffer[0] = (int16)samples[0];
      buffer[1] = (int16)samples[1];
    }

    renderClient->ReleaseBuffer(1, 0);
  }

  DSP dsp;
  IMMDeviceEnumerator* enumerator = nullptr;
  IMMDevice* device = nullptr;
  IPropertyStore* propertyStore = nullptr;
  IAudioClient* audioClient = nullptr;
  IAudioRenderClient* renderClient = nullptr;
  WAVEFORMATEX* waveFormat = nullptr;
  PROPVARIANT propVariant;
  HANDLE taskHandle = nullptr;
  DWORD taskIndex = 0;
  REFERENCE_TIME devicePeriod = 0;
  uint32 bufferFrameCount = 0;
  uint8* bufferData = nullptr;
  bool ieee = false;
};