Skip to content

Commit

Permalink
Qt port.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed Aug 25, 2023
1 parent 19d0016 commit 1b13250
Show file tree
Hide file tree
Showing 100 changed files with 21,929 additions and 97 deletions.
2 changes: 1 addition & 1 deletion apu/apu.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void S9xLandSamples (void);
void S9xClearSamples (void);
bool8 S9xMixSamples (uint8 *, int);
void S9xSetSamplesAvailableCallback (apu_callback, void *);
void S9xUpdateDynamicRate (int, int);
void S9xUpdateDynamicRate (int empty = 1, int buffer_size = 2);

#define DSP_INTERPOLATION_NONE 0
#define DSP_INTERPOLATION_LINEAR 1
Expand Down
118 changes: 118 additions & 0 deletions common/audio/s9x_sound_driver_cubeb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/

#include "s9x_sound_driver_cubeb.hpp"
#include <cstdio>

void S9xCubebSoundDriver::write_samples(int16_t *data, int samples)
{
if (samples > buffer.space_empty())
samples = buffer.space_empty();
buffer.push(data, samples);
}

S9xCubebSoundDriver::S9xCubebSoundDriver()
{
}

S9xCubebSoundDriver::~S9xCubebSoundDriver()
{
deinit();
}

void S9xCubebSoundDriver::init()
{
if (!context)
cubeb_init(&context, "Snes9x", nullptr);
stop();
}

void S9xCubebSoundDriver::deinit()
{
stop();
if (stream)
{
cubeb_stream_destroy(stream);
stream = nullptr;
}

if (context)
{
cubeb_destroy(context);
context = nullptr;
}
}

void S9xCubebSoundDriver::start()
{
if (stream)
cubeb_stream_start(stream);
}

void S9xCubebSoundDriver::stop()
{
if (stream)
cubeb_stream_stop(stream);
}

void state_callback(cubeb_stream *stream, void *user_ptr, cubeb_state state)
{
}

long data_callback(cubeb_stream *stream, void *user_ptr,
void const *input_buffer,
void *output_buffer, long nframes)
{
return ((S9xCubebSoundDriver *)user_ptr)->data_callback(stream, input_buffer, output_buffer, nframes);
}

long S9xCubebSoundDriver::data_callback(cubeb_stream *stream, void const *input_buffer, void *output_buffer, long nframes)
{
buffer.read((int16_t *)output_buffer, nframes * 2);
return nframes;
}

bool S9xCubebSoundDriver::open_device(int playback_rate, int buffer_size)
{
cubeb_stream_params params{};
params.channels = 2;
params.format = CUBEB_SAMPLE_S16LE;
params.layout = CUBEB_LAYOUT_UNDEFINED;
params.rate = playback_rate;
params.prefs = CUBEB_STREAM_PREF_NONE;

uint32_t min_latency;
cubeb_get_min_latency(context, &params, &min_latency);

auto retval = cubeb_stream_init(context, &stream, "Snes9x",
nullptr, nullptr,
nullptr, &params,
min_latency,
&::data_callback,
&state_callback,
this);

if (retval != CUBEB_OK)
{
printf("Failed to start stream. Error: %d!\n", retval);
stream = nullptr;
return false;
}

buffer.resize(2 * buffer_size * playback_rate / 1000);

return true;
}

int S9xCubebSoundDriver::space_free()
{
return buffer.space_empty();
}

std::pair<int, int> S9xCubebSoundDriver::buffer_level()
{
return { buffer.space_empty(), buffer.buffer_size };
}
36 changes: 36 additions & 0 deletions common/audio/s9x_sound_driver_cubeb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/

#ifndef __S9X_SOUND_DRIVER_CUBEB_HPP
#define __S9X_SOUND_DRIVER_CUBEB_HPP

#include "s9x_sound_driver.hpp"
#include <cstdint>
#include "cubeb/cubeb.h"
#include "../../apu/resampler.h"

class S9xCubebSoundDriver : public S9xSoundDriver
{
public:
S9xCubebSoundDriver();
~S9xCubebSoundDriver();
void init() override;
void deinit() override;
bool open_device(int playback_rate, int buffer_size) override;
void start() override;
void stop() override;
long data_callback(cubeb_stream *stream, void const *input_buffer, void *output_buffer, long nframes);
void write_samples(int16_t *data, int samples) override;
int space_free() override;
std::pair<int, int> buffer_level() override;

private:
Resampler buffer;
cubeb *context = nullptr;
cubeb_stream *stream = nullptr;
};

#endif /* __S9X_SOUND_DRIVER_SDL_HPP */
139 changes: 73 additions & 66 deletions common/audio/s9x_sound_driver_portaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ S9xPortAudioSoundDriver::S9xPortAudioSoundDriver()
audio_stream = NULL;
}

S9xPortAudioSoundDriver::~S9xPortAudioSoundDriver()
{
deinit();
}

void S9xPortAudioSoundDriver::init()
{
PaError err;
Expand Down Expand Up @@ -60,9 +65,64 @@ void S9xPortAudioSoundDriver::stop()
}
}

