-
Notifications
You must be signed in to change notification settings - Fork 2
/
sound.cpp
188 lines (179 loc) · 3.91 KB
/
sound.cpp
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
#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <mmsystem.h>
#include <dsound.h>
#include <math.h>
#include "myfile.h"
LPDIRECTSOUND dsnd;
extern HWND hWnd;
extern bool u6o_sound;
extern unsigned char u6ovolume;
bool DirectSoundCreate_fail=FALSE;
struct sound {
DSBUFFERDESC d;
WAVEFORMATEX wf;
LPDIRECTSOUNDBUFFER s;
unsigned char* o;
sound* ss;
};
sound* tempsound[256];
bool soundsetupf=FALSE;
bool soundsetup()
{
if (DS_OK!=DirectSoundCreate(NULL,&dsnd,NULL)){
DirectSoundCreate_fail=TRUE;
MessageBox(NULL,"WAV DirectSoundCreate failed","Ultima 6 Online",MB_OK);
return FALSE;
}
dsnd->SetCooperativeLevel(hWnd,DSSCL_NORMAL);
static long i;
for (i=0;i<256;i++)
{
tempsound[i]=NULL;
}
soundsetupf=TRUE;
return TRUE;
}
sound* soundnew(long freq,long bit,long stereo,long bytes)
{
if (DirectSoundCreate_fail) return NULL;
if (soundsetupf==FALSE) soundsetup();
if (DirectSoundCreate_fail) return NULL;
sound* ts=(sound*)malloc(sizeof(sound));
ZeroMemory(ts,sizeof(sound));
ts->wf.cbSize=0;
ts->wf.wFormatTag=WAVE_FORMAT_PCM;
ts->wf.nChannels=stereo;
ts->wf.nSamplesPerSec=freq;
ts->wf.wBitsPerSample=bit;
ts->wf.nBlockAlign=ts->wf.nChannels*ts->wf.wBitsPerSample/8;
ts->wf.nAvgBytesPerSec=ts->wf.nSamplesPerSec*ts->wf.nBlockAlign;
ts->d.dwSize=sizeof(DSBUFFERDESC);
//software buffer is used to maintain offset of sound data
ts->d.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN|DSBCAPS_CTRLFREQUENCY|DSBCAPS_GETCURRENTPOSITION2;
ts->d.dwBufferBytes=bytes;
ts->d.lpwfxFormat=&ts->wf;
dsnd->CreateSoundBuffer(&ts->d,&ts->s,NULL);
ts->d.dwBufferBytes=bytes;
static void* o1;
static unsigned long s1;
ts->s->Lock(0,0,&o1,&s1,NULL,NULL,DSBLOCK_ENTIREBUFFER);
ts->o=(unsigned char*)o1;
ts->s->Unlock(o1,s1,NULL,NULL);
return ts;
}
sound* soundload(LPCSTR fn)
{
if (DirectSoundCreate_fail) return NULL;
if (soundsetupf==FALSE) soundsetup();
if (DirectSoundCreate_fail) return NULL;
static sound* ts;
static file* fh;
static long freq,bits,stereo,x;
fh=open(fn);
if (fh->h==HFILE_ERROR)
{
ts=NULL;
return ts;
}
seek(fh,24);
get(fh,&freq,4);
seek(fh,34);
bits=0;
get(fh,&bits,2);
seek(fh,32);
stereo=0;
get(fh,&stereo,2);
if (stereo==(bits/8))
{stereo=1;}else{stereo=2;}
ts=soundnew(freq,bits,stereo,lof(fh)-58-16-16-32-32);
seek(fh,58);
get(fh,ts->o,lof(fh)-58-16-16-32-32);
close(fh);
return ts;
}
sound* soundplay(sound* s)
{
if (DirectSoundCreate_fail) return NULL;
//if (u6o_sound==FALSE) return NULL; //no sound
if (soundsetupf==FALSE) return NULL;
static unsigned long i,i2;
if (s==NULL) return NULL;
i=-1;
busysound:
i++;
if (i==256) return NULL;
if (tempsound[i]!=NULL)
{
tempsound[i]->s->GetStatus(&i2);
if (i2&DSBSTATUS_PLAYING) goto busysound;
tempsound[i]->s->Release();
free((void*)tempsound[i]);
}
sound* ts=(sound*)malloc(sizeof(sound));
memcpy(ts,s,sizeof(sound));
dsnd->DuplicateSoundBuffer(s->s,&ts->s);
tempsound[i]=ts;
tempsound[i]->ss=s;
ts->s->Play(NULL,NULL,NULL);
return ts;
}
sound* soundplay2(sound* s,long v)
{
if (DirectSoundCreate_fail) return NULL;
if (soundsetupf==FALSE) return NULL;
if (u6ovolume==0) return NULL;
if (v>255) v=255;
if (v<=0) return NULL;
static long i;
static unsigned long i2;
static float f;
if (s==NULL) return NULL;
i=-1;
busysound2:
i++;
if (i==256) return NULL;
if (tempsound[i]!=NULL)
{
tempsound[i]->s->GetStatus(&i2);
if (i2&DSBSTATUS_PLAYING) goto busysound2;
tempsound[i]->s->Release();
free((void*)tempsound[i]);
}
sound* ts=(sound*)malloc(sizeof(sound));
memcpy(ts,s,sizeof(sound));
dsnd->DuplicateSoundBuffer(s->s,&ts->s);
tempsound[i]=ts;
tempsound[i]->ss=s;
f=v;
f=f*(float)u6ovolume/255.0f;
f=255-f;
f=f*0.25f;
f*=f;
ts->s->SetVolume(-f);
ts->s->Play(NULL,NULL,NULL);
return ts;
}
void free(sound* s){
if (DirectSoundCreate_fail) return;
if (soundsetupf==FALSE) return;
if (s!=NULL){
s->s->Release();
static long i;
for (i=0;i<256;i++)
{
if (tempsound[i]!=NULL)
{
if (tempsound[i]->ss==s)
{
tempsound[i]->s->Release();
free((void*)tempsound[i]);
tempsound[i]=NULL;
}
free((void*)s);
}
}
}
return;
}