basic sounds

This commit is contained in:
mrbesen 2021-12-13 01:23:03 +01:00
parent 772376e440
commit 92b7defaf5
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
7 changed files with 63 additions and 39 deletions

View File

@ -20,12 +20,14 @@ public:
~MainWindow();
public slots:
void buttonPressed();
void buttonPressed(unsigned int id);
void buttonPressed0();
void buttonPressed1();
void shortcut();
void shortcut2();
private:
void playSound();
void playSound(unsigned int id);
Ui::MainWindow *ui;
QShortcut* sc;

View File

@ -24,8 +24,6 @@ private:
Sound();
~Sound();
void init();
ma_context context;
std::vector<SoundDevice*> devices;

View File

@ -2,52 +2,65 @@
#include "ui_mainwindow.h"
#include <iostream>
#include <string>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
Sound& sound = Sound::instance(); // init sound
sound.addDeviceWithName("VirtualMic");
sound.addDefaultDevice();
QPushButton* btn = ui->pushButton;
QObject::connect(btn, SIGNAL( clicked() ), this, SLOT( buttonPressed() ));
QPushButton* btn2 = ui->pushButton2;
QObject::connect(btn2, SIGNAL( clicked() ), this, SLOT( buttonPressed() ));
sc = new QShortcut(QKeySequence("Shift+1"), this, 0, 0, Qt::ApplicationShortcut);
QObject::connect(sc, SIGNAL( activated() ), this, SLOT( shortcut() ));
globalShortcut = new QxtGlobalShortcut(QKeySequence("Shift+F1"));
QObject::connect(globalShortcut, SIGNAL(activated()), this, SLOT( shortcut2() ));
Sound& sound = Sound::instance(); // init sound
sound.addDeviceWithName("VirtualMic");
sound.addDefaultDevice();
}
static const std::string SOUNDS[2] {"Uwu_voice-xjrU3N8M4eo-251.mp3", "bonk.wav"};
static const unsigned int SOUNDSSIZE = 2;
MainWindow::~MainWindow() {
delete ui;
Sound::deinit();
}
void MainWindow::buttonPressed() {
std::cout << "Button Pressed" << std::endl;
void MainWindow::buttonPressed(unsigned int id) {
std::cout << "Button Pressed: " << id << std::endl;
playSound();
playSound(id);
}
void MainWindow::buttonPressed0() {
buttonPressed(0);
}
void MainWindow::buttonPressed1() {
buttonPressed(1);
}
void MainWindow::shortcut() {
std::cout << "Shortcut Pressed" << std::endl;
playSound();
// playSound();
}
void MainWindow::shortcut2() {
std::cout << "Shortcut2 Pressed!!" << std::endl;
playSound();
// playSound();
}
void MainWindow::playSound() {
//Sound::instance().addPlayback("Uwu_voice-xjrU3N8M4eo-251.mp3");
Sound::instance().addPlayback("bonk.wav");
void MainWindow::playSound(unsigned int soundid) {
if(soundid < SOUNDSSIZE) {
Sound::instance().addPlayback(SOUNDS[soundid]);
}
}

View File

@ -48,8 +48,7 @@ const std::string Sound::FOLDER = "/home/yannis/prog/ts3/soundboardsounds/";
Sound& Sound::instance() {
if (!inst) {
inst = new Sound(); //TODO: needs delete
inst->init();
inst = new Sound();
}
return *inst;
}
@ -93,6 +92,10 @@ void Sound::stopAll() {
}
Sound::Sound() {
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
std::cout << "Failed to initialize Sound context." << std::endl;
throw new std::exception();
}
}
Sound::~Sound() {
@ -104,12 +107,3 @@ Sound::~Sound() {
ma_context_uninit(&context);
}
void Sound::init() {
//dies wird getrennt vom ctor, da es beim ma_device_start sonst zu einer race condition kommen kann
// enumerate devices
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
std::cout << "Failed to initialize Sound context." << std::endl;
throw new std::exception();
}
}

View File

@ -111,7 +111,7 @@ SoundDevice* SoundDevice::createDevice(ma_context* ctx, const ma_device_id* did)
if(did != NULL)
deviceConfig.playback.pDeviceID = did;
deviceConfig.sampleRate = 44100;
deviceConfig.sampleRate = 48000; // 44100;
deviceConfig.dataCallback = global_sound_callback;
deviceConfig.pulse.pStreamNamePlayback = "MySoundboard";

View File

@ -29,6 +29,10 @@ HEADERS += \
include/sounddevice.h \
miniaudio/miniaudio.h
MOC_DIR = generated/
UI_DIR = ui/
OBJECTS_DIR = obj/
FORMS += \
ui/mainwindow.ui

View File

@ -17,8 +17,8 @@
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>130</x>
<y>110</y>
<x>330</x>
<y>10</y>
<width>151</width>
<height>19</height>
</rect>
@ -30,14 +30,27 @@
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>120</x>
<y>150</y>
<width>100</width>
<height>27</height>
<x>0</x>
<y>30</y>
<width>371</width>
<height>531</height>
</rect>
</property>
<property name="text">
<string>Testbutton</string>
<string>UwU</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton2">
<property name="geometry">
<rect>
<x>400</x>
<y>30</y>
<width>381</width>
<height>531</height>
</rect>
</property>
<property name="text">
<string>Bonk</string>
</property>
</widget>
</widget>