[go: up one dir, main page]

Menu

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

Download this file

404 lines (320 with data), 11.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "vfs.h"
#include "audio_player.h"
// Global virtual filesystem
static GHashTable* virtual_filesystem = NULL;
// Initialize the virtual filesystem
void init_virtual_filesystem() {
if (!virtual_filesystem) {
virtual_filesystem = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
}
}
// Clean up the virtual filesystem
void cleanup_virtual_filesystem() {
if (virtual_filesystem) {
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, virtual_filesystem);
while (g_hash_table_iter_next(&iter, &key, &value)) {
VirtualFile* vf = (VirtualFile*)value;
if (vf) {
free(vf->data);
free(vf);
}
}
g_hash_table_destroy(virtual_filesystem);
virtual_filesystem = NULL;
}
}
// Create a new virtual file
VirtualFile* create_virtual_file(const char* filename) {
if (!virtual_filesystem) {
init_virtual_filesystem();
}
VirtualFile* vf = malloc(sizeof(VirtualFile));
if (!vf) return NULL;
vf->data = malloc(1024); // Initial capacity
vf->size = 0;
vf->capacity = 1024;
vf->position = 0;
if (!vf->data) {
free(vf);
return NULL;
}
// Remove existing file if it exists
VirtualFile* existing = g_hash_table_lookup(virtual_filesystem, filename);
if (existing) {
free(existing->data);
free(existing);
}
g_hash_table_insert(virtual_filesystem, g_strdup(filename), vf);
return vf;
}
// Get an existing virtual file
VirtualFile* get_virtual_file(const char* filename) {
if (!virtual_filesystem) return NULL;
return (VirtualFile*)g_hash_table_lookup(virtual_filesystem, filename);
}
// Write data to virtual file
bool virtual_file_write(VirtualFile* vf, const void* data, size_t size) {
if (!vf || !data) return false;
// Expand capacity if needed
if (vf->position + size > vf->capacity) {
size_t new_capacity = vf->capacity;
while (new_capacity < vf->position + size) {
new_capacity *= 2;
}
char* new_data = realloc(vf->data, new_capacity);
if (!new_data) return false;
vf->data = new_data;
vf->capacity = new_capacity;
}
memcpy(vf->data + vf->position, data, size);
vf->position += size;
if (vf->position > vf->size) {
vf->size = vf->position;
}
return true;
}
// Read data from virtual file
size_t virtual_file_read(VirtualFile* vf, void* buffer, size_t size) {
if (!vf || !buffer) return 0;
size_t available = vf->size - vf->position;
size_t to_read = (size < available) ? size : available;
if (to_read > 0) {
memcpy(buffer, vf->data + vf->position, to_read);
vf->position += to_read;
}
return to_read;
}
// Seek in virtual file
bool virtual_file_seek(VirtualFile* vf, long offset, int whence) {
if (!vf) return false;
long new_position;
switch (whence) {
case SEEK_SET:
new_position = offset;
break;
case SEEK_CUR:
new_position = vf->position + offset;
break;
case SEEK_END:
new_position = vf->size + offset;
break;
default:
return false;
}
if (new_position < 0 || (size_t)new_position > vf->size) {
return false;
}
vf->position = new_position;
return true;
}
// Get current position in virtual file
long virtual_file_tell(VirtualFile* vf) {
if (!vf) return -1;
return (long)vf->position;
}
// Get virtual file size
size_t virtual_file_size(VirtualFile* vf) {
if (!vf) return 0;
return vf->size;
}
// Delete a virtual file
bool delete_virtual_file(const char* filename) {
if (!virtual_filesystem) return false;
VirtualFile* vf = g_hash_table_lookup(virtual_filesystem, filename);
if (vf) {
free(vf->data);
free(vf);
return g_hash_table_remove(virtual_filesystem, filename);
}
return false;
}
VirtualWAVConverter* virtual_wav_converter_init(const char* filename, int sample_rate, int channels) {
VirtualWAVConverter* converter = malloc(sizeof(VirtualWAVConverter));
if (!converter) return NULL;
converter->vf = create_virtual_file(filename);
if (!converter->vf) {
free(converter);
return NULL;
}
converter->sample_rate = sample_rate;
converter->channels = channels;
converter->samples_written = 0;
// Write WAV header (we'll update it later)
char header[44] = {0};
// RIFF header
memcpy(header, "RIFF", 4);
// File size (will be updated later)
memcpy(header + 8, "WAVE", 4);
// Format chunk
memcpy(header + 12, "fmt ", 4);
*(uint32_t*)(header + 16) = 16; // Chunk size
*(uint16_t*)(header + 20) = 1; // Audio format (PCM)
*(uint16_t*)(header + 22) = channels;
*(uint32_t*)(header + 24) = sample_rate;
*(uint32_t*)(header + 28) = sample_rate * channels * 2; // Byte rate
*(uint16_t*)(header + 32) = channels * 2; // Block align
*(uint16_t*)(header + 34) = 16; // Bits per sample
// Data chunk
memcpy(header + 36, "data", 4);
// Data size (will be updated later)
virtual_file_write(converter->vf, header, 44);
return converter;
}
bool virtual_wav_converter_write(VirtualWAVConverter* converter, int16_t* samples, size_t count) {
if (!converter || !samples) return false;
size_t bytes = count * sizeof(int16_t);
bool success = virtual_file_write(converter->vf, samples, bytes);
if (success) {
converter->samples_written += count;
}
return success;
}
void virtual_wav_converter_finish(VirtualWAVConverter* converter) {
if (!converter) return;
// Update WAV header with correct sizes
size_t data_size = converter->samples_written * sizeof(int16_t);
size_t file_size = data_size + 36;
// Seek to file size position and update
virtual_file_seek(converter->vf, 4, SEEK_SET);
uint32_t file_size_le = file_size;
virtual_file_write(converter->vf, &file_size_le, 4);
// Seek to data size position and update
virtual_file_seek(converter->vf, 40, SEEK_SET);
uint32_t data_size_le = data_size;
virtual_file_write(converter->vf, &data_size_le, 4);
}
void virtual_wav_converter_free(VirtualWAVConverter* converter) {
if (converter) {
free(converter);
}
}
// Modified functions for the audio player
bool convert_midi_to_virtual_wav(AudioPlayer *player, const char* filename) {
// Generate a unique virtual filename
static int virtual_counter = 0;
char virtual_filename[256];
snprintf(virtual_filename, sizeof(virtual_filename), "virtual_midi_%d.wav", virtual_counter++);
strncpy(player->temp_wav_file, virtual_filename, sizeof(player->temp_wav_file) - 1);
player->temp_wav_file[sizeof(player->temp_wav_file) - 1] = '\0';
printf("Converting MIDI to virtual WAV: %s -> %s\n", filename, virtual_filename);
// Temporarily shut down the main SDL audio system
if (player->audio_device) {
SDL_CloseAudioDevice(player->audio_device);
player->audio_device = 0;
}
SDL_QuitSubSystem(SDL_INIT_AUDIO);
playTime = 0;
isPlaying = true;
if (!initSDL()) {
printf("SDL init for conversion failed\n");
return false;
}
if (!loadMidiFile(filename)) {
printf("MIDI file load failed\n");
cleanup();
return false;
}
VirtualWAVConverter* wav_converter = virtual_wav_converter_init(virtual_filename, SAMPLE_RATE, AUDIO_CHANNELS);
if (!wav_converter) {
printf("Virtual WAV converter init failed\n");
cleanup();
return false;
}
int16_t audio_buffer[AUDIO_BUFFER * AUDIO_CHANNELS];
double buffer_duration = (double)AUDIO_BUFFER / SAMPLE_RATE;
processEvents();
while (isPlaying) {
memset(audio_buffer, 0, sizeof(audio_buffer));
OPL_Generate(audio_buffer, AUDIO_BUFFER);
if (!virtual_wav_converter_write(wav_converter, audio_buffer, AUDIO_BUFFER * AUDIO_CHANNELS)) {
printf("Virtual WAV write failed\n");
break;
}
playTime += buffer_duration;
playwait -= buffer_duration;
while (playwait <= 0 && isPlaying) {
processEvents();
}
if (((int)playTime) % 10 == 0) {
static int last_reported = -1;
if ((int)playTime != last_reported) {
printf("Converting... %d seconds\n", (int)playTime);
last_reported = (int)playTime;
}
}
}
virtual_wav_converter_finish(wav_converter);
virtual_wav_converter_free(wav_converter);
cleanup();
printf("Virtual conversion complete: %.2f seconds\n", playTime);
// Reinitialize the main SDL audio system for playback
printf("Reinitializing SDL audio for playback...\n");
if (!init_audio(player)) {
printf("Failed to reinitialize audio for playback\n");
return false;
}
return true;
}
bool load_virtual_wav_file(AudioPlayer *player, const char* virtual_filename) {
VirtualFile* vf = get_virtual_file(virtual_filename);
if (!vf) {
printf("Cannot open virtual WAV file: %s\n", virtual_filename);
return false;
}
// Reset position to beginning
virtual_file_seek(vf, 0, SEEK_SET);
// Read WAV header
char header[44];
if (virtual_file_read(vf, header, 44) != 44) {
printf("Cannot read virtual WAV header\n");
return false;
}
// Verify WAV format
if (strncmp(header, "RIFF", 4) != 0 || strncmp(header + 8, "WAVE", 4) != 0) {
printf("Invalid virtual WAV format\n");
return false;
}
// Extract WAV info
player->sample_rate = *(int*)(header + 24);
player->channels = *(short*)(header + 22);
player->bits_per_sample = *(short*)(header + 34);
printf("Virtual WAV: %d Hz, %d channels, %d bits\n",
player->sample_rate, player->channels, player->bits_per_sample);
// ADDED: Reinitialize audio with the correct sample rate and channels
if (!init_audio(player, player->sample_rate, player->channels)) {
printf("Failed to reinitialize audio for virtual WAV format\n");
return false;
}
// Calculate data size and duration
size_t total_size = virtual_file_size(vf);
size_t data_size = total_size - 44;
player->song_duration = data_size / (double)(player->sample_rate * player->channels * (player->bits_per_sample / 8));
printf("Virtual WAV duration: %.2f seconds\n", player->song_duration);
// Allocate and read audio data
int16_t* wav_data = (int16_t*)malloc(data_size);
if (!wav_data) {
printf("Memory allocation failed\n");
return false;
}
if (virtual_file_read(vf, wav_data, data_size) != data_size) {
printf("Virtual WAV data read failed\n");
free(wav_data);
return false;
}
// Store in audio buffer
pthread_mutex_lock(&player->audio_mutex);
if (player->audio_buffer.data) free(player->audio_buffer.data);
player->audio_buffer.data = wav_data;
player->audio_buffer.length = data_size / sizeof(int16_t);
player->audio_buffer.position = 0;
pthread_mutex_unlock(&player->audio_mutex);
printf("Loaded %zu samples from virtual file\n", player->audio_buffer.length);
return true;
}