void SfxMan::PlayTone()

in agdk/agde/endless-tunnel/endless-tunnel/Android/app/src/main/cpp/sfxman.cpp [206:273]


void SfxMan::PlayTone(const char *tone) {
    if (!mInitOk) {
        LOGW("SfxMan: not playing sound because initialization failed.");
        return;
    }
    if (_bufferActive) {
        // can't play -- the buffer is in use
        LOGW("SfxMan: can't play tone; buffer is active.");
        return;
    }

    // synth the tone
    int total_samples = 0;
    int num_samples;
    int frequency = 100;
    int duration = 50;
    int volume_int;
    float amplitude = DEFAULT_VOLUME;

    while (*tone) {
       switch (*tone) {
           case 'f':
               // set frequency
               tone = _parseInt(tone + 1, &frequency);
               break;
           case 'd':
               // set duration
               tone = _parseInt(tone + 1, &duration);
               break;
           case 'a':
               // set amplitude.
               tone = _parseInt(tone + 1, &volume_int);
               amplitude = volume_int / 100.0f;
               amplitude = amplitude < 0.0f ? 0.0f : amplitude > 1.0f ? 1.0f : amplitude;
               break;
           case '.':
               // synth
               num_samples = duration * SAMPLES_PER_SEC / 1000;
               if (num_samples > (BUF_SAMPLES_MAX - total_samples - 1)) {
                   num_samples = BUF_SAMPLES_MAX - total_samples - 1;
               }
               num_samples = _synth(frequency, duration, amplitude, _sample_buf + total_samples,
                       num_samples);
               total_samples += num_samples;
               tone++;
               break;
           default:
               // ignore and advance to next character
               tone++;
       }
    }

    SLresult result;
    int total_size = total_samples * sizeof(short);
    if (total_size <= 0) {
        LOGW("Tone is empty. Not playing.");
        return;
    }

    _taper(_sample_buf, total_samples);

    _bufferActive = true;
    result = (*mPlayerBufferQueue)->Enqueue(mPlayerBufferQueue, _sample_buf, total_size);
    if (result != SL_RESULT_SUCCESS) {
        LOGW("SfxMan: warning: failed to enqueue buffer: %lu", (unsigned long)result);
        return;
    }
}