Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Audio/Oboe/OboeAudioDevice.cpp
1177 views
1
uint8 AudioDevice::contextInitialized;
2
oboe::Result AudioDevice::status = oboe::Result::OK;
3
oboe::AudioStream *AudioDevice::stream;
4
pthread_mutex_t AudioDevice::mutex;
5
6
// REAL LINE I WROTE
7
AudioDevice *AudioDevice::audioDevice;
8
9
bool AudioDevice::createStream() {
10
oboe::AudioStreamBuilder builder;
11
12
builder.setSampleRate(AUDIO_FREQUENCY)
13
->setFormat(oboe::AudioFormat::Float)
14
->setChannelCount(AUDIO_CHANNELS)
15
->setBufferCapacityInFrames(MIX_BUFFER_SIZE)
16
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
17
->setDataCallback(audioDevice)
18
->setErrorCallback(audioDevice);
19
20
oboe::Result result = builder.openStream(&stream);
21
if (result != oboe::Result::OK) {
22
PrintLog(PRINT_NORMAL, "Oboe: Failed to open stream: (%s)", convertToText(result));
23
return false;
24
}
25
return true;
26
}
27
28
bool AudioDevice::shutdownStream() {
29
if (stream->getState() != oboe::StreamState::Closed) {
30
stream->stop();
31
oboe::Result result = stream->close();
32
if (result != oboe::Result::OK) {
33
PrintLog(PRINT_NORMAL, "Oboe: Failed to shutdown stream: (%s)", convertToText(result));
34
return false;
35
}
36
}
37
return true;
38
}
39
40
bool32 AudioDevice::Init()
41
{
42
if (!contextInitialized) {
43
contextInitialized = true;
44
InitAudioChannels();
45
audioDevice = new AudioDevice();
46
}
47
48
if (!createStream())
49
return false;
50
51
stream->requestStart();
52
return true;
53
}
54
55
void AudioDevice::InitAudioChannels()
56
{
57
pthread_mutex_init(&mutex, NULL);
58
59
AudioDeviceBase::InitAudioChannels();
60
}
61
62
oboe::DataCallbackResult AudioDevice::onAudioReady(oboe::AudioStream *s, void *data, int32 len)
63
{
64
if (s != stream)
65
return oboe::DataCallbackResult::Stop;
66
67
LockAudioDevice();
68
AudioDevice::ProcessAudioMixing(data, len * AUDIO_CHANNELS);
69
UnlockAudioDevice();
70
71
return oboe::DataCallbackResult::Continue;
72
}
73
74
void AudioDevice::onErrorAfterClose(oboe::AudioStream *s, oboe::Result error) {
75
LockAudioDevice();
76
if (error == oboe::Result::ErrorDisconnected) {
77
// Clean up and create a new audio stream
78
shutdownStream();
79
createStream();
80
stream->requestStart();
81
}
82
UnlockAudioDevice();
83
}
84
85
void AudioDevice::Release()
86
{
87
LockAudioDevice();
88
shutdownStream();
89
UnlockAudioDevice();
90
AudioDeviceBase::Release();
91
pthread_mutex_destroy(&mutex);
92
delete audioDevice;
93
};
94
95
void AudioDevice::FrameInit()
96
{
97
if (status != oboe::Result::OK) {
98
stream->requestStop();
99
stream->close();
100
Init();
101
}
102
};
103
104
void AudioDevice::HandleStreamLoad(ChannelInfo *channel, bool32 async)
105
{
106
if (async) {
107
pthread_t loadThread;
108
pthread_create(&loadThread, NULL, LoadStreamASync, channel);
109
}
110
else
111
LoadStream(channel);
112
};
113