Skip to content

Commit

Permalink
Work on sound driver levels.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed Aug 25, 2023
1 parent 62c4686 commit e9b4f6d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
23 changes: 23 additions & 0 deletions apu/resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ class Resampler
r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0;
}

inline void dump(unsigned int num_samples)
{
if (space_filled() >= num_samples)
start = (start + num_samples) % buffer_size;
}

inline void add_silence(unsigned int num_samples)
{
if (space_empty() <= num_samples)
{
int new_end = (end + num_samples) % buffer_size;

if (new_end < end)
{
memset(buffer + end, 0, 2 * (buffer_size - end));
memset(buffer, 0, 2 * (num_samples - (buffer_size - end)));
}

memset(buffer + end, 0, 2 * num_samples);
end = new_end;
}
}

inline bool pull(int16_t *dst, int num_samples)
{
if (space_filled() < num_samples)
Expand Down
7 changes: 5 additions & 2 deletions common/audio/s9x_sound_driver_cubeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
bool S9xCubebSoundDriver::write_samples(int16_t *data, int samples)
{
bool retval = true;
if (samples > buffer.space_empty())
auto empty = buffer.space_empty();
if (samples > empty)
{
retval = false;
samples = buffer.space_empty();
buffer.dump(buffer.buffer_size / 2 - empty);
}

buffer.push(data, samples);

return retval;
Expand Down Expand Up @@ -83,6 +85,7 @@ long S9xCubebSoundDriver::data_callback(cubeb_stream *stream, void const *input_
auto zeroed_samples = nframes * 2 - avail;
memset(output_buffer, 0, zeroed_samples);
buffer.read((int16_t *)output_buffer + zeroed_samples, nframes * 2 - zeroed_samples);
buffer.add_silence(buffer.buffer_size / 2);
}
else
{
Expand Down
19 changes: 17 additions & 2 deletions common/audio/s9x_sound_driver_pulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ bool S9xPulseSoundDriver::open_device(int playback_rate, int buffer_size_ms)
ss.rate = playback_rate;

pa_buffer_attr buffer_attr;
buffer_attr.tlength = pa_usec_to_bytes(buffer_size_ms * 1000, &ss);
buffer_attr.tlength = 2 * pa_usec_to_bytes(buffer_size_ms * 1000, &ss);
buffer_attr.maxlength = buffer_attr.tlength * 2;
buffer_attr.minreq = pa_usec_to_bytes(3000, &ss);
buffer_attr.prebuf = -1;
buffer_attr.prebuf = buffer_attr.tlength / 2;

printf("PulseAudio sound driver initializing...\n");

Expand Down Expand Up @@ -189,8 +189,23 @@ bool S9xPulseSoundDriver::write_samples(int16_t *data, int samples)
size_t bytes = pa_stream_writable_size(stream);
unlock();

if (draining)
{
if (bytes > buffer_size / 2)
{
return false;
}
else
{
draining = false;
}
}

if (samples * 2 > bytes)
{
draining = true;
return false;
}

if (samples * 2 < bytes)
bytes = samples * 2;
Expand Down
1 change: 1 addition & 0 deletions common/audio/s9x_sound_driver_pulse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class S9xPulseSoundDriver : public S9xSoundDriver
void wait();

int buffer_size;
bool draining = false;
};

#endif /* __S9X_SOUND_DRIVER_PULSE_HPP */
10 changes: 8 additions & 2 deletions common/audio/s9x_sound_driver_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
bool S9xSDLSoundDriver::write_samples(int16_t *data, int samples)
{
bool retval = true;
if (samples > buffer.space_empty())
auto empty = buffer.space_empty();
if (samples > empty)
{
retval = false;
samples = buffer.space_empty();
buffer.dump(buffer.buffer_size / 2 - empty);
}
buffer.push(data, samples);

Expand All @@ -24,6 +25,11 @@ void S9xSDLSoundDriver::mix(unsigned char *output, int bytes)
{
if (buffer.avail() >= bytes >> 1)
buffer.read((int16_t *)output, bytes >> 1);
else
{
buffer.read((int16_t *)output, buffer.avail());
buffer.add_silence(buffer.buffer_size / 2);
}
}

S9xSDLSoundDriver::S9xSDLSoundDriver()
Expand Down
6 changes: 6 additions & 0 deletions qt/src/EmuApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ static void trackBufferLevel(int percent, QWidget *parent)
ticks = 0;
}

dialog->setFocusPolicy(Qt::FocusPolicy::NoFocus);

if (!dialog->isVisible())
{
dialog->setDisabled(true);
dialog->show();
dialog->setModal(false);
}
}
#endif

Expand Down

0 comments on commit e9b4f6d

Please sign in to comment.