[go: up one dir, main page]

Menu

[22bd09]: / gtk3 / m3u.cpp  Maximize  Restore  History

Download this file

180 lines (151 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <pthread.h>
#include <ctype.h>
#include <SDL2/SDL.h>
// Add these includes for access() and F_OK
#ifdef _WIN32
#include <io.h>
#define F_OK 0
#else
#include <unistd.h>
#endif
#include "visualization.h"
#include "midiplayer.h"
#include "dbopl_wrapper.h"
#include "wav_converter.h"
#include "audioconverter.h"
#include "convertoggtowav.h"
#include "convertopustowav.h"
#include "audio_player.h"
#include "vfs.h"
#include "icon.h"
#include "aiff.h"
#include "equalizer.h"
bool is_m3u_file(const char *filename) {
const char *ext = strrchr(filename, '.');
if (!ext) return false;
char ext_lower[10];
strncpy(ext_lower, ext, sizeof(ext_lower) - 1);
ext_lower[sizeof(ext_lower) - 1] = '\0';
for (int i = 0; ext_lower[i]; i++) {
ext_lower[i] = tolower(ext_lower[i]);
}
return strcmp(ext_lower, ".m3u") == 0 || strcmp(ext_lower, ".m3u8") == 0;
}
bool load_m3u_playlist(AudioPlayer *player, const char *m3u_path) {
FILE *file = fopen(m3u_path, "r");
if (!file) {
printf("Cannot open M3U file: %s\n", m3u_path);
return false;
}
char line[1024];
char m3u_dir[512];
bool was_empty_queue = (player->queue.count == 0);
int added_count = 0;
// Get directory of M3U file for relative paths
strncpy(m3u_dir, m3u_path, sizeof(m3u_dir) - 1);
m3u_dir[sizeof(m3u_dir) - 1] = '\0';
char *last_slash = strrchr(m3u_dir, '/');
if (!last_slash) last_slash = strrchr(m3u_dir, '\\');
if (last_slash) {
*(last_slash + 1) = '\0';
} else {
strcpy(m3u_dir, "./");
}
while (fgets(line, sizeof(line), file)) {
// Remove newline
line[strcspn(line, "\r\n")] = '\0';
// Skip empty lines and comments
if (line[0] == '\0' || line[0] == '#') continue;
char full_path[1024];
// Handle relative vs absolute paths
if (line[0] == '/' || (strlen(line) > 1 && line[1] == ':')) {
// Absolute path
strncpy(full_path, line, sizeof(full_path) - 1);
} else {
// Relative path
snprintf(full_path, sizeof(full_path), "%s%s", m3u_dir, line);
}
full_path[sizeof(full_path) - 1] = '\0';
// Check if file exists and has supported extension
if (access(full_path, F_OK) == 0) {
const char *ext = strrchr(full_path, '.');
if (ext) {
char ext_lower[10];
strncpy(ext_lower, ext, sizeof(ext_lower) - 1);
ext_lower[sizeof(ext_lower) - 1] = '\0';
for (int i = 0; ext_lower[i]; i++) {
ext_lower[i] = tolower(ext_lower[i]);
}
if (strcmp(ext_lower, ".mid") == 0 || strcmp(ext_lower, ".midi") == 0 ||
strcmp(ext_lower, ".wav") == 0 || strcmp(ext_lower, ".mp3") == 0 ||
strcmp(ext_lower, ".ogg") == 0 || strcmp(ext_lower, ".flac") == 0 ||
strcmp(ext_lower, ".aif") == 0 || strcmp(ext_lower, ".aiff") == 0 ||
strcmp(ext_lower, ".opus") == 0) {
if (add_to_queue(&player->queue, full_path)) {
added_count++;
printf("Added to queue: %s\n", full_path);
}
}
}
} else {
printf("File not found, skipping: %s\n", full_path);
}
}
fclose(file);
printf("M3U loaded: %d files added\n", added_count);
// If queue was empty and we added files, load the first one
if (was_empty_queue && player->queue.count > 0) {
if (load_file_from_queue(player)) {
update_gui_state(player);
}
}
update_queue_display(player);
update_gui_state(player);
return added_count > 0;
}
bool save_m3u_playlist(AudioPlayer *player, const char *m3u_path) {
if (player->queue.count == 0) {
printf("No files in queue to save\n");
return false;
}
FILE *file = fopen(m3u_path, "w");
if (!file) {
printf("Cannot create M3U file: %s\n", m3u_path);
return false;
}
// Write M3U header
fprintf(file, "#EXTM3U\n");
// Get directory of M3U file for relative paths
char m3u_dir[512];
strncpy(m3u_dir, m3u_path, sizeof(m3u_dir) - 1);
m3u_dir[sizeof(m3u_dir) - 1] = '\0';
char *last_slash = strrchr(m3u_dir, '/');
if (!last_slash) last_slash = strrchr(m3u_dir, '\\');
if (last_slash) {
*(last_slash + 1) = '\0';
} else {
strcpy(m3u_dir, "");
}
for (int i = 0; i < player->queue.count; i++) {
const char *file_path = player->queue.files[i];
// Try to make path relative if it's in the same directory or subdirectory
if (strlen(m3u_dir) > 0 && strncmp(file_path, m3u_dir, strlen(m3u_dir)) == 0) {
// File is in M3U directory or subdirectory, use relative path
fprintf(file, "%s\n", file_path + strlen(m3u_dir));
} else {
// Use absolute path
fprintf(file, "%s\n", file_path);
}
}
fclose(file);
printf("M3U playlist saved: %s (%d files)\n", m3u_path, player->queue.count);
return true;
}