File: directsound.cpp

package info (click to toggle)
ares 134%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,680 kB
  • sloc: cpp: 338,717; ansic: 89,036; sh: 52; makefile: 27
file content (160 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download | duplicates (2)
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
#include <dsound.h>

struct AudioDirectSound : AudioDriver {
  AudioDirectSound& self = *this;
  AudioDirectSound(Audio& super) : AudioDriver(super) {}
  ~AudioDirectSound() { terminate(); }

  auto create() -> bool override {
    super.setChannels(2);
    super.setFrequency(48000);
    super.setLatency(40);
    return initialize();
  }

  auto driver() -> string override { return "DirectSound 7.0"; }
  auto ready() -> bool override { return _ready; }

  auto hasBlocking() -> bool override { return true; }

  auto hasFrequencies() -> vector<u32> override {
    return {44100, 48000, 96000};
  }

  auto hasLatencies() -> vector<u32> override {
    return {40, 60, 80, 100};
  }

  auto setBlocking(bool blocking) -> bool override { return true; }
  auto setFrequency(u32 frequency) -> bool override { return initialize(); }
  auto setLatency(u32 latency) -> bool override { return initialize(); }

  auto clear() -> void override {
    if(!ready()) return;

    _ringRead = 0;
    _ringWrite = _rings - 1;
    _ringDistance = _rings - 1;

    if(_buffer) memory::fill<u32>(_buffer, _period * _rings);
    _offset = 0;

    if(!_secondary) return;
    _secondary->Stop();
    _secondary->SetCurrentPosition(0);

    void* output;
    DWORD size;
    _secondary->Lock(0, _period * _rings * 4, &output, &size, 0, 0, 0);
    memory::fill<u8>(output, size);
    _secondary->Unlock(output, size, 0, 0);

    _secondary->Play(0, 0, DSBPLAY_LOOPING);
  }

  auto output(const f64 samples[]) -> void override {
    if(!ready()) return;

    _buffer[_offset]  = (u16)sclamp<16>(samples[0] * 32767.0) <<  0;
    _buffer[_offset] |= (u16)sclamp<16>(samples[1] * 32767.0) << 16;
    if(++_offset < _period) return;
    _offset = 0;

    if(self.blocking) {
      //wait until playback buffer has an empty ring to write new audio data to
      while(_ringDistance >= _rings - 1) {
        DWORD position;
        _secondary->GetCurrentPosition(&position, 0);
        u32 ringActive = position / (_period * 4);
        if(ringActive == _ringRead) continue;

        //subtract number of played rings from ring distance counter
        _ringDistance -= (_rings + ringActive - _ringRead) % _rings;
        _ringRead = ringActive;

        if(_ringDistance < 2) {
          //buffer underflow; set max distance to recover quickly
          _ringDistance = _rings - 1;
          _ringWrite = (_rings + _ringRead - 1) % _rings;
          break;
        }
      }
    }

    _ringWrite = (_ringWrite + 1) % _rings;
    _ringDistance = (_ringDistance + 1) % _rings;

    void* output;
    DWORD size;
    if(_secondary->Lock(_ringWrite * _period * 4, _period * 4, &output, &size, 0, 0, 0) == DS_OK) {
      memory::copy<u32>(output, _buffer, _period);
      _secondary->Unlock(output, size, 0, 0);
    }
  }

private:
  auto initialize() -> bool {
    terminate();

    _rings = 8;
    _period = self.frequency * self.latency / _rings / 1000.0 + 0.5;
    _buffer = new u32[_period * _rings];
    _offset = 0;

    if(DirectSoundCreate(0, &_interface, 0) != DS_OK) return terminate(), false;
    _interface->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY);

    DSBUFFERDESC primaryDescription = {};
    primaryDescription.dwSize = sizeof(DSBUFFERDESC);
    primaryDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
    primaryDescription.dwBufferBytes = 0;
    primaryDescription.lpwfxFormat = 0;
    _interface->CreateSoundBuffer(&primaryDescription, &_primary, 0);

    WAVEFORMATEX waveFormat = {};
    waveFormat.wFormatTag = WAVE_FORMAT_PCM;
    waveFormat.nChannels = self.channels;
    waveFormat.nSamplesPerSec = self.frequency;
    waveFormat.wBitsPerSample = 16;
    waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
    waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
    _primary->SetFormat(&waveFormat);

    DSBUFFERDESC secondaryDescription = {};
    secondaryDescription.dwSize = sizeof(DSBUFFERDESC);
    secondaryDescription.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS | DSBCAPS_LOCSOFTWARE;
    secondaryDescription.dwBufferBytes = _period * _rings * 4;
    secondaryDescription.guid3DAlgorithm = GUID_NULL;
    secondaryDescription.lpwfxFormat = &waveFormat;
    _interface->CreateSoundBuffer(&secondaryDescription, &_secondary, 0);
    _secondary->SetFrequency(self.frequency);
    _secondary->SetCurrentPosition(0);

    _ready = true;
    clear();
    return true;
  }

  auto terminate() -> void {
    _ready = false;
    if(_buffer) { delete[] _buffer; _buffer = nullptr; }
    if(_secondary) { _secondary->Stop(); _secondary->Release(); _secondary = nullptr; }
    if(_primary) { _primary->Stop(); _primary->Release(); _primary = nullptr; }
    if(_interface) { _interface->Release(); _interface = nullptr; }
  }

  bool _ready = false;

  LPDIRECTSOUND _interface = nullptr;
  LPDIRECTSOUNDBUFFER _primary = nullptr;
  LPDIRECTSOUNDBUFFER _secondary = nullptr;

  u32* _buffer = nullptr;
  u32 _offset = 0;

  u32 _period = 0;
  u32 _rings = 0;
  u32 _ringRead = 0;
  u32 _ringWrite = 0;
  s32 _ringDistance = 0;
};