File: sdl2_mixer_music.c

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (72 lines) | stat: -rw-r--r-- 1,425 bytes parent folder | download | duplicates (2)
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 <emscripten.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

Mix_Music* music;

void main2()
{
	if (Mix_PlayingMusic())
		return;

	printf("Done playing sound\n");
	printf("Shutting down\n");
	emscripten_cancel_main_loop();
	Mix_FreeMusic(music);
	Mix_CloseAudio();
#ifdef REPORT_RESULT
	REPORT_RESULT(1);
#endif
}

int main(int argc, char* argv[])
{
	int frequency, active_flags;

	if (SDL_Init(SDL_INIT_AUDIO) < 0) {
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return -1;
	}

	frequency = EM_ASM_INT_V({
		var context;
		try {
		  context = new AudioContext();
		} catch (e) {
		  context = new webkitAudioContext(); // safari only
		}
		return context.sampleRate;
	});

	if(Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, 2, 1024) == -1) {
		printf("Failed to Mix_OpenAudio(): %s\n", Mix_GetError());
		return -1;
	}

#ifndef FLAGS
#error "must define FLAGS"
#endif
	active_flags = Mix_Init(FLAGS);
	if ((FLAGS & active_flags) != FLAGS) {
		printf("Failed to Mix_Init(): %s\n", Mix_GetError());
		return -1;
	}

#ifndef SOUND_PATH
#error "must define SOUND_PATH"
#endif
	music = Mix_LoadMUS(SOUND_PATH);
	if (music == NULL) {
		printf("Failed to Mix_LoadMUS(): %s\n", Mix_GetError());
		return -1;
	}

	if (Mix_PlayMusic(music, 1)) {
		printf("Failed to Mix_PlayMusic(): %s\n", Mix_GetError());
		return -1;
	}

	printf("Playing sound\n");
	emscripten_set_main_loop(main2, 0, 1);
	return 0;
}