12 #include <unordered_map>
16 #define SAMPLE_DATATYPE short
18 #include <portaudio.h>
59 unsigned long currentFrame = 0;
60 std::vector<char> data;
61 bool playingRepeat =
false;
63 template <
class Archive>
64 void serialize(Archive& archive) {
81 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS)
83 #elif defined(__ANDROID__)
84 oboe::AudioStreamBuilder *streamBuilder;
85 oboe::AudioStream *stream;
86 #elif defined(SMALL3D_IOS)
87 static ALCdevice *openalDevice;
88 static ALCcontext *openalContext;
93 static bool noOutputDevice;
95 static unsigned int numInstances;
97 #if !defined(SMALL3D_IOS) && !defined(__ANDROID__)
98 static PaDeviceIndex defaultOutput;
99 static int audioCallback(
const void *inputBuffer,
void *outputBuffer,
100 unsigned long framesPerBuffer,
101 const PaStreamCallbackTimeInfo *timeInfo,
102 PaStreamCallbackFlags statusFlags,
104 #elif defined(__ANDROID__)
106 class AudioCallbackClass :
public oboe::AudioStreamDataCallback {
108 SoundData *soundData;
110 AudioCallbackClass(SoundData *soundData) {
111 this->soundData = soundData;
113 oboe::DataCallbackResult onAudioReady(oboe::AudioStream *audioStream,
void *audioData,
116 auto *out =
static_cast<SAMPLE_DATATYPE*
>(audioData);
118 if (soundData->currentFrame * SAMPLES_PER_FRAME >= soundData->samples) {
119 if (soundData->repeat) {
120 soundData->currentFrame = 0;
124 memset(out, 0, WORD_SIZE * numFrames * SAMPLES_PER_FRAME * soundData->channels);
125 return oboe::DataCallbackResult::Stop;
129 memcpy(out, &soundData->data.data()[WORD_SIZE * soundData->currentFrame *
130 SAMPLES_PER_FRAME * soundData->channels],
131 WORD_SIZE * numFrames * SAMPLES_PER_FRAME * soundData->channels);
133 soundData->currentFrame += numFrames;
135 return oboe::DataCallbackResult::Continue;
139 AudioCallbackClass audioCallbackObject;
143 void load(
const std::string& soundFilePath);
156 explicit Sound(
const std::string& soundFilePath);
167 void play(
const bool repeat=
false);
205 void saveBinary(
const std::string& binaryFilePath);