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
|
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include "base.h"
#define MIXER_FLAGS (MIX_INIT_FLAC | MIX_INIT_OGG)
bool initSDL(void) {
// initialization of SDLs audio system and the mixer
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
fprintf(stderr, "error initializing SDL_audio: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
s32 mixflags = Mix_Init(MIXER_FLAGS);
if ((mixflags & MIXER_FLAGS) != mixflags) {
fprintf(stderr, "error initializing SDL_mixer: %s\n", Mix_GetError());
exit(EXIT_FAILURE);
}
// setting up the default audio device for playback (using common defaults)
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16SYS, MIX_DEFAULT_CHANNELS, 2048) != 0) {
fprintf(stderr, "error opening audio device: %s\n", Mix_GetError());
exit(EXIT_FAILURE);
}
return true;
}
void cleanup(Mix_Music* musicfile) {
Mix_HaltMusic();
Mix_FreeMusic(musicfile);
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
}
int main (int argc, char **argv) {
bool initialized = initSDL();
assert(initialized == true);
Mix_Music *musicfile = NULL;
musicfile = Mix_LoadMUS(argv[1]);
if (musicfile == NULL) {
fprintf(stderr, "error opening file: %s\n", Mix_GetError());
exit(EXIT_FAILURE);
}
if (Mix_PlayMusic(musicfile, 0) == -1) {
fprintf(stderr, "error playing music: '%s'\n", argv[1]);
}
fprintf(stderr, "currently playing: '%s'\n", argv[1]);
u8 key;
while (Mix_PlayingMusic() != 0) {
if (Mix_PlayingMusic() == 0) break;
if (read(STDIN_FILENO, &key, 1) != 1 || key == 'q') break;
}
cleanup(musicfile);
return 0;
}
|