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
|
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include "sndfile.h"
#include "fmt.h"
/*
* gcc -O2 -o playit-alsa playit-alsa.c -I.. -I../src/include -DHAVE_CONFIG_H -lm `pkg-config alsa --cflags --libs` -s ../src/libplayit.a
*/
song_t * song_load_it(const char *file)
{
slurp_t *s;
song_t *song;
s = slurp(file, NULL, 0);
if (!s) {
printf("failed to open \"%s\"\n", file);
goto _fail;
}
song = csf_allocate();
if (!song) {
printf("failed to allocate song\n");
goto _fail_slurp;
}
if (fmt_it_load_song(song, s, 0) != LOAD_SUCCESS)
goto _fail_song;
unslurp(s);
return song;
_fail_song:
csf_free(song);
_fail_slurp:
unslurp(s);
_fail:
return NULL;
}
static snd_pcm_t * alsa_open(const char *dev)
{
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;
int err;
unsigned int rate = 44100;
snd_pcm_t *pcm;
if((err = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n",
dev,
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "cannot allocate hardware parameter structure(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_any(pcm, hw_params)) < 0) {
fprintf(stderr, "cannot initialize hardware parameter structure(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_set_access(pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set access type(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_set_format(pcm, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf(stderr, "cannot set sample format(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, 0)) < 0) {
fprintf(stderr, "cannot set sample rate(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params_set_channels(pcm, hw_params, 2)) < 0) {
fprintf(stderr, "cannot set channel count(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_hw_params(pcm, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters(%s)\n",
snd_strerror(err));
return NULL;
}
snd_pcm_hw_params_free(hw_params);
if((err = snd_pcm_sw_params_malloc(&sw_params)) < 0) {
fprintf(stderr, "cannot allocate software parameters structure(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_sw_params_current(pcm, sw_params)) < 0) {
fprintf(stderr, "cannot initialize software parameters structure(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_sw_params_set_avail_min(pcm, sw_params, 4096)) < 0) {
fprintf(stderr, "cannot set minimum available count(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0U)) < 0) {
fprintf(stderr, "cannot set start mode(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_sw_params(pcm, sw_params)) < 0) {
fprintf(stderr, "cannot set software parameters(%s)\n",
snd_strerror(err));
return NULL;
}
if((err = snd_pcm_prepare(pcm)) < 0) {
fprintf(stderr, "cannot prepare audio interface for use(%s)\n",
snd_strerror(err));
return NULL;
}
return pcm;
}
static void alsa_close(snd_pcm_t *pcm)
{
snd_pcm_close(pcm);
}
int main(int argc, char *argv[])
{
song_t *song;
uint8_t buf[8192];
int len;
snd_pcm_t *pcm;
if (argc != 2) {
printf("usage: %s song.it\n", argv[0]);
goto _fail;
}
song = song_load_it(argv[1]);
if (!song) {
printf("failed to load song!\n");
goto _fail;
}
if (!(pcm = alsa_open("default"))) {
printf("Failed to open ALSA device\n");
goto _fail_song;
}
csf_set_current_order(song, 0);
song->repeat_count = 0;
song->buffer_count = 0;
song->stop_at_order = -1;
song->stop_at_row = -1;
song->flags &= ~(SONG_PAUSED | SONG_PATTERNLOOP | SONG_ENDREACHED);
csf_reset_playmarks(song);
csf_set_resampling_mode(song, SRCMODE_LINEAR);
csf_set_wave_config(song, 44100, 16, 2);
while ((len = csf_read(song, buf, sizeof(buf)))) {
int err;
if ((err = snd_pcm_writei(pcm, buf, len)) < 0) {
fprintf (stderr, "write failed (%s)\n", snd_strerror(err));
break;
}
}
alsa_close(pcm);
return EXIT_SUCCESS;
_fail_song:
/* TODO cleanup song */
_fail:
return EXIT_FAILURE;
}
|