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

Method generateSamples

test/browser/test_sdl2_audio_beep.cpp:91–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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