Remove some amount of glitchiness from portaudio initialization
This commit is contained in:
parent
1987f3b9fa
commit
dd7ccc85b9
@ -35,10 +35,10 @@ namespace audio::impl{
|
||||
bool m_stopped;
|
||||
|
||||
//audio thread access only!
|
||||
struct{
|
||||
mixdata data;
|
||||
size_t offset;
|
||||
}m_playing_chunk = {};
|
||||
struct alignas(64){
|
||||
mixdata data = {};
|
||||
size_t offset = 0;
|
||||
}m_playing_chunk;
|
||||
|
||||
public:
|
||||
channel();
|
||||
|
||||
@ -65,7 +65,7 @@ namespace audio{
|
||||
static PaStream* initialize_global_instance();
|
||||
//to avoid using portaudio functions in the header
|
||||
static int open_default_stream(PaStream**, int in_c, int out_c,
|
||||
int fmt, double samplerate,
|
||||
long unsigned int fmt, double samplerate,
|
||||
size_t bufsize, detail::callback_iface* cb);
|
||||
static int callback(const void* input, void* output, unsigned long framecount,
|
||||
const PaStreamCallbackTimeInfo* /*timeInfo*/, PaStreamCallbackFlags /*statusFlags*/,
|
||||
|
||||
2
makefile
2
makefile
@ -40,7 +40,7 @@ ifneq ($(WINDOWS),1)
|
||||
CC::=gcc
|
||||
CXX::=g++
|
||||
LDLIBS::=
|
||||
LDFLAGS::= -lglfw -lgl3w -ldl -lm -lportaudio -lasound -lsndfile
|
||||
LDFLAGS::= -lglfw -lgl3w -ldl -lm -lportaudio -lasound -lsndfile -lpthread
|
||||
STRIP::=strip
|
||||
RANLIB::=ranlib
|
||||
AR::=ar
|
||||
|
||||
@ -22,6 +22,8 @@
|
||||
|
||||
#include <portaudio.h>
|
||||
|
||||
#define PORTAUDIO_FIXED_LATENCY 0.030
|
||||
|
||||
namespace audio{
|
||||
|
||||
stream::~stream(){
|
||||
@ -69,10 +71,14 @@ namespace audio{
|
||||
return nullptr;
|
||||
}
|
||||
int stream::open_default_stream(PaStream** stream, int in_c, int out_c,
|
||||
int fmt, double samplerate,
|
||||
long unsigned int fmt, double samplerate,
|
||||
size_t bufsize, detail::callback_iface* cb)
|
||||
{
|
||||
return Pa_OpenDefaultStream(stream, in_c, out_c, fmt, samplerate, bufsize, callback, cb);
|
||||
PaStreamParameters out = {Pa_GetDefaultOutputDevice(), out_c, fmt, PORTAUDIO_FIXED_LATENCY, nullptr};
|
||||
PaStreamParameters in = {Pa_GetDefaultInputDevice(), in_c, fmt, PORTAUDIO_FIXED_LATENCY, nullptr};
|
||||
PaStreamParameters* outp = out_c ? &out : nullptr;
|
||||
PaStreamParameters* inp = in_c ? &in : nullptr;
|
||||
return Pa_OpenStream(stream, inp, outp, samplerate, paFramesPerBufferUnspecified, paNoFlag, callback, cb);
|
||||
}
|
||||
int stream::callback(const void* input, void* output, unsigned long framecount,
|
||||
const PaStreamCallbackTimeInfo* /*timeInfo*/, PaStreamCallbackFlags /*statusFlags*/,
|
||||
|
||||
56
src/main.cpp
56
src/main.cpp
@ -1,11 +1,19 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
|
||||
#include "render.hpp"
|
||||
#include "game_state.hpp"
|
||||
#include "math/math.hpp"
|
||||
#include "config.hpp"
|
||||
#include "audio/sndrd.hpp"
|
||||
#include "audio/mixdata.hpp"
|
||||
#include "audio/mixer.hpp"
|
||||
|
||||
// 0 | 1 | 2
|
||||
// ---------
|
||||
@ -85,10 +93,6 @@ int exists_empty_tile(const game_state& gs){
|
||||
return 0;
|
||||
}
|
||||
|
||||
void play_audio(){
|
||||
system("mpv assets/moans/$(ls assets/moans/ | sort -R | head -n 1) > /dev/null");
|
||||
}
|
||||
|
||||
void handle_window_close(){
|
||||
debug_print("[II] Window close event triggered\n");
|
||||
running = false;
|
||||
@ -100,19 +104,61 @@ void handle_input_events(GLFWwindow*, int key, int, int, int){
|
||||
manager.request_exit();
|
||||
}
|
||||
|
||||
audio::mixchunk read_audio_file(const char* filename){
|
||||
debug_print("!!!!filename!!!! %s\n", filename);
|
||||
audio::sndrd f(filename, audio::sndrd::mode::r);
|
||||
if(!f.valid()){
|
||||
return {};
|
||||
}
|
||||
return f.read_all();
|
||||
}
|
||||
std::string select_audio_file(){
|
||||
size_t filecnt = 0;
|
||||
for(std::filesystem::directory_iterator i("assets/moans");i != std::filesystem::directory_iterator();++i){
|
||||
std::filesystem::directory_entry e = *i;
|
||||
if(!e.is_directory())
|
||||
++filecnt;
|
||||
}
|
||||
|
||||
size_t selection = rand() % filecnt;
|
||||
for(std::filesystem::directory_iterator i("assets/moans");i != std::filesystem::directory_iterator();++i){
|
||||
std::filesystem::directory_entry e = *i;
|
||||
if(selection == 0){
|
||||
return e.path().native();
|
||||
}
|
||||
if(!e.is_directory())
|
||||
--selection;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void gay_thread(audio::mixer& m, std::atomic_bool& should_gay_thread_stop){
|
||||
while(!should_gay_thread_stop){
|
||||
audio::mixchunk ch = read_audio_file(select_audio_file().c_str());
|
||||
m.get_channel(0).play(ch);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
srand(time(NULL));
|
||||
audio::mixer mix(audio::mixer::mode::STEREO, 1);
|
||||
audio::mixchunk ch = read_audio_file(select_audio_file().c_str());
|
||||
game_state gs = {};
|
||||
|
||||
play_audio();
|
||||
manager.init(640, 480, "Tic-Tac-Gugh");
|
||||
manager.handle_window_close_event(handle_window_close);
|
||||
manager.handle_keypress_event(handle_input_events);
|
||||
|
||||
std::atomic_bool should_gay_thread_stop = false;
|
||||
std::thread t(gay_thread, std::ref(mix), std::ref(should_gay_thread_stop));
|
||||
|
||||
while(running)
|
||||
manager.update();
|
||||
|
||||
should_gay_thread_stop = true;
|
||||
while(exists_empty_tile(gs) && gs.turn != -1){
|
||||
game_turn(gs, get_player_input());
|
||||
}
|
||||
t.join();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user