-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
289 lines (252 loc) · 8.02 KB
/
audio.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
//#include <dplay.h>
#include <dsound.h>
#include <stdio.h>
#include "defines.h"
#include "Vcc.h"
#include "config.h"
#include "coco3.h"
#include "audio.h"
#include "logger.h"
#define MAXCARDS 12
//PlayBack
static LPDIRECTSOUND lpds; // directsound interface pointer
static DSBUFFERDESC dsbd; // directsound description
static DSCAPS dscaps; // directsound caps
static DSBCAPS dsbcaps; // directsound buffer caps
//Record
static LPDIRECTSOUNDCAPTURE8 lpdsin;
static DSCBUFFERDESC dsbdin; // directsound description
HRESULT hr;
static LPDIRECTSOUNDBUFFER lpdsbuffer1=NULL; //the sound buffers
static LPDIRECTSOUNDCAPTUREBUFFER lpdsbuffer2=NULL; //the sound buffers for capture
static WAVEFORMATEX pcmwf; //generic waveformat structure
static void *SndPointer1 = NULL,*SndPointer2 = NULL;
static unsigned short BitRate=0;
static DWORD SndLenth1= 0,SndLenth2= 0,SndBuffLenth = 0;
static unsigned char InitPassed=0;
static unsigned short BlockSize=0;
static unsigned short AuxBuffer[6][44100/60]; //Biggest block size possible
static DWORD WritePointer=0;
static DWORD BuffOffset=0;
static char AuxBufferPointer=0;
static int CardCount=0;
static unsigned short CurrentRate=0;
static unsigned char AudioPause=0;
static SndCardList *Cards=NULL;
BOOL CALLBACK DSEnumCallback(LPGUID,LPCSTR,LPCSTR,LPVOID);
int SoundInit (HWND main_window_handle,_GUID * Guid,unsigned short Rate)
{
Rate=(Rate & 3);
if (Rate != 0) //Force 44100 or Mute
Rate = 3;
CurrentRate=Rate;
if (InitPassed)
{
InitPassed=0;
lpdsbuffer1->Stop();
if (lpdsbuffer1 !=NULL)
{
hr=lpdsbuffer1->Release();
lpdsbuffer1=NULL;
}
if (lpds!=NULL)
{
hr=lpds->Release();
lpds=NULL;
}
}
SndLenth1= 0;
SndLenth2= 0;
BuffOffset=0;
AuxBufferPointer=0;
BitRate=iRateList[Rate];
BlockSize=BitRate*4/TARGETFRAMERATE;
SndBuffLenth = (BlockSize*AUDIOBUFFERS);
if (Rate)
{
hr=DirectSoundCreate(Guid, &lpds, NULL); // create a directsound object
if (hr!=DS_OK)
return(1);
hr=lpds->SetCooperativeLevel(main_window_handle,DSSCL_NORMAL);//DSSCL_NORMAL);// set cooperation level normal DSSCL_EXCLUSIVE
if (hr!=DS_OK)
return(1);
// set up the format data structure
memset(&pcmwf, 0, sizeof(WAVEFORMATEX));
pcmwf.wFormatTag = WAVE_FORMAT_PCM;
pcmwf.nChannels = 2;
pcmwf.nSamplesPerSec = BitRate;
pcmwf.wBitsPerSample = 16;
pcmwf.nBlockAlign = (pcmwf.wBitsPerSample * pcmwf.nChannels) >> 3;
pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
pcmwf.cbSize = 0;
// create the secondary buffer
memset(&dsbd,0,sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_LOCSOFTWARE|DSBCAPS_STATIC|DSBCAPS_GLOBALFOCUS;
dsbd.dwBufferBytes = SndBuffLenth;
dsbd.lpwfxFormat = &pcmwf;
hr=lpds->CreateSoundBuffer(&dsbd,&lpdsbuffer1,NULL);
if (hr!=DS_OK)
return(1);
// Clear out sound buffers
hr= lpdsbuffer1->Lock(0,SndBuffLenth,&SndPointer1,&SndLenth1,&SndPointer2,&SndLenth2,DSBLOCK_ENTIREBUFFER );
if (hr!=DS_OK)
return(1);
memset (SndPointer1,0,SndBuffLenth);
hr=lpdsbuffer1->Unlock(SndPointer1,SndLenth1,SndPointer2,SndLenth2);
if (hr!=DS_OK)
return(1);
lpdsbuffer1->SetCurrentPosition(0);
hr=lpdsbuffer1->Play(0,0,DSBPLAY_LOOPING ); // play the sound in looping mode
if (hr!=DS_OK)
return(1);
InitPassed=1;
AudioPause=0;
}
SetAudioRate (iRateList[Rate]);
// SetAudioRate(44100);
return(0);
}
void FlushAudioBuffer(unsigned int *Abuffer,unsigned short Lenth)
{
unsigned short LeftAverage=0,RightAverage=0,Index=0;
unsigned char Flag=0;
unsigned char *Abuffer2=(unsigned char *)Abuffer;
LeftAverage=Abuffer[0]>>16;
RightAverage=Abuffer[0]& 0xFFFF;
UpdateSoundBar(LeftAverage,RightAverage);
if ((!InitPassed) | (AudioPause))
return;
// sprintf(Msg,"Lenth= %i Free Blocks= %i\n",Lenth,GetFreeBlockCount());
// WriteLog(Msg,0);
if (GetFreeBlockCount()<=0) //this should only kick in when frame skipping or unthrottled
{
memcpy(AuxBuffer[AuxBufferPointer],Abuffer2,Lenth); //Saving buffer to aux stack
AuxBufferPointer++; //and chase your own tail
AuxBufferPointer%=5; //At this point we are so far behind we may as well drop the buffer
return;
}
hr=lpdsbuffer1->Lock(BuffOffset,Lenth,&SndPointer1,&SndLenth1,&SndPointer2,&SndLenth2,0);
if (hr != DS_OK)
return;
memcpy(SndPointer1, Abuffer2, SndLenth1); // copy first section of circular buffer
if (SndPointer2 !=NULL) // copy last section of circular buffer if wrapped
memcpy(SndPointer2, Abuffer2+SndLenth1,SndLenth2);
hr=lpdsbuffer1->Unlock(SndPointer1,SndLenth1,SndPointer2,SndLenth2);// unlock the buffer
BuffOffset=(BuffOffset + Lenth)% SndBuffLenth; //Where to write next
}
int GetFreeBlockCount(void) //return 0 on full buffer
{
unsigned long WriteCursor=0,PlayCursor=0;
long RetVal=0,MaxSize=0;
if ((!InitPassed) | (AudioPause))
return(AUDIOBUFFERS);
RetVal=lpdsbuffer1->GetCurrentPosition(&PlayCursor,&WriteCursor);
if (BuffOffset <=PlayCursor)
MaxSize= PlayCursor - BuffOffset;
else
MaxSize= SndBuffLenth - BuffOffset + PlayCursor;
return(MaxSize/BlockSize);
}
void PurgeAuxBuffer(void)
{
if ((!InitPassed) | (AudioPause))
return;
return;
AuxBufferPointer--; //Normally points to next free block Point to last used block
if (AuxBufferPointer>=0) //zero is a valid data block
{
while ((GetFreeBlockCount()<=0));
hr=lpdsbuffer1->Lock(BuffOffset,BlockSize,&SndPointer1,&SndLenth1,&SndPointer2,&SndLenth2,0);//DSBLOCK_FROMWRITECURSOR);
if (hr != DS_OK)
return;
memcpy(SndPointer1, AuxBuffer[AuxBufferPointer], SndLenth1);
if (SndPointer2 !=NULL)
memcpy(SndPointer2, (AuxBuffer[AuxBufferPointer]+(SndLenth1>>2)),SndLenth2);
BuffOffset=(BuffOffset + BlockSize)% SndBuffLenth;
hr=lpdsbuffer1->Unlock(SndPointer1,SndLenth1,SndPointer2,SndLenth2);
AuxBufferPointer--;
}
AuxBufferPointer=0;
return;
}
int GetSoundCardList (SndCardList *List)
{
CardCount=0;
Cards=List;
DirectSoundEnumerate(DSEnumCallback, NULL);
return(CardCount);
}
BOOL CALLBACK DSEnumCallback(LPGUID lpGuid,LPCSTR lpcstrDescription,LPCSTR lpcstrModule,LPVOID lpContext)
{
strncpy(Cards[CardCount].CardName,lpcstrDescription,63);
Cards[CardCount++].Guid=lpGuid;
return (CardCount<MAXCARDS);
}
int SoundDeInit(void)
{
if (InitPassed)
{
InitPassed=0;
lpdsbuffer1->Stop();
lpds->Release();
}
return(0);
}
int SoundInInit (HWND main_window_handle,_GUID * Guid)
{
hr=DirectSoundCaptureCreate(Guid, &lpdsin, NULL);
if (hr!=DS_OK)
return(1);
dsbdin.dwSize=sizeof(DSCBUFFERDESC); // Size of the structure
dsbdin.dwFlags=0;
dsbdin.dwReserved=0;
dsbdin.lpwfxFormat=&pcmwf; //fix
dsbdin.dwBufferBytes=SndBuffLenth;
hr=lpdsin->CreateCaptureBuffer(&dsbdin, &lpdsbuffer2, NULL);
if (hr!=DS_OK)
return(1);
// lpdsbuffer2->Initialize(lpdsin,&dsbdin);
lpdsbuffer2->Start(hr);
return(0);
}
unsigned short GetSoundStatus(void)
{
return(CurrentRate);
}
void ResetAudio (void)
{
SetAudioRate (iRateList[CurrentRate]);
// SetAudioRate(44100);
if (InitPassed)
lpdsbuffer1->SetCurrentPosition(0);
BuffOffset=0;
AuxBufferPointer=0;
return;
}
unsigned char PauseAudio(unsigned char Pause)
{
AudioPause=Pause;
if (InitPassed)
{
if (AudioPause==1)
hr=lpdsbuffer1->Stop();
else
hr=lpdsbuffer1->Play(0,0,DSBPLAY_LOOPING);
}
return(AudioPause);
}