MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / generateSamples

Method generateSamples

test/browser/test_sdl_audio_beep.cpp:91–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89
90template<typename T>
91void Beeper::generateSamples(T *stream, int length) {
92 T AMPLITUDE;
93 if (sdlAudioFormat == AUDIO_F32) {
94 AMPLITUDE = (T) 0.8f;
95 }
96 else {
97 AMPLITUDE = (sizeof(T) == 2) ? 28000 : 120;
98 }
99 const T offset = (sdlAudioFormat == AUDIO_U8) ? 120 : 0;
100
101 int i = 0;
102 length /= numChannels;
103 while (i < length) {
104 if (beeps.empty()) {
105 memset(stream + numChannels*i, 0, sizeof(T)*numChannels*(length-i));
106 return;
107 }
108 BeepObject& bo = beeps.front();
109
110 // In Stereo tests, mute one of the channels to be able to distinguish that Stereo output works.
111 if (bo.samplesLeft > tone_duration * frequency / 2 / 1000) {
112 mutedChannel = 1;
113 } else {
114 mutedChannel = 0;
115 }
116
117 int samplesToDo = std::min(i + bo.samplesLeft, length);
118 bo.samplesLeft -= samplesToDo - i;
119
120 while (i < samplesToDo) {
121 for(int j = 0; j < numChannels; ++j) {
122 stream[numChannels*i+j] = (T)(offset + (AMPLITUDE * std::sin(phase * 2 * M_PI / frequency)));
123 if (numChannels > 1 && j == mutedChannel) {
124 stream[numChannels*i+j] = 0;
125 }
126 }
127 phase += bo.toneFrequency;
128 i++;
129 }
130
131 if (bo.samplesLeft == 0) {
132 beeps.pop();
133 }
134 }
135}
136
137void Beeper::beep(double toneFrequency, int durationMSecs) {
138 BeepObject bo;

Callers 1

audio_callbackFunction · 0.45

Calls 6

memsetFunction · 0.85
sinFunction · 0.85
minFunction · 0.50
emptyMethod · 0.45
frontMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected