MCPcopy Create free account
hub / github.com/android/ndk-samples / PlayTone

Method PlayTone

endless-tunnel/app/src/main/cpp/sfxman.cpp:221–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

219}
220
221void SfxMan::PlayTone(const char* tone) {
222 if (!mInitOk) {
223 LOGW("SfxMan: not playing sound because initialization failed.");
224 return;
225 }
226 if (_bufferActive) {
227 // can't play -- the buffer is in use
228 LOGW("SfxMan: can't play tone; buffer is active.");
229 return;
230 }
231
232 // synth the tone
233 int total_samples = 0;
234 int num_samples;
235 int frequency = 100;
236 int duration = 50;
237 int volume_int;
238 float amplitude = DEFAULT_VOLUME;
239
240 while (*tone) {
241 switch (*tone) {
242 case 'f':
243 // set frequency
244 tone = _parseInt(tone + 1, &frequency);
245 break;
246 case 'd':
247 // set duration
248 tone = _parseInt(tone + 1, &duration);
249 break;
250 case 'a':
251 // set amplitude.
252 tone = _parseInt(tone + 1, &volume_int);
253 amplitude = volume_int / 100.0f;
254 amplitude = amplitude < 0.0f ? 0.0f
255 : amplitude > 1.0f ? 1.0f
256 : amplitude;
257 break;
258 case '.':
259 // synth
260 num_samples = duration * SAMPLES_PER_SEC / 1000;
261 if (num_samples > (BUF_SAMPLES_MAX - total_samples - 1)) {
262 num_samples = BUF_SAMPLES_MAX - total_samples - 1;
263 }
264 num_samples = _synth(frequency, amplitude, _sample_buf + total_samples,
265 num_samples);
266 total_samples += num_samples;
267 tone++;
268 break;
269 default:
270 // ignore and advance to next character
271 tone++;
272 }
273 }
274
275 SLresult result;
276 int total_size = total_samples * sizeof(short);
277 if (total_size <= 0) {
278 LOGW("Tone is empty. Not playing.");

Callers 2

DoFrameMethod · 0.80
DetectCollisionsMethod · 0.80

Calls 3

_parseIntFunction · 0.85
_synthFunction · 0.85
_taperFunction · 0.85

Tested by

no test coverage detected