-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaudio.cpp
79 lines (62 loc) · 1.6 KB
/
audio.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
#include "audio.h"
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <libsm64.h>
#include "config.h"
#include "audioApis/audio_sdl2.h"
#include "audioApis/audio_wasapi.h"
pthread_t gSoundThread;
bool audio_started;
AudioAPI* audio_api;
long long timeInMilliseconds(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return(((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}
void* audio_thread(void* keepAlive)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
long long currentTime = timeInMilliseconds();
long long targetTime = 0;
while(1)
{
if(!*((bool*)keepAlive)) return NULL;
int16_t audioBuffer[544 * 2 * 2];
uint32_t numSamples = sm64_audio_tick(audio_api->getBuffered(), 1100, audioBuffer);
audio_api->play(audioBuffer, numSamples*2*4);
targetTime = currentTime + 33;
while (timeInMilliseconds() < targetTime)
{
usleep(100);
if(!*((bool*)keepAlive)) return NULL;
}
currentTime = timeInMilliseconds();
}
}
void audio_thread_init()
{
if (audio_started) return;
if (getConfig("use_wasapi_audio"))
audio_api = new AudioAPI_WASAPI();
else
audio_api = new AudioAPI_SDL2();
if (!audio_api->init())
{
delete audio_api;
return;
}
// it's best to run audio in a separate thread
pthread_create(&gSoundThread, NULL, audio_thread, &audio_started);
audio_started = true;
}
void audio_thread_stop()
{
if (!audio_started) return;
pthread_cancel(gSoundThread);
audio_started = false;
audio_api->destroy();
delete audio_api;
}