Hellow everyone,
Ik ben aan het proberen om een loopback player te maken (audio dat speelt op een bepaald audio endpoint opnieuw afspelen op een ander endpoint). Bij wijze van oefening wil ik hervoor de nieuwe vista audio api's gebruiken, en mijn oog is gevallen op wasapi.
Echter, het werkt niet. Het spelen op een endpoint apart, werkt. Het recorden van een endpoint in loopback mode, werkt. Echter, wanneer ik de 2 stukken tesamen breng, en ze dezelfde buffer laat gebruiken, loopt het mis.
De capture buffer, lijkt zelfs steeds leeg te zijn, waardoor ik vermoed dat het endpoint zelfs niet in loopback geactiveerd te zijn. Zeer raar...
Misschien dat eimand die al wat ervaring heeft met wasapi 5 minuutjes de tijd heeft om de code even te doorlopen? Mijn excuses, het is nog dirty code, ik ben nog maar aan het uitzoeken of het uberhaubt kan...
Ik ben aan het proberen om een loopback player te maken (audio dat speelt op een bepaald audio endpoint opnieuw afspelen op een ander endpoint). Bij wijze van oefening wil ik hervoor de nieuwe vista audio api's gebruiken, en mijn oog is gevallen op wasapi.
Echter, het werkt niet. Het spelen op een endpoint apart, werkt. Het recorden van een endpoint in loopback mode, werkt. Echter, wanneer ik de 2 stukken tesamen breng, en ze dezelfde buffer laat gebruiken, loopt het mis.
De capture buffer, lijkt zelfs steeds leeg te zijn, waardoor ik vermoed dat het endpoint zelfs niet in loopback geactiveerd te zijn. Zeer raar...
Misschien dat eimand die al wat ervaring heeft met wasapi 5 minuutjes de tijd heeft om de code even te doorlopen? Mijn excuses, het is nog dirty code, ik ben nog maar aan het uitzoeken of het uberhaubt kan...
code:
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
195
| HRESULT hr; //Variables for getting from and to endpoints IMMDevice *pEndpointFrom = NULL; IMMDevice *pEndpointTo = NULL; IMMDeviceEnumerator *pEnumerator = NULL; IMMDeviceCollection *pCollection = NULL; //Variables for capturing REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC; REFERENCE_TIME hnsActualDuration; IAudioClient *pAudioClientFrom = NULL; IAudioClient *pAudioClientTo = NULL; IAudioRenderClient *pRenderClient = NULL; IAudioCaptureClient *pCaptureClient = NULL; WAVEFORMATEX *pwfx = NULL; UINT32 bufferFrameCountFrom; UINT32 bufferFrameCountTo; UINT32 numFramesAvailable; UINT32 numFramesPadding; BYTE *pData; UINT32 packetLength = 0; DWORD flags = 0; HANDLE hEvent = NULL; HANDLE hTask = NULL; //Get COM stuff figured out hr = CoCreateInstance( CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator); EXIT_ON_ERROR(hr) //Get all audio endpoints hr = pEnumerator->EnumAudioEndpoints( eRender, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &pCollection); EXIT_ON_ERROR(hr) UINT count; hr = pCollection->GetCount(&count); EXIT_ON_ERROR(hr) if (count == 0){ throw gcnew System::Exception("No endpoints detected"); } //Select FROM and TO endpoints hr = pCollection->Item(from, &pEndpointFrom); hr = pCollection->Item(to, &pEndpointTo); EXIT_ON_ERROR(hr) // Activate FROM endpoint hr = pEndpointFrom->Activate( IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pAudioClientFrom); EXIT_ON_ERROR(hr) //Activate TO endpoint hr = pEndpointTo->Activate( IID_IAudioClient2, CLSCTX_ALL, NULL, (void**)&pAudioClientTo); EXIT_ON_ERROR(hr) //Get mix format from either from or to device hr = pAudioClientFrom->GetMixFormat(&pwfx); EXIT_ON_ERROR(hr) //Activate FROM audioclient hr = pAudioClientFrom->Initialize( AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_LOOPBACK, hnsRequestedDuration, 0, pwfx, NULL); EXIT_ON_ERROR(hr) // Activate TO audioclient hr = pAudioClientTo->Initialize( AUDCLNT_SHAREMODE_SHARED, 0, hnsRequestedDuration, 0, pwfx, NULL); EXIT_ON_ERROR(hr) // Create an event handle and register it for buffer-event notifications. hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (hEvent == NULL){ hr = E_FAIL; goto Exit; } hr = pAudioClientFrom->SetEventHandle(hEvent); EXIT_ON_ERROR(hr); // Get the actual size of the two allocated buffers. hr = pAudioClientFrom->GetBufferSize(&bufferFrameCountFrom); EXIT_ON_ERROR(hr) hr = pAudioClientFrom->GetService( IID_IAudioCaptureClient, (void**)&pCaptureClient); EXIT_ON_ERROR(hr) // Get the size of the allocated buffer. hr = pAudioClientTo->GetBufferSize(&bufferFrameCountTo); EXIT_ON_ERROR(hr) hr = pAudioClientTo->GetService( IID_IAudioRenderClient, (void**)&pRenderClient); EXIT_ON_ERROR(hr) // Load the first buffer with data from the audio source before starting the stream. hr = pRenderClient->GetBuffer(bufferFrameCountTo, &pData); EXIT_ON_ERROR(hr) //Check if there is any data in the capture buffer hr = pCaptureClient->GetNextPacketSize(&packetLength); EXIT_ON_ERROR(hr) while (packetLength != 0) { // Get the available data in the shared buffer. hr = pCaptureClient->GetBuffer( &pData, &numFramesAvailable, &flags, NULL, NULL); EXIT_ON_ERROR(hr) hr = pCaptureClient->ReleaseBuffer(numFramesAvailable); EXIT_ON_ERROR(hr) hr = pCaptureClient->GetNextPacketSize(&packetLength); EXIT_ON_ERROR(hr) } hr = pRenderClient->ReleaseBuffer(bufferFrameCountTo, flags); EXIT_ON_ERROR(hr) hr = pAudioClientFrom->Start(); // Start playing. hr = pAudioClientTo->Start(); // Start capturing. EXIT_ON_ERROR(hr) bool test = true; // Each loop fills one of the two buffers. while (test) { // Wait for next buffer event to be signaled. DWORD retval = WaitForSingleObject(hEvent, INFINITE); if (retval != WAIT_OBJECT_0) { // Event handle timed out after a 2-second wait. pAudioClientFrom->Stop(); hr = ERROR_TIMEOUT; goto Exit; } // Grab the next empty buffer from the audio device. hr = pRenderClient->GetBuffer(bufferFrameCountTo, &pData); EXIT_ON_ERROR(hr) //Check if there is any data in the capture buffer hr = pCaptureClient->GetNextPacketSize(&packetLength); EXIT_ON_ERROR(hr) while (packetLength != 0) { // Get the available data in the shared buffer. hr = pCaptureClient->GetBuffer( &pData, &numFramesAvailable, &flags, NULL, NULL); EXIT_ON_ERROR(hr) hr = pCaptureClient->ReleaseBuffer(numFramesAvailable); EXIT_ON_ERROR(hr) hr = pCaptureClient->GetNextPacketSize(&packetLength); EXIT_ON_ERROR(hr) } hr = pRenderClient->ReleaseBuffer(bufferFrameCountTo, flags); EXIT_ON_ERROR(hr) } |