|
| 1 | +// This file is part of SmallBASIC |
| 2 | +// |
| 3 | +// Copyright(C) 2001-2019 Chris Warren-Smith. |
| 4 | +// |
| 5 | +// This program is distributed under the terms of the GPL v2.0 or later |
| 6 | +// Download the GNU Public License (GPL) from www.gnu.org |
| 7 | +// |
| 8 | + |
| 9 | +#define MINIAUDIO_IMPLEMENTATION |
| 10 | +#define DR_WAV_IMPLEMENTATION |
| 11 | +//#define DR_MP3_IMPLEMENTATION |
| 12 | + |
| 13 | +#include "config.h" |
| 14 | +#include <stdio.h> |
| 15 | +#include "common/osd.h" |
| 16 | +#include "common/device.h" |
| 17 | +#include "ui/strlib.h" |
| 18 | +#include "lib/miniaudio/miniaudio.h" |
| 19 | +#include "lib/miniaudio/extras/dr_wav.h" |
| 20 | +//#include "lib/miniaudio/extras/dr_mp3.h" |
| 21 | + |
| 22 | +#define DEVICE_SAMPLE_RATE 48000 |
| 23 | +#define DEVICE_CHANNELS 1 |
| 24 | +#define MILLIS_TO_MICROS(n) (n * 1000) |
| 25 | + |
| 26 | +struct Sound { |
| 27 | + Sound(int frequency, int millis, int volume); |
| 28 | + ~Sound(); |
| 29 | + |
| 30 | + ma_sine_wave *_tone; |
| 31 | + uint32_t _start; |
| 32 | + uint32_t _duration; |
| 33 | +}; |
| 34 | + |
| 35 | +Sound::Sound(int frequency, int millis, int volume) : |
| 36 | + _tone(nullptr), |
| 37 | + _start(0), |
| 38 | + _duration(millis) { |
| 39 | + _tone = (ma_sine_wave *)malloc(sizeof(ma_sine_wave)); |
| 40 | + ma_sine_wave_init(volume / 100.0, frequency, DEVICE_SAMPLE_RATE, _tone); |
| 41 | +} |
| 42 | + |
| 43 | +Sound::~Sound() { |
| 44 | + free(_tone); |
| 45 | + _tone = nullptr; |
| 46 | +} |
| 47 | + |
| 48 | +strlib::Queue<Sound *> queue; |
| 49 | +ma_decoder decoder; |
| 50 | +ma_device device; |
| 51 | +ma_device_config config; |
| 52 | + |
| 53 | +void data_callback(ma_device *device, void *output, const void *input, ma_uint32 frameCount) { |
| 54 | + if (device->playback.channels == DEVICE_CHANNELS) { |
| 55 | + if (queue.empty()) { |
| 56 | + usleep(MILLIS_TO_MICROS(10)); |
| 57 | + } else { |
| 58 | + Sound *sound = queue.front(); |
| 59 | + if (sound->_start == 0) { |
| 60 | + // start new sound |
| 61 | + sound->_start = dev_get_millisecond_count(); |
| 62 | + ma_sine_wave_read_f32(sound->_tone, frameCount, (float*)output); |
| 63 | + } else if (dev_get_millisecond_count() - sound->_start > sound->_duration) { |
| 64 | + // sound has timed out |
| 65 | + queue.pop(); |
| 66 | + } else { |
| 67 | + // continue sound |
| 68 | + ma_sine_wave_read_f32(sound->_tone, frameCount, (float*)output); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +bool open_audio() { |
| 75 | + config = ma_device_config_init(ma_device_type_playback); |
| 76 | + config.playback.format = ma_format_f32; |
| 77 | + config.playback.channels = DEVICE_CHANNELS; |
| 78 | + config.sampleRate = DEVICE_SAMPLE_RATE; |
| 79 | + config.dataCallback = data_callback; |
| 80 | + config.pUserData = nullptr; |
| 81 | + return (ma_device_init(nullptr, &config, &device) == MA_SUCCESS); |
| 82 | +} |
| 83 | + |
| 84 | +void close_audio() { |
| 85 | + ma_device_uninit(&device); |
| 86 | +} |
| 87 | + |
| 88 | +void osd_audio(const char *path) { |
| 89 | + //if (ma_decoder_init_file_wav(path, nullptr, &decoder) == MA_SUCCESS) { |
| 90 | + // ma_device_start(&device); |
| 91 | + // } else { |
| 92 | + // fprintf(stderr, "failed\n"); |
| 93 | + // } |
| 94 | +} |
| 95 | + |
| 96 | +void osd_clear_sound_queue() { |
| 97 | + ma_device_stop(&device); |
| 98 | + queue.removeAll(); |
| 99 | +} |
| 100 | + |
| 101 | +void osd_beep() { |
| 102 | + osd_sound(1000, 30, 100, 0); |
| 103 | + osd_sound(500, 30, 100, 0); |
| 104 | +} |
| 105 | + |
| 106 | +void osd_sound(int frequency, int millis, int volume, int background) { |
| 107 | + ma_mutex_lock(&device.lock); |
| 108 | + queue.push(new Sound(frequency, millis, volume)); |
| 109 | + ma_mutex_unlock(&device.lock); |
| 110 | + |
| 111 | + if (!background) { |
| 112 | + ma_device_start(&device); |
| 113 | + usleep(MILLIS_TO_MICROS(millis)); |
| 114 | + ma_device_stop(&device); |
| 115 | + ma_mutex_lock(&device.lock); |
| 116 | + queue.pop(); |
| 117 | + ma_mutex_unlock(&device.lock); |
| 118 | + } else if (queue.size() == 1) { |
| 119 | + ma_device_start(&device); |
| 120 | + } |
| 121 | +} |
0 commit comments