2023-02-20 03:27:35 +00:00
|
|
|
#include "ultra64.h"
|
2023-11-13 00:40:02 +00:00
|
|
|
#include "ultramodern.hpp"
|
2023-02-20 03:27:35 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
static uint32_t sample_rate = 48000;
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
static ultramodern::audio_callbacks_t audio_callbacks;
|
2023-06-22 22:29:55 +00:00
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
void set_audio_callbacks(const ultramodern::audio_callbacks_t& callbacks) {
|
2023-11-04 16:45:22 +00:00
|
|
|
audio_callbacks = callbacks;
|
2023-06-22 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
void ultramodern::init_audio() {
|
2023-02-20 03:27:35 +00:00
|
|
|
// Pick an initial dummy sample rate; this will be set by the game later to the true sample rate.
|
|
|
|
set_audio_frequency(48000);
|
|
|
|
}
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
void ultramodern::set_audio_frequency(uint32_t freq) {
|
2023-06-22 22:29:55 +00:00
|
|
|
if (audio_callbacks.set_frequency) {
|
|
|
|
audio_callbacks.set_frequency(freq);
|
2023-02-20 03:27:35 +00:00
|
|
|
}
|
|
|
|
sample_rate = freq;
|
|
|
|
}
|
|
|
|
|
2023-11-13 00:40:02 +00:00
|
|
|
void ultramodern::queue_audio_buffer(RDRAM_ARG PTR(int16_t) audio_data_, uint32_t byte_count) {
|
2023-02-20 03:27:35 +00:00
|
|
|
// Ensure that the byte count is an integer multiple of samples.
|
|
|
|
assert((byte_count & 1) == 0);
|
|
|
|
|
|
|
|
// Calculate the number of samples from the number of bytes.
|
2023-06-22 22:29:55 +00:00
|
|
|
uint32_t sample_count = byte_count / sizeof(int16_t);
|
2023-02-20 03:27:35 +00:00
|
|
|
|
|
|
|
// Queue the swapped audio data.
|
2023-06-22 22:29:55 +00:00
|
|
|
if (audio_callbacks.queue_samples) {
|
2024-03-22 03:56:19 +00:00
|
|
|
audio_callbacks.queue_samples(TO_PTR(int16_t, audio_data_), sample_count);
|
2023-06-22 22:29:55 +00:00
|
|
|
}
|
2023-02-20 03:27:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 22:29:55 +00:00
|
|
|
// For SDL2
|
|
|
|
//uint32_t buffer_offset_frames = 1;
|
|
|
|
// For Godot
|
|
|
|
float buffer_offset_frames = 0.5f;
|
2023-03-06 01:16:55 +00:00
|
|
|
|
2023-02-20 03:27:35 +00:00
|
|
|
// If there's ever any audio popping, check here first. Some games are very sensitive to
|
|
|
|
// the remaining sample count and reporting a number that's too high here can lead to issues.
|
|
|
|
// Reporting a number that's too low can lead to audio lag in some games.
|
2023-11-13 00:40:02 +00:00
|
|
|
uint32_t ultramodern::get_remaining_audio_bytes() {
|
2023-02-20 03:27:35 +00:00
|
|
|
// Get the number of remaining buffered audio bytes.
|
2023-06-22 22:29:55 +00:00
|
|
|
uint32_t buffered_byte_count;
|
2023-10-23 19:32:30 +00:00
|
|
|
if (audio_callbacks.get_frames_remaining != nullptr) {
|
|
|
|
buffered_byte_count = audio_callbacks.get_frames_remaining() * 2 * sizeof(int16_t);
|
2023-06-22 22:29:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffered_byte_count = 100;
|
2023-02-20 03:27:35 +00:00
|
|
|
}
|
2023-06-22 22:29:55 +00:00
|
|
|
// Adjust the reported count to be some number of refreshes in the future, which helps ensure that
|
|
|
|
// there are enough samples even if the audio thread experiences a small amount of lag. This prevents
|
|
|
|
// audio popping on games that use the buffered audio byte count to determine how many samples
|
|
|
|
// to generate.
|
|
|
|
uint32_t samples_per_vi = (sample_rate / 60);
|
|
|
|
if (buffered_byte_count > static_cast<uint32_t>(buffer_offset_frames * sizeof(int16_t) * samples_per_vi)) {
|
|
|
|
buffered_byte_count -= static_cast<uint32_t>(buffer_offset_frames * sizeof(int16_t) * samples_per_vi);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffered_byte_count = 0;
|
|
|
|
}
|
|
|
|
return buffered_byte_count;
|
2023-02-20 03:27:35 +00:00
|
|
|
}
|