-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
executable file
·159 lines (121 loc) · 3.61 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
#include "audio.h"
HMIDIOUT device;
int device_flag;
void CALLBACK WaveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
WAVEHDR *whdr;
switch(uMsg) {
case WOM_OPEN:
break;
case WOM_DONE:
whdr = (WAVEHDR*)dwParam1;
if(whdr->dwUser)
waveOutWrite(hwo, whdr, sizeof(WAVEHDR));
else {
waveOutUnprepareHeader(hwo, whdr, sizeof(WAVEHDR));
free(whdr);
}
break;
case WOM_CLOSE:
break;
}
dwInstance = dwInstance;
dwParam2 = dwParam2;
}
void openAudioFile(LPSTR szFileName, Audio *audio)
{
FILE *f;
UINT devid;
f = fopen(szFileName, "rb");
if(!f)
return;
fseek(f, 0, SEEK_END);
audio->file_size = ftell(f);
fseek(f, 0, SEEK_SET);
audio->lpfile = (char *)malloc(sizeof(char)* audio->file_size );
fread(audio->lpfile, 1, audio->file_size , f);
fclose(f);
audio->wfex.wFormatTag = *((WORD* )(audio->lpfile + OFFSET_FORMATTAG ));
audio->wfex.nChannels = *((WORD* )(audio->lpfile + OFFSET_CHANNELS ));
audio->wfex.nSamplesPerSec = *((DWORD*)(audio->lpfile + OFFSET_SAMPLESPERSEC ));
audio->wfex.nAvgBytesPerSec = *((DWORD*)(audio->lpfile + OFFSET_AVGBYTESPERSEC));
audio->wfex.nBlockAlign = *((WORD* )(audio->lpfile + OFFSET_BLOCKALIGN ));
audio->wfex.wBitsPerSample = *((WORD* )(audio->lpfile + OFFSET_BITSPERSAMPLE ));
audio->loop = 0;
audio->whdr = NULL;
for(devid = 0; devid < waveOutGetNumDevs(); devid++) {
if(devid == waveOutGetNumDevs()) {
audio->whdr = NULL;
return;
}
if(waveOutOpen(&(audio->hwo), WAVE_MAPPER, &(audio->wfex), (DWORD)WaveOutProc, 0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR)
break;
}
return;
}
void playAudio(Audio *audio)
{
WAVEHDR* whdr;
whdr = (WAVEHDR*)malloc(sizeof(WAVEHDR));
ZeroMemory((void*)whdr, sizeof(WAVEHDR));
whdr->dwFlags = 0L;
whdr->lpData = audio->lpfile+HEADER_SIZE;
whdr->dwBufferLength = audio->file_size-HEADER_SIZE;
whdr->dwUser = (DWORD) audio->loop;
if(waveOutPrepareHeader(audio->hwo, whdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
audio->whdr = NULL;
return;
}
if(waveOutWrite(audio->hwo, whdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
audio->whdr = NULL;
return;
}
audio->whdr = whdr;
}
void pauseAudio(Audio *audio)
{
waveOutPause(audio->hwo);
}
void restartAudio(Audio *audio)
{
waveOutRestart(audio->hwo);
}
long getPosition(Audio *audio)
{
MMTIME mmtime;
mmtime.wType = TIME_MS;
if (audio->whdr != NULL) {
waveOutGetPosition(audio->hwo, &mmtime, sizeof(mmtime));
return mmtime.u.ms;
} else {
return 0;
}
}
void openMidi(void)
{
device_flag = midiOutOpen(&device, 0, 0, 0, CALLBACK_NULL);
}
void closeMidi(void)
{
if (device_flag == MMSYSERR_NOERROR) {
midiOutReset(device);
midiOutClose(device);
}
device_flag = -1000;
}
void playMidi(int piano_or_drum, int pitch, int volume)
{
union {
unsigned long word;
unsigned char data[4];
} message;
if (piano_or_drum == 0)
message.data[0] = 0x90;
else
message.data[0] = 0x99;
message.data[1] = pitch;
message.data[2] = volume;
message.data[3] = 0;
if (device_flag == MMSYSERR_NOERROR)
midiOutShortMsg(device, message.word);
}