-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmixer_ogg.cpp
209 lines (169 loc) · 4.54 KB
/
mixer_ogg.cpp
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
#include "mixer_ogg.h"
#include <stdarg.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <kos.h>
#include <oggvorbis/sndoggvorbis.h>
char *strdup(const char *src) {
char *dst = (char *)malloc(strlen(src) + 1);
if (dst) {
strcpy(dst, src);
}
return dst;
}
static char mix_error[256] = "";
static int playing = 0;
static int looping = 0;
static Mix_Music *current_music = NULL;
static void Mix_SetError(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vsnprintf(mix_error, sizeof(mix_error), fmt, args);
va_end(args);
}
int Mix_OpenAudio(int frequency, uint16_t format, int channels, int chunksize) {
printf("SDL_Mixer mixer_ogg\n");
if (snd_stream_init() < 0) {
Mix_SetError("Mix_OpenAudio: Failed to initialize sound system.");
return -1;
}
if (sndoggvorbis_init() < 0) {
Mix_SetError("Mix_OpenAudio: Failed to initialize sndoggvorbis.");
return -1;
}
return 0;
}
Mix_Music *Mix_LoadMUS(const char *file) {
Mix_Music *music = (Mix_Music *)malloc(sizeof(Mix_Music));
if (!music) {
Mix_SetError("Mix_LoadMUS: Memory allocation failed.");
return NULL;
}
music->file_path = strdup(file);
if (!music->file_path) {
Mix_SetError("Mix_LoadMUS: Failed to allocate memory for file path.");
free(music);
return NULL;
}
return music;
}
Mix_Chunk *Mix_LoadWAV(const char *file) {
int sound_id = snd_sfx_load(file);
if (sound_id < 0) {
Mix_SetError("Mix_LoadWAV: Failed to load WAV file %s.", file);
return NULL;
}
Mix_Chunk *chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
if (!chunk) {
Mix_SetError("Mix_LoadWAV: Memory allocation failed for Mix_Chunk.");
return NULL;
}
chunk->sound_id = sound_id;
return chunk;
}
int Mix_PlayMusic(Mix_Music *music, int loops) {
if (!music) {
Mix_SetError("Mix_PlayMusic: No music to play.");
return -1;
}
int loop_flag = 0;
if (loops == -1) {
loop_flag = 1;
printf("Looping: %s\n", (char *)music);
} else {
printf("Playing once: %s\n", (char *)music);
}
if (sndoggvorbis_start(music->file_path, loop_flag) < 0) {
Mix_SetError("Mix_PlayMusic: Failed to start playback for file %s.", music->file_path);
return -1;
}
current_music = music;
playing = 1;
looping = loops;
return 0;
}
int Mix_PlayChannel(int channel, Mix_Chunk *chunk, int loops) {
if (!chunk) {
Mix_SetError("Mix_PlayChannel: Invalid Mix_Chunk.");
return -1;
}
int result = snd_sfx_play(chunk->sound_id, loops, channel);
if (result < 0) {
Mix_SetError("Mix_PlayChannel: Failed to play sound ID %d.", chunk->sound_id);
return -1;
}
return 0;
}
void Mix_HaltMusic() {
if (playing) {
sndoggvorbis_stop();
playing = 0;
}
}
static int music_paused = 0; // 0 = playback , 1 = paused
void Mix_Pause(int channel) {
if (music_paused) {
Mix_SetError("Mix_Pause: Music is already paused.");
return;
}
sndoggvorbis_stop();
music_paused = 1;
printf("Music paused.\n");
}
int Mix_HaltChannel(int channel) {
if (channel < 0) {
Mix_SetError("Mix_HaltChannel: Invalid channel %d.", channel);
return -1;
}
snd_sfx_stop(channel);
printf("Channel %d halted.", channel);
return 0;
}
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms) {
if (!music) {
Mix_SetError("Mix_FadeInMusic: No music provided.");
return -1;
}
sndoggvorbis_start(music->file_path, loops);
return 0;
}
int Mix_FadeOutMusic(int ms) {
if (!playing) {
Mix_SetError("Mix_FadeOutMusic: No music is playing.");
return -1;
}
sndoggvorbis_stop();
playing = 0;
return 0;
}
void Mix_PauseMusic(void) {
Mix_SetError("Mix_PauseMusic: Not implemented.");
}
void Mix_Resume(int channel) {
Mix_SetError("Mix_Resume: Not implemented for channel %d.", channel);
}
void Mix_ResumeMusic(void) {
Mix_SetError("Mix_ResumeMusic: Not implemented.");
}
void Mix_FreeMusic(Mix_Music *music) {
if (music) {
if (music->file_path) {
free(music->file_path);
}
free(music);
}
}
void Mix_FreeChunk(Mix_Chunk *chunk) {
if (chunk) {
snd_sfx_unload(chunk->sound_id);
free(chunk);
}
}
void Mix_CloseAudio() {
Mix_HaltMusic();
snd_sfx_unload_all();
sndoggvorbis_stop();
sndoggvorbis_shutdown();
snd_stream_shutdown();
}