-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample-play.c
64 lines (47 loc) · 1.27 KB
/
example-play.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
// SPDX-License-Identifier: GPL-2.0
/*
* Simple example on how to play an SNDH file in 44.1 kHz stereo on
* standard output. Pipe output to for example the aplay command
*
* lib/psgplay/example-play example.sndh | aplay -r44100 -c2 -fS16_LE
*
* or -fS16_BE for a big-endian machine.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "internal/macro.h"
#include "internal/print.h"
#include "psgplay/psgplay.h"
#include "system/unix/file.h"
#include "system/unix/sndh.h"
int main(int argc, char *argv[])
{
if (argc <= 1) {
fprintf(stderr, "usage: example-play <sndh-file>...\n");
return EXIT_FAILURE;
}
for (int arg = 1; arg < argc; arg++) {
const char *path = argv[arg];
struct file f = sndh_read_file(path);
if (!file_valid(f))
pr_fatal_errno(path);
struct psgplay *pp = psgplay_init(f.data, f.size, 1, 44100);
if (!pp)
pr_fatal_error("%s: Failed to play file\n", path);
for (;;) {
struct psgplay_stereo buffer[4096];
const ssize_t r = psgplay_read_stereo(pp,
buffer, ARRAY_SIZE(buffer));
if (r <= 0)
break;
const ssize_t s = sizeof(struct psgplay_stereo[r]);
const ssize_t w = xwrite(STDOUT_FILENO, buffer, s);
if (w != s)
break;
}
psgplay_free(pp);
file_free(f);
}
return EXIT_SUCCESS;
}