Skip to content

Commit

Permalink
Avoid copying stream buffers senselessly
Browse files Browse the repository at this point in the history
Should result in a slightly better performance.
  • Loading branch information
YuriSizov committed May 23, 2024
1 parent 2c414eb commit 46298b1
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ __pycache__/

### IDEs and code editors ###

# Visual Studio
.vs/
*.sln
*.vcxproj*

# Visual Studio Code
.vscode/
*.code-workspace
Expand Down
4 changes: 2 additions & 2 deletions src/chip/channels/siopm_channel_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ void SiOPMChannelSampler::buffer(int p_length) {
SiOPMStream *stream = _streams[i] ? _streams[i] : _sound_chip->get_stream_slot(i);
if (stream) {
double volume = _volumes[i] * _expression * _sound_chip->get_sampler_volume();
stream->write_from_vector(_sample_data->get_wave_data(), _sample_index, _buffer_index, processed, volume, _sample_pan, _sample_data->get_channel_count());
stream->write_from_vector(&_sample_data->get_wave_data(), _sample_index, _buffer_index, processed, volume, _sample_pan, _sample_data->get_channel_count());

Check failure on line 123 in src/chip/channels/siopm_channel_sampler.cpp

View workflow job for this annotation

GitHub Actions / Compile and upload Linux version / Compile and upload Linux version

taking address of rvalue [-fpermissive]
}
}
}
} else {
SiOPMStream *stream = _streams[0] ? _streams[0] : _sound_chip->get_output_stream();

double volume = _volumes[0] * _expression * _sound_chip->get_sampler_volume();
stream->write_from_vector(_sample_data->get_wave_data(), _sample_index, _buffer_index, processed, volume, _sample_pan, _sample_data->get_channel_count());
stream->write_from_vector(&_sample_data->get_wave_data(), _sample_index, _buffer_index, processed, volume, _sample_pan, _sample_data->get_channel_count());

Check failure on line 131 in src/chip/channels/siopm_channel_sampler.cpp

View workflow job for this annotation

GitHub Actions / Compile and upload Linux version / Compile and upload Linux version

taking address of rvalue [-fpermissive]
}

_sample_index += processed;
Expand Down
8 changes: 2 additions & 6 deletions src/chip/siopm_sound_chip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
#include "chip/siopm_operator_params.h"
#include "chip/siopm_stream.h"

Vector<double> SiOPMSoundChip::get_output_buffer() const {
return output_stream->get_buffer();
}

