Compare commits

..

2 Commits

Author SHA1 Message Date
496c5e9acf Added a basic play queue 2025-10-25 23:20:10 +10:00
62c95a5de8 Added frontend pitch and volume control 2025-10-25 22:05:03 +10:00
7 changed files with 194 additions and 41 deletions

View File

@@ -20,10 +20,12 @@
extern configHandler_config_t* configObj;
bool bLikedSongsShow = false;
bool bAudioSettingsShow = false;
bool bPlayQueueShow = false;
void showLikedSongs();
void showAudioSettings();
void showPlayQueue();
int qt_gui_entry(int argc, char** argv) {
int gui_entry() {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
printf("SDL could not be initialized: %s\n", SDL_GetError());
@@ -104,6 +106,12 @@ int qt_gui_entry(int argc, char** argv) {
bAudioSettingsShow = true;
}
ImGui::SameLine();
if (ImGui::Button("Play Queue")) {
bPlayQueueShow = true;
}
ImGui::End();
}
@@ -115,6 +123,10 @@ int qt_gui_entry(int argc, char** argv) {
showAudioSettings();
}
if (bPlayQueueShow) {
showPlayQueue();
}
// Render
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
@@ -186,9 +198,10 @@ void showLikedSongs() {
}
if (selectedSong != -1) {
printf("Song: %s (%s)\n", starredStruct->songs[selectedSong].title,
starredStruct->songs[selectedSong].id);
OSSPlayer_QueueAppend(starredStruct->songs[selectedSong].id);
OSSPlayer_QueueAppend(starredStruct->songs[selectedSong].title,
starredStruct->songs[selectedSong].artist,
starredStruct->songs[selectedSong].id,
starredStruct->songs[selectedSong].duration);
selectedSong = -1;
}
@@ -196,20 +209,57 @@ void showLikedSongs() {
}
float in_volume_val = 0;
float out_volume_val = 0;
float pitch_val = 0;
bool hasInVolumeFirstRun = false;
void showAudioSettings() {
ImGui::Begin("Audio Settings");
if (!hasInVolumeFirstRun) {
in_volume_val = OSSPlayer_GstECont_InVolume_Get();
out_volume_val = OSSPlayer_GstECont_OutVolume_Get();
pitch_val = configObj->audio_pitch_cents / 100.0f; // Cents to semitones
hasInVolumeFirstRun = true;
}
ImGui::Text("In Vol / Out Vol");
// Idk what that field is, styling?, Size, Storage, Low, High
if (ImGui::VSliderFloat("##v", ImVec2(35, 160), &in_volume_val, 0.0f, 1.0f)) {
if (ImGui::VSliderFloat("##invol", ImVec2(35, 160), &in_volume_val, 0.0f, 1.0f)) {
// Data has changed
OSSPlayer_GstECont_InVolume_set(in_volume_val);
}
ImGui::SameLine();
if (ImGui::VSliderFloat("##outvol", ImVec2(35, 160), &out_volume_val, 0.0f, 1.0f)) {
OSSPlayer_GstECont_OutVolume_set(out_volume_val);
}
ImGui::SameLine();
if (ImGui::VSliderFloat("##pitch", ImVec2(35, 160), &pitch_val, -6.00f, 6.00f)) {
OSSPlayer_GstECont_Pitch_Set(pitch_val * 100.0f); // Convert semitones to cents
}
ImGui::End();
}
// TODO: go through abstraction
#include "../player/playQueue.hpp"
void showPlayQueue() {
ImGui::Begin("Play Queue");
static int selectedSong = -1;
if (ImGui::BeginChild("Play Queue", ImVec2(0, 200), ImGuiChildFlags_Border)) {
for (int i = 0; i < internal_OSSPQ_GetItemCount(); i++) {
if (ImGui::Selectable(internal_OSSPQ_GetTitleAtIndex(i), selectedSong == i)) {
selectedSong = i;
}
}
ImGui::EndChild();
}
ImGui::End();
}

View File

@@ -11,7 +11,7 @@
extern "C" {
#endif // __cplusplus
int qt_gui_entry(int argc, char** argv);
int gui_entry();
#ifdef __cplusplus
}

View File

@@ -68,7 +68,7 @@ int main(int argc, char** argv) {
discordrpc_struct_deinit(&discordrpc);
// Launch QT frontend
qt_gui_entry(argc, argv);
gui_entry();
// Cleanup and exit
configHandler_Free(&configObj);

View File

@@ -5,29 +5,82 @@
* Info: Gstreamer Queue Handler
*/
/*
* Now you might ask why this even exists in this way, but I thought that it would be easier to jump to C++
* to store a song queue with objects instead of some hacked together solution in C
* And I was right.
* Like yeah, you can store an array of structs easily in C, but to dynamically be able to move those around, no,
* std::deque makes that MUCH simpler.
*/
#include <cstddef>
#include <string.h>
#include <string>
#include <deque>
#include "playQueue.hpp"
// NOTE: Acronym is OpenSubsonicPlayerQueue
std::deque<std::string> OSSPQ_Items;
// C++ interface for storing song queue data (C interface is in the header)
class SongObject {
public:
std::string title;
std::string artist;
std::string id;
long duration;
};
int internal_OSSPQ_AppendToEnd(char* id) {
// NOTE: Acronym is OpenSubsonicPlayerQueue
std::deque<SongObject> OSSPQ_Items;
int internal_OSSPQ_AppendToEnd(char* title, char* artist, char* id, long duration) {
// Append a new song to the end of the queue
printf("Title: %s\nArtist: %s\nID: %s\nDuration: %ld\n", title, artist, id, duration);
// TODO: Find a neater way of converting a C string to a C++ string??
std::string cpp_title(title);
std::string cpp_artist(artist);
std::string cpp_id(id);
OSSPQ_Items.push_back(cpp_id);
SongObject songObject;
songObject.title = cpp_title;
songObject.artist = cpp_artist;
songObject.id = cpp_id;
songObject.duration = duration;
OSSPQ_Items.push_back(songObject);
return 0;
}
char* internal_OSSPQ_PopFromFront() {
OSSPQ_SongStruct* internal_OSSPQ_PopFromFront() {
if (OSSPQ_Items.empty()) {
// No items in play queue
return NULL;
}
char* id = strdup(OSSPQ_Items.front().c_str()); // Heap allocate id
// Pull the first song off the song queue
SongObject songObject = OSSPQ_Items.front();
OSSPQ_Items.pop_front();
return id;
// Move song data into a C readable format
// NOTE: I am initializing the variables to a known value just in case there is missing information in songObject
OSSPQ_SongStruct* playQueueObject = (OSSPQ_SongStruct*)malloc(sizeof(OSSPQ_SongStruct));
playQueueObject->title = NULL;
playQueueObject->artist = NULL;
playQueueObject->id = NULL;
playQueueObject->duration = 0;
playQueueObject->title = strdup(songObject.title.c_str());
playQueueObject->artist = strdup(songObject.artist.c_str());
playQueueObject->id = strdup(songObject.id.c_str());
playQueueObject->duration = songObject.duration;
return playQueueObject;
}
void internal_OSSPQ_FreeSongObject(OSSPQ_SongStruct* songObject) {
if (songObject->title != NULL) { free(songObject->title); }
if (songObject->artist != NULL) { free(songObject->artist); }
if (songObject->id != NULL) { free(songObject->id); }
if (songObject != NULL) { free(songObject); }
}
char* internal_OSSPQ_GetTitleAtIndex(int idx) {
return (char*)OSSPQ_Items[idx].title.c_str();
}
int internal_OSSPQ_GetItemCount() {

View File

@@ -11,8 +11,18 @@
extern "C" {
#endif // __cplusplus
int internal_OSSPQ_AppendToEnd(char* id);
char* internal_OSSPQ_PopFromFront();
// C interface for sending song queue data (C++ interface is in the C++ file)
typedef struct {
char* title;
char* artist;
char* id;
long duration;
} OSSPQ_SongStruct;
int internal_OSSPQ_AppendToEnd(char* title, char* artist, char* id, long duration);
OSSPQ_SongStruct* internal_OSSPQ_PopFromFront();
void internal_OSSPQ_FreeSongObject(OSSPQ_SongStruct* songObject);
char* internal_OSSPQ_GetTitleAtIndex(int idx);
int internal_OSSPQ_GetItemCount();
#ifdef __cplusplus

View File

@@ -10,19 +10,17 @@
#include <math.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
#include "../configHandler.h"
#include "../discordrpc.h"
#include "../libopensubsonic/logger.h"
#include "../libopensubsonic/endpoint_getSong.h"
#include "../libopensubsonic/httpclient.h"
#include "playQueue.hpp"
#include "player.h"
#include <unistd.h> // For worse sleep TODO
extern configHandler_config_t* configObj;
static int rc = 0;
GstElement *pipeline, *playbin, *filter_bin, *conv_in, *conv_out, *in_volume, *equalizer, *pitch, *reverb;
GstElement *pipeline, *playbin, *filter_bin, *conv_in, *conv_out, *in_volume, *equalizer, *pitch, *reverb, *out_volume;
GstPad *sink_pad, *src_pad;
GstBus* bus;
guint bus_watch_id;
@@ -118,12 +116,15 @@ void* OSSPlayer_ThrdInit(void*) {
if (internal_OSSPQ_GetItemCount() != 0 && isPlaying == false) {
printf("IS VALID\n");
// Player is not playing and a song is in the song queue
char* id = OSSPlayer_QueuePopFront();
if (id == NULL) {
OSSPQ_SongStruct* pq = OSSPlayer_QueuePopFront();
if (pq == NULL) {
printf("FUCK\n");
// TODO: this
}
char* id = strdup(pq->id);
//free(pq);
// NOTE: Using a few strdup()'s because the cleanup/deinit functions perform free's and to avoid UAFs/Double frees
// Fetch song information
@@ -168,7 +169,7 @@ void* OSSPlayer_ThrdInit(void*) {
isPlaying = true;
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
usleep(200 * 1000); // Use futex and signals instead of this TODO
usleep(200 * 1000);
}
}
@@ -216,6 +217,7 @@ int OSSPlayer_GstInit() {
// Calf Studio Plugins Reverb
reverb = gst_element_factory_make(configObj->lv2_reverb_filter_name, "reverb");
}
out_volume = gst_element_factory_make("volume", "out-volume");
// TODO: Make better error messages for here, and exit out early
if (!equalizer) {
logger_log_error(__func__, "Could not initialize equalizer.");
@@ -229,8 +231,8 @@ int OSSPlayer_GstInit() {
// Add and link elements to the filter bin
// TODO: Check creation and dynamic as per config
gst_bin_add_many(GST_BIN(filter_bin), conv_in, in_volume, equalizer, conv_out, NULL);
gst_element_link_many(conv_in, in_volume, equalizer, conv_out, NULL);
gst_bin_add_many(GST_BIN(filter_bin), conv_in, in_volume, equalizer, pitch, out_volume, conv_out, NULL);
gst_element_link_many(conv_in, in_volume, equalizer, pitch, out_volume, conv_out, NULL);
sink_pad = gst_element_get_static_pad(conv_in, "sink");
src_pad = gst_element_get_static_pad(conv_out, "src");
gst_element_add_pad(filter_bin, gst_ghost_pad_new("sink", sink_pad));
@@ -240,7 +242,7 @@ int OSSPlayer_GstInit() {
// Setup playbin3 (Configure audio plugins and set user agent)
g_object_set(playbin, "audio-filter", filter_bin, NULL);
g_signal_connect (playbin, "source-setup", G_CALLBACK(gst_playbin3_sourcesetup_callback), NULL);
g_signal_connect(playbin, "source-setup", G_CALLBACK(gst_playbin3_sourcesetup_callback), NULL);
// Add playbin3 to the pipeline
gst_bin_add(GST_BIN(pipeline), playbin);
@@ -248,10 +250,11 @@ int OSSPlayer_GstInit() {
// Initialize in-volume (Volume before the audio reaches the plugins)
g_object_set(in_volume, "volume", 0.175, NULL);
// Initialize out-volume (Volume after the audio plugins)
g_object_set(out_volume, "volume", 1.00, NULL);
// Initialize equalizer
if (configObj->audio_equalizer_enable) {
printf("Initializing %d equalizer bands...\n", configObj->audio_equalizer_graphCount);
// Dynamically append settings to the equalizer to match the config file
for (int i = 0; i < configObj->audio_equalizer_graphCount; i++) {
char* ftl_name = NULL;
@@ -296,7 +299,7 @@ int OSSPlayer_GstInit() {
g_object_set(equalizer, fl_name, freq, NULL);
g_object_set(equalizer, fr_name, freq, NULL);
} else {
printf("EQ band %d - F: %.2f / G: %.2f / Q: 4.36\n", i + 1, (float)configObj->audio_equalizer_graph[i].frequency, gain);
printf("EQ band %d - F: %.2f(Nfp) / G: %.2f / Q: 4.36\n", i + 1, (float)configObj->audio_equalizer_graph[i].frequency, gain);
g_object_set(equalizer, fl_name, (float)configObj->audio_equalizer_graph[i].frequency, NULL);
g_object_set(equalizer, fr_name, (float)configObj->audio_equalizer_graph[i].frequency, NULL);
}
@@ -315,9 +318,11 @@ int OSSPlayer_GstInit() {
}
// Initialize pitch
if (configObj->audio_pitch_enable) {
float scaleFactor = OSSPlayer_CentsToPSF(configObj->audio_pitch_cents);
printf("Pitch Cents: %.2f, Scale factor: %.6f\n", configObj->audio_pitch_cents, scaleFactor);
g_object_set(pitch, "pitch", scaleFactor, NULL);
}
// Initialize reverb
}
@@ -329,25 +334,28 @@ int OSSPlayer_GstDeInit() {
/*
* Player Queue Control Functions
*/
int OSSPlayer_QueueAppend(char* id) {
int OSSPlayer_QueueAppend(char* title, char* artist, char* id, long duration) {
// Call to C++ function
internal_OSSPQ_AppendToEnd(id);
// Note: I would receive a song struct instead of individual elements, but it would significantly slow down the GUI
internal_OSSPQ_AppendToEnd(title, artist, id, duration);
}
char* OSSPlayer_QueuePopFront() {
OSSPQ_SongStruct* OSSPlayer_QueuePopFront() {
// Call to C++ function
// NOTE: 'id' is heap-allocated from C++
char* id = internal_OSSPQ_PopFromFront();
if (id == NULL) {
OSSPQ_SongStruct* songObject = internal_OSSPQ_PopFromFront();
if (songObject == NULL) {
// Queue is empty TODO
printf("FUCKFUCKFUCK\n");
}
return id;
return songObject;
}
/*
* Gstreamer Element Control Functions
*/
// TODO: Consolidate volume functions?
float OSSPlayer_GstECont_InVolume_Get() {
gdouble vol;
g_object_get(in_volume, "volume", &vol, NULL);
@@ -358,6 +366,25 @@ void OSSPlayer_GstECont_InVolume_set(float val) {
g_object_set(in_volume, "volume", val, NULL);
}
float OSSPlayer_GstECont_OutVolume_Get() {
gdouble vol;
g_object_get(out_volume, "volume", &vol, NULL);
return (float)vol;
}
void OSSPlayer_GstECont_OutVolume_set(float val) {
g_object_set(out_volume, "volume", val, NULL);
}
float OSSPlayer_GstECont_Pitch_Get() {
//
}
void OSSPlayer_GstECont_Pitch_Set(float cents) {
float psf = OSSPlayer_CentsToPSF(cents);
g_object_set(pitch, "pitch", psf, NULL);
}
/*
* Utility Functions
*/
@@ -370,3 +397,9 @@ float OSSPlayer_PitchFollow(float freq, float semitone) {
// Calculate new EQ frequency from semitone adjustment
return freq * pow(2.0, semitone / 12.0);
}
float OSSPlayer_CentsToPSF(float cents) {
// Convert Cents to a Pitch Scale Factor
float semitone = cents / 100.0;
return pow(2, (semitone / 12.0f));
}

View File

@@ -11,17 +11,24 @@
extern "C" {
#endif
#include "playQueue.hpp"
void* OSSPlayer_GMainLoop(void*);
void* OSSPlayer_ThrdInit(void*);
int OSSPlayer_GstInit();
int OSSPlayer_QueueAppend(char* id);
char* OSSPlayer_QueuePopFront();
int OSSPlayer_QueueAppend(char* title, char* artist, char* id, long duration);
OSSPQ_SongStruct* OSSPlayer_QueuePopFront();
float OSSPlayer_GstECont_InVolume_Get();
void OSSPlayer_GstECont_InVolume_set(float val);
float OSSPlayer_GstECont_OutVolume_Get();
void OSSPlayer_GstECont_OutVolume_set(float val);
float OSSPlayer_GstECont_Pitch_Get();
void OSSPlayer_GstECont_Pitch_Set(float cents);
float OSSPlayer_DbLinMul(float db);
float OSSPlayer_PitchFollow(float freq, float semitone);
float OSSPlayer_CentsToPSF(float cents);
#ifdef __cplusplus
}