-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.c
More file actions
140 lines (109 loc) · 3.02 KB
/
audio.c
File metadata and controls
140 lines (109 loc) · 3.02 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
#include "cMusix.h"
int audio_file(const char* filename) {
if (!filename) return 0;
const char* ext = strrchr(filename, '.');
if (!ext) return 0;
// Convert extension to lowercase for comparison
char lower_ext[10];
int i = 0;
while (ext[i] && i < 9) {
lower_ext[i] = tolower(ext[i]);
i++;
}
lower_ext[i] = '\0';
// Check for supported audio file extensions
return (strcmp(lower_ext, ".mp3") == 0 ||
strcmp(lower_ext, ".wav") == 0 ||
strcmp(lower_ext, ".flac") == 0 ||
strcmp(lower_ext, ".ogg") == 0 ||
strcmp(lower_ext, ".m4a") == 0 ||
strcmp(lower_ext, ".aac") == 0);
}
int init_audio() {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
return 0;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
return 0;
}
return 1;
}
void cleanup() {
// Stop and free music
if (player.current_music) {
Mix_HaltMusic();
Mix_FreeMusic(player.current_music);
player.current_music = NULL;
}
// Close audio systems
Mix_CloseAudio();
SDL_Quit();
// Restore terminal state
clear_screen();
move_cursor(1, 1);
show_cursor();
reset_color();
// Restore normal terminal mode
rawModeOff();
// Print a nice goodbye message
printf("You are now exiting cMusix...\n");
fflush(stdout);
}
void playSong() {
if (player.count == 0) return;
if (player.current_music) {
Mix_FreeMusic(player.current_music);
}
player.current_music = Mix_LoadMUS(player.songs[player.current_index].path);
if (!player.current_music) return;
Mix_VolumeMusic((int)(player.volume * 128));
if (Mix_PlayMusic(player.current_music, 0) == -1) return;
player.is_playing = 1;
player.is_paused = 0;
player.song_start_time = time(NULL);
}
void pauseResume() {
if (!player.is_playing) return;
if (player.is_paused) {
Mix_ResumeMusic();
player.is_paused = 0;
} else {
Mix_PauseMusic();
player.is_paused = 1;
}
}
void stopPlayback() {
Mix_HaltMusic();
player.is_playing = 0;
player.is_paused = 0;
}
void nextSong() {
if (player.count == 0) return;
if (player.shuffle) {
player.current_index = rand() % player.count;
} else {
player.current_index = (player.current_index + 1) % player.count;
}
playSong();
}
void previousSong() {
if (player.count == 0) return;
if (player.shuffle) {
player.current_index = rand() % player.count;
} else {
player.current_index = (player.current_index - 1 + player.count) % player.count;
}
playSong();
}
void set_volume(float volume) {
player.volume = volume;
if (player.volume < 0.0f) player.volume = 0.0f;
if (player.volume > 1.0f) player.volume = 1.0f;
Mix_VolumeMusic((int)(player.volume * 128));
}
void shuffleFunction() {
player.shuffle = !player.shuffle;
}
void repeatFunction() {
player.repeat = !player.repeat;
}