void SiOPMSoundChip::set_output_buffer(Vector<double> p_buffer) {
output_stream->set_buffer(p_buffer);
Vector<double> *SiOPMSoundChip::get_output_buffer_ptr() {
return output_stream->get_buffer_ptr();
}

int SiOPMSoundChip::get_channel_count() const {
Expand Down
3 changes: 1 addition & 2 deletions src/chip/siopm_sound_chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class SiOPMSoundChip : public Object {
SinglyLinkedList<int> *get_zero_buffer() const { return zero_buffer; }

SiOPMStream *get_output_stream() const { return output_stream; }
Vector<double> get_output_buffer() const;
void set_output_buffer(Vector<double> p_buffer);
Vector<double> *get_output_buffer_ptr();
int get_channel_count() const;

SiOPMStream *get_stream_slot(int p_slot) const { return stream_slot[p_slot]; }
Expand Down
18 changes: 9 additions & 9 deletions src/chip/siopm_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void SiOPMStream::write_stereo(SinglyLinkedList<int> *p_left, SinglyLinkedList<i
}
}

void SiOPMStream::write_from_vector(Vector<double> p_data, int p_start_data, int p_start_buffer, int p_length, double p_volume, int p_pan, int p_sample_channel_count) {
void SiOPMStream::write_from_vector(Vector<double> *p_data, int p_start_data, int p_start_buffer, int p_length, double p_volume, int p_pan, int p_sample_channel_count) {
double volume = p_volume;

if (channels == 2) {
Expand All @@ -120,10 +120,10 @@ void SiOPMStream::write_from_vector(Vector<double> p_data, int p_start_data, int
int buffer_size = (p_start_data + p_length) << 1;

for (int j = p_start_data << 1, i = p_start_buffer << 1; j < buffer_size;) {
buffer.write[i] += p_data[j] * volume_left;
buffer.write[i] += (*p_data)[j] * volume_left;
j++;
i++;
buffer.write[i] += p_data[j] * volume_right;
buffer.write[i] += (*p_data)[j] * volume_right;
j++;
i++;
}
Expand All @@ -133,9 +133,9 @@ void SiOPMStream::write_from_vector(Vector<double> p_data, int p_start_data, int
int buffer_size = p_start_data + p_length;

for (int j = p_start_data, i = p_start_buffer << 1; j < buffer_size; j++) {
buffer.write[i] += p_data[j] * volume_left;
buffer.write[i] += (*p_data)[j] * volume_left;
i++;
buffer.write[i] += p_data[j] * volume_right;
buffer.write[i] += (*p_data)[j] * volume_right;
i++;
}
}
Expand All @@ -145,19 +145,19 @@ void SiOPMStream::write_from_vector(Vector<double> p_data, int p_start_data, int
int buffer_size = (p_start_data + p_length) << 1;

for (int j = p_start_data << 1, i = p_start_buffer << 1; j < buffer_size;) {
buffer.write[i] += (p_data[j] + p_data[j + 1]) * volume;
buffer.write[i] += ((*p_data)[j] + (*p_data)[j + 1]) * volume;
i++;
buffer.write[i] += (p_data[j] + p_data[j + 1]) * volume;
buffer.write[i] += ((*p_data)[j] + (*p_data)[j + 1]) * volume;
i++;
j += 2;
}
} else { // mono data to mono buffer
int buffer_size = p_start_data + p_length;

for (int j = p_start_data, i = p_start_buffer << 1; j < buffer_size; j++) {
buffer.write[i] += p_data[j] * volume;
buffer.write[i] += (*p_data)[j] * volume;
i++;
buffer.write[i] += p_data[j] * volume;
buffer.write[i] += (*p_data)[j] * volume;
i++;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/chip/siopm_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SiOPMStream {
void set_channel_count(int p_value) { channels = p_value; }

Vector<double> get_buffer() const { return buffer; }
Vector<double> *get_buffer_ptr() { return &buffer; }
void set_buffer(Vector<double> p_buffer) { buffer = p_buffer; }

void resize(int p_length);
Expand All @@ -31,7 +32,7 @@ class SiOPMStream {

void write(SinglyLinkedList<int> *p_data, int p_start, int p_length, double p_volume, int p_pan);
void write_stereo(SinglyLinkedList<int> *p_left, SinglyLinkedList<int> *p_right, int p_start, int p_length, double p_volume, int p_pan);
void write_from_vector(Vector<double> p_data, int p_start_data, int p_start_buffer, int p_length, double p_volume, int p_pan, int p_sample_channel_count);
void write_from_vector(Vector<double> *p_data, int p_start_data, int p_start_buffer, int p_length, double p_volume, int p_pan, int p_sample_channel_count);

SiOPMStream() {}
~SiOPMStream() {}
Expand Down
6 changes: 2 additions & 4 deletions src/effector/si_effect_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ int SiEffectStream::prepare_process() {
}

int SiEffectStream::process(int p_start_idx, int p_length, bool p_write_in_stream) {
Vector<double> buffer = _stream->get_buffer();
Vector<double> *buffer = _stream->get_buffer_ptr();
int channel_count = _stream->get_channel_count();

for (int i = 0; i < _chain.size(); i++) {
channel_count = _chain[i]->process(channel_count, &buffer, p_start_idx, p_length);
channel_count = _chain[i]->process(channel_count, buffer, p_start_idx, p_length);
}

_stream->set_buffer(buffer);

if (p_write_in_stream) {
if (_has_effect_send) {
for (int i = 0; i < SiOPMSoundChip::STREAM_SEND_SIZE; i++) {
Expand Down
10 changes: 4 additions & 6 deletions src/effector/si_effector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,11 @@ void SiEffector::end_process() {
if (effect->is_outputting_directly()) {
effect->process(0, _sound_chip->get_buffer_length(), false);

// TODO: Is this the best way to do this — to copy the buffer over?
Vector<double> buffer = effect->get_stream()->get_buffer();
Vector<double> output = _sound_chip->get_output_buffer();
for (int j = 0; j < output.size(); j++) {
output.write[j] += buffer[j];
Vector<double> *buffer = effect->get_stream()->get_buffer_ptr();
Vector<double> *output = _sound_chip->get_output_stream()->get_buffer_ptr();
for (int j = 0; j < output->size(); j++) {
output->write[j] += (*buffer)[j];
}
_sound_chip->set_output_buffer(output);
} else {
effect->process(0, _sound_chip->get_buffer_length(), true);
}
Expand Down
14 changes: 7 additions & 7 deletions src/sion_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,16 @@ bool SiONDriver::_rendering() {
_render_buffer.resize_zeroed(_render_buffer_index + buffer_extension);
}

// Copy the output.
Vector<double> output_buffer = sound_chip->get_output_buffer();
// Read the output.
Vector<double> *output_buffer = sound_chip->get_output_buffer_ptr();

if (_render_buffer_channel_count == 2) {
for (int i = 0, j = _render_buffer_index; i < rendering_length; i++, j++) {
_render_buffer.write[j] = output_buffer[i];
_render_buffer.write[j] = (*output_buffer)[i];
}
} else {
for (int i = 0, j = _render_buffer_index; i < rendering_length; i += 2, j++) {
_render_buffer.write[j] = output_buffer[i];
_render_buffer.write[j] = (*output_buffer)[i];
}
}

Expand Down Expand Up @@ -464,9 +464,9 @@ void SiONDriver::_streaming() {
_performance_stats.average_processing_time = _performance_stats.total_processing_time * _performance_stats.total_processing_time_ratio;

// Write samples.
Vector<double> output_buffer = sound_chip->get_output_buffer();
for (int i = 0; i < output_buffer.size(); i += 2) {
stream_buffer.push_back(Vector2(output_buffer[i], output_buffer[i + 1]));
Vector<double> *output_buffer = sound_chip->get_output_buffer_ptr();
for (int i = 0; i < output_buffer->size(); i += 2) {
stream_buffer.push_back(Vector2((*output_buffer)[i], (*output_buffer)[i + 1]));
}
_audio_playback->push_buffer(stream_buffer);

Expand Down

0 comments on commit 46298b1

Please sign in to comment.