godot/servers/audio/audio_server_sw.cpp

963 lines
25 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* audio_server_sw.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "audio_server_sw.h"
#include "globals.h"
#include "os/os.h"
struct _AudioDriverLock {
_AudioDriverLock() {
if (AudioDriverSW::get_singleton()) AudioDriverSW::get_singleton()->lock();
}
~_AudioDriverLock() {
if (AudioDriverSW::get_singleton()) AudioDriverSW::get_singleton()->unlock();
}
2014-02-10 01:10:30 +00:00
};
#define AUDIO_LOCK _AudioDriverLock _adlock;
AudioMixer *AudioServerSW::get_mixer() {
return mixer;
}
/* CALLBACKS */
void AudioServerSW::audio_mixer_chunk_callback(int p_frames) {
/*
2014-02-10 01:10:30 +00:00
for(List<Stream*>::Element *E=event_streams.front();E;E=E->next()) {
if (E->get()->active)
E->get()->audio_stream->mix(NULL,p_frames);
}
*/
}
void AudioServerSW::_mixer_callback(void *p_udata) {
AudioServerSW *self = (AudioServerSW *)p_udata;
for (List<Stream *>::Element *E = self->active_audio_streams.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
if (!E->get()->active)
continue;
EventStream *es = E->get()->event_stream;
2014-02-10 01:10:30 +00:00
if (!es)
continue;
es->update(self->mixer_step_usecs);
}
}
void AudioServerSW::driver_process_chunk(int p_frames, int32_t *p_buffer) {
2014-02-10 01:10:30 +00:00
int samples = p_frames * internal_buffer_channels;
2014-02-10 01:10:30 +00:00
for (int i = 0; i < samples; i++) {
internal_buffer[i] = 0;
2014-02-10 01:10:30 +00:00
}
while (voice_rb.commands_left()) {
2014-02-10 01:10:30 +00:00
VoiceRBSW::Command cmd = voice_rb.pop_command();
if (cmd.type == VoiceRBSW::Command::CMD_CHANGE_ALL_FX_VOLUMES) {
2014-02-10 01:10:30 +00:00
SelfList<Voice> *al = active_list.first();
while (al) {
2014-02-10 01:10:30 +00:00
Voice *v = al->self();
if (v->channel != AudioMixer::INVALID_CHANNEL) {
mixer->channel_set_volume(v->channel, v->volume * fx_volume_scale);
2014-02-10 01:10:30 +00:00
}
al = al->next();
2014-02-10 01:10:30 +00:00
}
continue;
}
if (!voice_owner.owns(cmd.voice))
continue;
Voice *v = voice_owner.get(cmd.voice);
switch (cmd.type) {
2014-02-10 01:10:30 +00:00
case VoiceRBSW::Command::CMD_NONE: {
} break;
case VoiceRBSW::Command::CMD_PLAY: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
2014-02-10 01:10:30 +00:00
mixer->channel_free(v->channel);
RID sample = cmd.play.sample;
if (!sample_manager->is_sample(sample))
continue;
v->channel = mixer->channel_alloc(sample);
v->volume = 1.0;
mixer->channel_set_volume(v->channel, fx_volume_scale);
if (v->channel == AudioMixer::INVALID_CHANNEL) {
2014-02-10 01:10:30 +00:00
#ifdef AUDIO_DEBUG
WARN_PRINT("AUDIO: all channels used, failed to allocate voice");
#endif
v->active = false;
2014-02-10 01:10:30 +00:00
break; // no voices left?
}
v->active = true; // this kind of ensures it works
2014-02-10 01:10:30 +00:00
if (!v->active_item.in_list())
active_list.add(&v->active_item);
} break;
case VoiceRBSW::Command::CMD_STOP: {
if (v->channel != AudioMixer::INVALID_CHANNEL) {
2014-02-10 01:10:30 +00:00
mixer->channel_free(v->channel);
if (v->active_item.in_list()) {
active_list.remove(&v->active_item);
}
}
v->active = false;
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_VOLUME: {
if (v->channel != AudioMixer::INVALID_CHANNEL) {
v->volume = cmd.volume.volume;
mixer->channel_set_volume(v->channel, cmd.volume.volume * fx_volume_scale);
2014-02-10 01:10:30 +00:00
}
} break;
case VoiceRBSW::Command::CMD_SET_PAN: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_pan(v->channel, cmd.pan.pan, cmd.pan.depth, cmd.pan.height);
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_FILTER: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_filter(v->channel, (AudioMixer::FilterType)cmd.filter.type, cmd.filter.cutoff, cmd.filter.resonance, cmd.filter.gain);
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_CHORUS: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_chorus(v->channel, cmd.chorus.send);
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_REVERB: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_reverb(v->channel, (AudioMixer::ReverbRoomType)cmd.reverb.room, cmd.reverb.send);
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_MIX_RATE: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_mix_rate(v->channel, cmd.mix_rate.mix_rate);
2014-02-10 01:10:30 +00:00
} break;
case VoiceRBSW::Command::CMD_SET_POSITIONAL: {
if (v->channel != AudioMixer::INVALID_CHANNEL)
mixer->channel_set_positional(v->channel, cmd.positional.positional);
2014-02-10 01:10:30 +00:00
} break;
default: {}
}
}
mixer->mix(internal_buffer, p_frames);
2014-02-10 01:10:30 +00:00
//uint64_t stepsize=mixer->get_step_usecs();
for (List<Stream *>::Element *E = active_audio_streams.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
ERR_CONTINUE(!E->get()->active); // bug?
AudioStream *as = E->get()->audio_stream;
2014-02-10 01:10:30 +00:00
if (!as)
continue;
int channels = as->get_channel_count();
if (channels == 0)
2014-02-10 01:10:30 +00:00
continue; // does not want mix
if (!as->mix(stream_buffer, p_frames))
2014-02-10 01:10:30 +00:00
continue; //nothing was mixed!!
int32_t stream_vol_scale = (stream_volume * stream_volume_scale * E->get()->volume_scale) * (1 << STREAM_SCALE_BITS);
2014-02-10 01:10:30 +00:00
#define STRSCALE(m_val) (((m_val >> STREAM_SCALE_BITS) * stream_vol_scale) >> 8)
switch (internal_buffer_channels) {
2014-02-10 01:10:30 +00:00
case 2: {
switch (channels) {
2014-02-10 01:10:30 +00:00
case 1: {
for (int i = 0; i < p_frames; i++) {
2014-02-10 01:10:30 +00:00
internal_buffer[(i << 1) + 0] += STRSCALE(stream_buffer[i]);
internal_buffer[(i << 1) + 1] += STRSCALE(stream_buffer[i]);
2014-02-10 01:10:30 +00:00
}
} break;
case 2: {
for (int i = 0; i < p_frames * 2; i++) {
2014-02-10 01:10:30 +00:00
internal_buffer[i] += STRSCALE(stream_buffer[i]);
2014-02-10 01:10:30 +00:00
}
} break;
case 4: {
for (int i = 0; i < p_frames; i++) {
2014-02-10 01:10:30 +00:00
internal_buffer[(i << 2) + 0] += STRSCALE((stream_buffer[(i << 2) + 0] + stream_buffer[(i << 2) + 2]) >> 1);
internal_buffer[(i << 2) + 1] += STRSCALE((stream_buffer[(i << 2) + 1] + stream_buffer[(i << 2) + 3]) >> 1);
2014-02-10 01:10:30 +00:00
}
} break;
}
break;
2014-02-10 01:10:30 +00:00
} break;
case 4: {
switch (channels) {
2014-02-10 01:10:30 +00:00
case 1: {
for (int i = 0; i < p_frames; i++) {
2014-02-10 01:10:30 +00:00
internal_buffer[(i << 2) + 0] += STRSCALE(stream_buffer[i]);
internal_buffer[(i << 2) + 1] += STRSCALE(stream_buffer[i]);
internal_buffer[(i << 2) + 2] += STRSCALE(stream_buffer[i]);
internal_buffer[(i << 2) + 3] += STRSCALE(stream_buffer[i]);
2014-02-10 01:10:30 +00:00
}
} break;
case 2: {
for (int i = 0; i < p_frames * 2; i++) {
2014-02-10 01:10:30 +00:00
internal_buffer[(i << 2) + 0] += STRSCALE(stream_buffer[(i << 1) + 0]);
internal_buffer[(i << 2) + 1] += STRSCALE(stream_buffer[(i << 1) + 1]);
internal_buffer[(i << 2) + 2] += STRSCALE(stream_buffer[(i << 1) + 0]);
internal_buffer[(i << 2) + 3] += STRSCALE(stream_buffer[(i << 1) + 1]);
2014-02-10 01:10:30 +00:00
}
} break;
case 4: {
for (int i = 0; i < p_frames * 4; i++) {
internal_buffer[i] += STRSCALE(stream_buffer[i]);
2014-02-10 01:10:30 +00:00
}
} break;
}
break;
2014-02-10 01:10:30 +00:00
} break;
case 6: {
} break;
}
#undef STRSCALE
}
SelfList<Voice> *activeE = active_list.first();
while (activeE) {
2014-02-10 01:10:30 +00:00
SelfList<Voice> *activeN = activeE->next();
if (activeE->self()->channel == AudioMixer::INVALID_CHANNEL || !mixer->channel_is_valid(activeE->self()->channel)) {
2014-02-10 01:10:30 +00:00
active_list.remove(activeE);
activeE->self()->active = false;
2014-02-10 01:10:30 +00:00
}
activeE = activeN;
2014-02-10 01:10:30 +00:00
}
uint32_t peak = 0;
for (int i = 0; i < samples; i++) {
2014-02-10 01:10:30 +00:00
//clamp to (1<<24) using branchless code
int32_t in = internal_buffer[i];
#ifdef DEBUG_ENABLED
{
int mask = (in >> (32 - 1));
uint32_t p = (in + mask) ^ mask;
if (p > peak)
peak = p;
2014-02-10 01:10:30 +00:00
}
#endif
int32_t lo = -0x800000, hi = 0x7FFFFF;
lo -= in;
hi -= in;
2014-02-10 01:10:30 +00:00
in += (lo & ((lo < 0) - 1)) + (hi & ((hi > 0) - 1));
p_buffer[i] = in << 8;
2014-02-10 01:10:30 +00:00
}
if (peak > max_peak)
max_peak = peak;
2014-02-10 01:10:30 +00:00
}
void AudioServerSW::driver_process(int p_frames, int32_t *p_buffer) {
2014-02-10 01:10:30 +00:00
_output_delay = p_frames / double(AudioDriverSW::get_singleton()->get_mix_rate());
2014-02-10 01:10:30 +00:00
//process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
int todo = p_frames;
while (todo) {
2014-02-10 01:10:30 +00:00
int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
driver_process_chunk(tomix, p_buffer);
p_buffer += tomix;
todo -= tomix;
2014-02-10 01:10:30 +00:00
}
}
/* SAMPLE API */
RID AudioServerSW::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
AUDIO_LOCK
return sample_manager->sample_create(p_format, p_stereo, p_length);
2014-02-10 01:10:30 +00:00
}
void AudioServerSW::sample_set_description(RID p_sample, const String &p_description) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_description(p_sample, p_description);
2014-02-10 01:10:30 +00:00
}
String AudioServerSW::sample_get_description(RID p_sample) const {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
return sample_manager->sample_get_description(p_sample);
}
AS::SampleFormat AudioServerSW::sample_get_format(RID p_sample) const {
//AUDIO_LOCK
return sample_manager->sample_get_format(p_sample);
}
bool AudioServerSW::sample_is_stereo(RID p_sample) const {
//AUDIO_LOCK
return sample_manager->sample_is_stereo(p_sample);
}
int AudioServerSW::sample_get_length(RID p_sample) const {
2014-02-10 01:10:30 +00:00
///AUDIO_LOCK
return sample_manager->sample_get_length(p_sample);
}
const void *AudioServerSW::sample_get_data_ptr(RID p_sample) const {
2014-02-10 01:10:30 +00:00
///AUDIO_LOCK
return sample_manager->sample_get_data_ptr(p_sample);
}
void AudioServerSW::sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_data(p_sample, p_buffer);
2014-02-10 01:10:30 +00:00
}
DVector<uint8_t> AudioServerSW::sample_get_data(RID p_sample) const {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
return sample_manager->sample_get_data(p_sample);
}
void AudioServerSW::sample_set_mix_rate(RID p_sample, int p_rate) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_mix_rate(p_sample, p_rate);
2014-02-10 01:10:30 +00:00
}
int AudioServerSW::sample_get_mix_rate(RID p_sample) const {
AUDIO_LOCK
return sample_manager->sample_get_mix_rate(p_sample);
}
void AudioServerSW::sample_set_loop_format(RID p_sample, SampleLoopFormat p_format) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_loop_format(p_sample, p_format);
2014-02-10 01:10:30 +00:00
}
AS::SampleLoopFormat AudioServerSW::sample_get_loop_format(RID p_sample) const {
AUDIO_LOCK
return sample_manager->sample_get_loop_format(p_sample);
}
void AudioServerSW::sample_set_loop_begin(RID p_sample, int p_pos) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_loop_begin(p_sample, p_pos);
2014-02-10 01:10:30 +00:00
}
int AudioServerSW::sample_get_loop_begin(RID p_sample) const {
AUDIO_LOCK
return sample_manager->sample_get_loop_begin(p_sample);
}
void AudioServerSW::sample_set_loop_end(RID p_sample, int p_pos) {
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
sample_manager->sample_set_loop_end(p_sample, p_pos);
2014-02-10 01:10:30 +00:00
}
int AudioServerSW::sample_get_loop_end(RID p_sample) const {
AUDIO_LOCK
return sample_manager->sample_get_loop_end(p_sample);
}
/* VOICE API */
RID AudioServerSW::voice_create() {
Voice *v = memnew(Voice);
v->channel = AudioMixer::INVALID_CHANNEL;
2014-02-10 01:10:30 +00:00
AUDIO_LOCK
return voice_owner.make_rid(v);
}
void AudioServerSW::voice_play(RID p_voice, RID p_sample) {
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(!v);
v->active = true; // force actvive (will be disabled later i gues..)
2014-02-10 01:10:30 +00:00
//stop old, start new
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_PLAY;
cmd.voice = p_voice;
cmd.play.sample = p_sample;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_volume(RID p_voice, float p_volume) {
2014-02-10 01:10:30 +00:00
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_VOLUME;
cmd.voice = p_voice;
cmd.volume.volume = p_volume;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_pan(RID p_voice, float p_pan, float p_depth, float p_height) {
2014-02-10 01:10:30 +00:00
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_PAN;
cmd.voice = p_voice;
cmd.pan.pan = p_pan;
cmd.pan.depth = p_depth;
cmd.pan.height = p_height;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain) {
2014-02-10 01:10:30 +00:00
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_FILTER;
cmd.voice = p_voice;
cmd.filter.type = p_type;
cmd.filter.cutoff = p_cutoff;
cmd.filter.resonance = p_resonance;
cmd.filter.gain = p_gain;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_chorus(RID p_voice, float p_chorus) {
2014-02-10 01:10:30 +00:00
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_CHORUS;
cmd.voice = p_voice;
cmd.chorus.send = p_chorus;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb) {
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_REVERB;
cmd.voice = p_voice;
cmd.reverb.room = p_room_type;
cmd.reverb.send = p_reverb;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_mix_rate(RID p_voice, int p_mix_rate) {
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_MIX_RATE;
cmd.voice = p_voice;
cmd.mix_rate.mix_rate = p_mix_rate;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
void AudioServerSW::voice_set_positional(RID p_voice, bool p_positional) {
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_SET_POSITIONAL;
cmd.voice = p_voice;
cmd.positional.positional = p_positional;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
float AudioServerSW::voice_get_volume(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_volume(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_pan(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_pan(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_pan_depth(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_pan_depth(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_pan_height(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_pan_height(v->channel);
2014-02-10 01:10:30 +00:00
}
AS::FilterType AudioServerSW::voice_get_filter_type(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, AS::FILTER_NONE);
return (AS::FilterType)mixer->channel_get_filter_type(v->channel);
}
float AudioServerSW::voice_get_filter_cutoff(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_filter_cutoff(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_filter_resonance(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_filter_resonance(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_chorus(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_chorus(v->channel);
2014-02-10 01:10:30 +00:00
}
AS::ReverbRoomType AudioServerSW::voice_get_reverb_type(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, REVERB_SMALL);
return (AS::ReverbRoomType)mixer->channel_get_reverb_type(v->channel);
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::voice_get_reverb(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_reverb(v->channel);
2014-02-10 01:10:30 +00:00
}
int AudioServerSW::voice_get_mix_rate(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_get_mix_rate(v->channel);
2014-02-10 01:10:30 +00:00
}
bool AudioServerSW::voice_is_positional(RID p_voice) const {
AUDIO_LOCK
Voice *v = voice_owner.get(p_voice);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!v, 0);
return mixer->channel_is_positional(v->channel);
2014-02-10 01:10:30 +00:00
}
void AudioServerSW::voice_stop(RID p_voice) {
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_STOP;
cmd.voice = p_voice;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
//return mixer->channel_free( v->channel );
}
bool AudioServerSW::voice_is_active(RID p_voice) const {
Voice *v = voice_owner.get(p_voice);
ERR_FAIL_COND_V(!v, false);
2014-02-10 01:10:30 +00:00
return v->active;
}
/* STREAM API */
RID AudioServerSW::audio_stream_create(AudioStream *p_stream) {
AUDIO_LOCK
2014-02-10 01:10:30 +00:00
Stream *s = memnew(Stream);
s->audio_stream = p_stream;
s->event_stream = NULL;
s->active = false;
s->E = NULL;
s->volume_scale = 1.0;
2014-02-10 01:10:30 +00:00
p_stream->set_mix_rate(AudioDriverSW::get_singleton()->get_mix_rate());
return stream_owner.make_rid(s);
}
RID AudioServerSW::event_stream_create(EventStream *p_stream) {
AUDIO_LOCK
Stream *s = memnew(Stream);
s->audio_stream = NULL;
s->event_stream = p_stream;
s->active = false;
s->E = NULL;
s->volume_scale = 1.0;
2014-02-10 01:10:30 +00:00
//p_stream->set_mix_rate(AudioDriverSW::get_singleton()->get_mix_rate());
return stream_owner.make_rid(s);
}
void AudioServerSW::stream_set_active(RID p_stream, bool p_active) {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND(!s);
_THREAD_SAFE_METHOD_
2014-02-10 01:10:30 +00:00
if (s->active == p_active)
2014-02-10 01:10:30 +00:00
return;
AUDIO_LOCK;
s->active = p_active;
if (p_active)
s->E = active_audio_streams.push_back(s);
else {
active_audio_streams.erase(s->E);
s->E = NULL;
2014-02-10 01:10:30 +00:00
}
}
bool AudioServerSW::stream_is_active(RID p_stream) const {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND_V(!s, false);
2014-02-10 01:10:30 +00:00
return s->active;
}
void AudioServerSW::stream_set_volume_scale(RID p_stream, float p_scale) {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND(!s);
s->volume_scale = p_scale;
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::stream_set_volume_scale(RID p_stream) const {
Stream *s = stream_owner.get(p_stream);
ERR_FAIL_COND_V(!s, 0);
2014-02-10 01:10:30 +00:00
return s->volume_scale;
}
void AudioServerSW::free(RID p_id) {
if (voice_owner.owns(p_id)) {
2014-02-10 01:10:30 +00:00
Voice *v = voice_owner.get(p_id);
AUDIO_LOCK
mixer->channel_free(v->channel);
2014-02-10 01:10:30 +00:00
voice_owner.free(p_id);
memdelete(v);
} else if (stream_owner.owns(p_id)) {
Stream *s = stream_owner.get(p_id);
2014-02-10 01:10:30 +00:00
if (s->active) {
stream_set_active(p_id, false);
2014-02-10 01:10:30 +00:00
}
memdelete(s);
stream_owner.free(p_id);
} else if (sample_manager->is_sample(p_id)) {
AUDIO_LOCK
sample_manager->free(p_id);
}
}
void AudioServerSW::_thread_func(void *self) {
2016-01-31 23:22:38 +00:00
Thread::set_name("AudioServerSW");
AudioServerSW *as = (AudioServerSW *)self;
2014-02-10 01:10:30 +00:00
while (!as->exit_update_thread) {
as->_update_streams(true);
OS::get_singleton()->delay_usec(5000);
}
}
void AudioServerSW::init() {
int latency = GLOBAL_DEF("audio/mixer_latency", 10);
internal_buffer_channels = 2; // read from driver
internal_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * internal_buffer_channels);
stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
2014-02-10 01:10:30 +00:00
AudioMixerSW::MixChannels mix_chans = AudioMixerSW::MIX_STEREO;
switch (AudioDriverSW::get_singleton()->get_output_format()) {
2014-02-10 01:10:30 +00:00
case AudioDriverSW::OUTPUT_MONO:
case AudioDriverSW::OUTPUT_STEREO:
mix_chans = AudioMixerSW::MIX_STEREO;
2014-02-10 01:10:30 +00:00
break;
case AudioDriverSW::OUTPUT_QUAD:
case AudioDriverSW::OUTPUT_5_1:
mix_chans = AudioMixerSW::MIX_QUAD;
2014-02-10 01:10:30 +00:00
break;
}
mixer = memnew(AudioMixerSW(sample_manager, latency, AudioDriverSW::get_singleton()->get_mix_rate(), mix_chans, mixer_use_fx, mixer_interp, _mixer_callback, this));
mixer_step_usecs = mixer->get_step_usecs();
2014-02-10 01:10:30 +00:00
_output_delay = 0;
stream_volume = 0.3;
2014-02-10 01:10:30 +00:00
// start the audio driver
if (AudioDriverSW::get_singleton())
AudioDriverSW::get_singleton()->start();
#ifndef NO_THREADS
exit_update_thread = false;
thread = Thread::create(_thread_func, this);
2014-02-10 01:10:30 +00:00
#endif
}
void AudioServerSW::finish() {
#ifndef NO_THREADS
exit_update_thread = true;
2014-02-10 01:10:30 +00:00
Thread::wait_to_finish(thread);
memdelete(thread);
#endif
if (AudioDriverSW::get_singleton())
AudioDriverSW::get_singleton()->finish();
memdelete_arr(internal_buffer);
memdelete_arr(stream_buffer);
memdelete(mixer);
}
void AudioServerSW::_update_streams(bool p_thread) {
_THREAD_SAFE_METHOD_
for (List<Stream *>::Element *E = active_audio_streams.front(); E;) { //stream might be removed durnig this callback
List<Stream *>::Element *N = E->next();
2014-02-10 01:10:30 +00:00
if (E->get()->audio_stream && p_thread == E->get()->audio_stream->can_update_mt())
E->get()->audio_stream->update();
E = N;
2014-02-10 01:10:30 +00:00
}
}
void AudioServerSW::update() {
_update_streams(false);
#ifdef NO_THREADS
2014-02-10 01:10:30 +00:00
_update_streams(true);
#endif
}
void AudioServerSW::lock() {
AudioDriverSW::get_singleton()->lock();
}
void AudioServerSW::unlock() {
AudioDriverSW::get_singleton()->unlock();
}
int AudioServerSW::get_default_mix_rate() const {
return AudioDriverSW::get_singleton()->get_mix_rate();
}
int AudioServerSW::get_default_channel_count() const {
return internal_buffer_channels;
}
void AudioServerSW::set_mixer_params(AudioMixerSW::InterpolationType p_interp, bool p_use_fx) {
mixer_interp = p_interp;
mixer_use_fx = p_use_fx;
2014-02-10 01:10:30 +00:00
}
void AudioServerSW::set_stream_global_volume_scale(float p_volume) {
stream_volume_scale = p_volume;
2014-02-10 01:10:30 +00:00
}
float AudioServerSW::get_stream_global_volume_scale() const {
return stream_volume_scale;
}
void AudioServerSW::set_fx_global_volume_scale(float p_volume) {
fx_volume_scale = p_volume;
2014-02-10 01:10:30 +00:00
//mixer->set_mixer_volume(fx_volume_scale);
VoiceRBSW::Command cmd;
cmd.type = VoiceRBSW::Command::CMD_CHANGE_ALL_FX_VOLUMES;
cmd.voice = RID();
cmd.volume.volume = p_volume;
2014-02-10 01:10:30 +00:00
voice_rb.push_command(cmd);
}
float AudioServerSW::get_fx_global_volume_scale() const {
return fx_volume_scale;
}
void AudioServerSW::set_event_voice_global_volume_scale(float p_volume) {
event_voice_volume_scale = p_volume;
2014-02-10 01:10:30 +00:00
//mixer->set_mixer_volume(event_voice_volume_scale);
}
float AudioServerSW::get_event_voice_global_volume_scale() const {
return event_voice_volume_scale;
}
double AudioServerSW::get_output_delay() const {
return _output_delay + AudioDriverSW::get_singleton()->get_latency();
}
2014-02-10 01:10:30 +00:00
double AudioServerSW::get_mix_time() const {
return AudioDriverSW::get_singleton()->get_mix_time();
}
uint32_t AudioServerSW::read_output_peak() const {
uint32_t val = max_peak;
uint32_t *p = (uint32_t *)&max_peak;
*p = 0;
2014-02-10 01:10:30 +00:00
return val;
}
AudioServerSW::AudioServerSW(SampleManagerSW *p_sample_manager) {
sample_manager = p_sample_manager;
String interp = GLOBAL_DEF("audio/mixer_interp", "linear");
Globals::get_singleton()->set_custom_property_info("audio/mixer_interp", PropertyInfo(Variant::STRING, "audio/mixer_interp", PROPERTY_HINT_ENUM, "raw,linear,cubic"));
if (interp == "raw")
mixer_interp = AudioMixerSW::INTERPOLATION_RAW;
else if (interp == "cubic")
mixer_interp = AudioMixerSW::INTERPOLATION_CUBIC;
2014-02-10 01:10:30 +00:00
else
mixer_interp = AudioMixerSW::INTERPOLATION_LINEAR;
mixer_use_fx = GLOBAL_DEF("audio/use_chorus_reverb", true);
stream_volume_scale = GLOBAL_DEF("audio/stream_volume_scale", 1.0);
fx_volume_scale = GLOBAL_DEF("audio/fx_volume_scale", 1.0);
event_voice_volume_scale = GLOBAL_DEF("audio/event_voice_volume_scale", 0.5);
max_peak = 0;
2014-02-10 01:10:30 +00:00
}
AudioServerSW::~AudioServerSW() {
}
AudioDriverSW *AudioDriverSW::singleton = NULL;
2014-02-10 01:10:30 +00:00
AudioDriverSW *AudioDriverSW::get_singleton() {
return singleton;
}
void AudioDriverSW::set_singleton() {
singleton = this;
2014-02-10 01:10:30 +00:00
}
void AudioDriverSW::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) {
2014-02-10 01:10:30 +00:00
AudioServerSW *audio_server = static_cast<AudioServerSW *>(AudioServer::get_singleton());
2014-02-10 01:10:30 +00:00
if (p_update_mix_time)
update_mix_time(p_frames);
audio_server->driver_process(p_frames, p_buffer);
2014-02-10 01:10:30 +00:00
}
void AudioDriverSW::update_mix_time(int p_frames) {
_mix_amount += p_frames;
_last_mix_time = OS::get_singleton()->get_ticks_usec();
2014-02-10 01:10:30 +00:00
}
double AudioDriverSW::get_mix_time() const {
double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0;
total += _mix_amount / (double)get_mix_rate();
2014-02-10 01:10:30 +00:00
return total;
}
AudioDriverSW::AudioDriverSW() {
_last_mix_time = 0;
_mix_amount = 0;
2014-02-10 01:10:30 +00:00
}
AudioDriverSW *AudioDriverManagerSW::drivers[MAX_DRIVERS];
int AudioDriverManagerSW::driver_count = 0;
2014-02-10 01:10:30 +00:00
void AudioDriverManagerSW::add_driver(AudioDriverSW *p_driver) {
ERR_FAIL_COND(driver_count >= MAX_DRIVERS);
drivers[driver_count++] = p_driver;
2014-02-10 01:10:30 +00:00
}
int AudioDriverManagerSW::get_driver_count() {
return driver_count;
}
AudioDriverSW *AudioDriverManagerSW::get_driver(int p_driver) {
ERR_FAIL_INDEX_V(p_driver, driver_count, NULL);
2014-02-10 01:10:30 +00:00
return drivers[p_driver];
}