bool S9xPortAudioSoundDriver::tryHostAPI(int index)
{
auto hostapi_info = Pa_GetHostApiInfo(index);
if (!hostapi_info)
{
printf("Host API #%d has no info\n", index);
return false;
}
printf("Attempting API: %s\n", hostapi_info->name);

auto device_info = Pa_GetDeviceInfo(hostapi_info->defaultOutputDevice);
if (!device_info)
{
printf("(%s)...No device info available.\n", hostapi_info->name);
return false;
}

PaStreamParameters param{};
param.device = hostapi_info->defaultOutputDevice;
param.suggestedLatency = buffer_size_ms * 0.001;
param.channelCount = 2;
param.sampleFormat = paInt16;
param.hostApiSpecificStreamInfo = NULL;

printf("(%s : %s, latency %dms)...\n",
hostapi_info->name,
device_info->name,
(int)(param.suggestedLatency * 1000.0));

auto err = Pa_OpenStream(&audio_stream,
NULL,
&param,
playback_rate,
0,
paNoFlag,
NULL,
NULL);

int frames = playback_rate * buffer_size_ms / 1000;
//int frames = Pa_GetStreamWriteAvailable(audio_stream);
printf("PortAudio set buffer size to %d frames.\n", frames);
output_buffer_size = frames;

if (err == paNoError)
{
printf("OK\n");
return true;
}
else
{
printf("Failed (%s)\n", Pa_GetErrorText(err));
return false;
}
}

bool S9xPortAudioSoundDriver::open_device(int playback_rate, int buffer_size_ms)
{
PaStreamParameters param;

const PaDeviceInfo *device_info;
const PaHostApiInfo *hostapi_info;
PaError err = paNoError;
Expand All @@ -74,84 +134,31 @@ bool S9xPortAudioSoundDriver::open_device(int playback_rate, int buffer_size_ms)

if (err != paNoError)
{
fprintf(stderr,
"Couldn't reset audio stream.\nError: %s\n",
Pa_GetErrorText(err));
fprintf(stderr, "Couldn't reset audio stream.\nError: %s\n", Pa_GetErrorText(err));
return true;
}

audio_stream = NULL;
}

param.channelCount = 2;
param.sampleFormat = paInt16;
param.hostApiSpecificStreamInfo = NULL;
this->playback_rate = playback_rate;
this->buffer_size_ms = buffer_size_ms;

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

for (int i = 0; i < Pa_GetHostApiCount(); i++)
{
printf(" --> ");

hostapi_info = Pa_GetHostApiInfo(i);
if (!hostapi_info)
{
printf("Host API #%d has no info\n", i);
err = paNotInitialized;
continue;
}

device_info = Pa_GetDeviceInfo(hostapi_info->defaultOutputDevice);
if (!device_info)
{
printf("(%s)...No device info available.\n", hostapi_info->name);
err = paNotInitialized;
continue;
}

param.device = hostapi_info->defaultOutputDevice;
param.suggestedLatency = buffer_size_ms * 0.001;

printf("(%s : %s, latency %dms)...\n",
hostapi_info->name,
device_info->name,
(int)(param.suggestedLatency * 1000.0));

fflush(stdout);
int host = 2; //Pa_GetDefaultHostApi();
if (tryHostAPI(host))
return true;

err = Pa_OpenStream(&audio_stream,
NULL,
&param,
playback_rate,
0,
paNoFlag,
NULL,
NULL);

int frames = Pa_GetStreamWriteAvailable(audio_stream);
printf ("PortAudio set buffer size to %d frames.\n", frames);
output_buffer_size = frames;

if (err == paNoError)
{
printf("OK\n");
break;
}
else
{
printf("Failed (%s)\n",
Pa_GetErrorText(err));
}
}

if (err != paNoError || Pa_GetHostApiCount() < 1)
for (int i = 0; i < Pa_GetHostApiCount(); i++)
{
fprintf(stderr,
"Couldn't initialize sound\n");
return false;
if (Pa_GetDefaultHostApi() != i)
if (tryHostAPI(i))
return true;
}

return true;
fprintf(stderr, "Couldn't initialize sound\n");
return false;
}

int S9xPortAudioSoundDriver::space_free()
Expand Down
4 changes: 4 additions & 0 deletions common/audio/s9x_sound_driver_portaudio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class S9xPortAudioSoundDriver : public S9xSoundDriver
{
public:
S9xPortAudioSoundDriver();
~S9xPortAudioSoundDriver();
void init() override;
void deinit() override;
bool open_device(int playback_rate, int buffer_size) override;
Expand All @@ -25,9 +26,12 @@ class S9xPortAudioSoundDriver : public S9xSoundDriver
int space_free() override;
std::pair<int, int> buffer_level() override;
void samples_available();
bool tryHostAPI(int index);

private:
PaStream *audio_stream;
int playback_rate;
int buffer_size_ms;
int output_buffer_size;
};

Expand Down
5 changes: 5 additions & 0 deletions common/audio/s9x_sound_driver_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ S9xSDLSoundDriver::S9xSDLSoundDriver()
{
}

S9xSDLSoundDriver::~S9xSDLSoundDriver()
{
deinit();
}

void S9xSDLSoundDriver::init()
{
SDL_InitSubSystem(SDL_INIT_AUDIO);
Expand Down
1 change: 1 addition & 0 deletions common/audio/s9x_sound_driver_sdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class S9xSDLSoundDriver : public S9xSoundDriver
{
public:
S9xSDLSoundDriver();
~S9xSDLSoundDriver();
void init() override;
void deinit() override;
bool open_device(int playback_rate, int buffer_size) override;
Expand Down
2 changes: 2 additions & 0 deletions common/video/glx_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ bool GTKGLXContext::create_context()
return false;
}

resize();

return true;
}

Expand Down
1 change: 1 addition & 0 deletions common/video/wayland_egl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool WaylandEGLContext::create_context()
EGL_RED_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_ALPHA_SIZE, 0,
EGL_NONE
};

Expand Down
Loading

0 comments on commit 1b13250

Please sign in to comment.