-
Notifications
You must be signed in to change notification settings - Fork 0
/
gme-pulse.c
100 lines (86 loc) · 2.08 KB
/
gme-pulse.c
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
/*
* Game Music Emu output via PulseAudio
* by Mike Melanson (mike -at- multimedia.cx)
*
* Compile using:
* gcc -Wall gme-pulse.c -o gme-pulse -lgme -lpulse-simple
*/
#include <stdio.h>
#include <stdlib.h>
#include <gme/gme.h>
#ifdef __linux__
#include <pulse/simple.h>
#else
#error This program is designed for Linux (uses Linux-only APIs)
#endif
#define SAMPLE_RATE 44100
#define BIT_RESOLUTION 16
#define CHANNELS 2
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
pa_simple *s;
pa_sample_spec spec;
int pa_error;
Music_Emu *emu;
gme_info_t *info;
gme_err_t gmeErr;
int track;
short audio_buffer[BUFFER_SIZE];
if (argc < 2)
{
printf("USAGE: gme-pulse <game music file> [track number]\n");
return 1;
}
gmeErr = gme_open_file(argv[1], &emu, SAMPLE_RATE);
if (gmeErr)
{
printf("%s\n", gmeErr);
return 1;
}
if (argc >= 3)
track = atoi(argv[2]);
else
track = 0;
if (track < 0 || track > gme_track_count(emu))
{
printf("there is no track %d; playing track 0 instead\n", track);
track = 0;
}
gmeErr = gme_track_info(emu, &info, track);
if (gmeErr)
{
printf("%s\n", gmeErr);
gme_delete(emu);
return 2;
}
spec.channels = CHANNELS;
spec.rate = SAMPLE_RATE;
spec.format = PA_SAMPLE_S16LE;
s = pa_simple_new(NULL, "Game Music Emu", PA_STREAM_PLAYBACK, NULL,
"Audio", &spec, NULL, NULL, NULL);
if (!s)
{
printf("problem opening audio via PulseAudio\n");
gme_delete(emu);
return 3;
}
printf("system: %s\ngame: %s\nsong: %s\nlength: %d ms\nplay length: %d ms\nplaying track %d...\nCtrl-C to exit\n",
info->system, info->game, info->song, info->length, info->play_length, track);
gme_start_track(emu, track);
do
{
gmeErr = gme_play(emu, BUFFER_SIZE, audio_buffer);
if (gmeErr)
{
printf("%s\n", gmeErr);
break;
}
pa_simple_write(s, audio_buffer, BUFFER_SIZE * CHANNELS, &pa_error);
} while (gme_tell(emu) < info->play_length);
pa_simple_drain(s, &pa_error);
pa_simple_free(s);
gme_free_info(info);
gme_delete(emu);
return 0;
}