Skip to content

Latest commit

 

History

History
213 lines (164 loc) · 4.44 KB

File metadata and controls

213 lines (164 loc) · 4.44 KB
 
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
203
204
205
206
207
208
209
210
211
212
/*
tinyalsa: sound output with TINY Advanced Linux Sound Architecture
copyright 2006-8 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Jarno Lehtinen <lehtinen@sci.fi>
*/
#include "out123_int.h"
#include <errno.h>
#include <tinyalsa/asoundlib.h>
#include "debug.h"
typedef struct
{
struct pcm *pcm;
struct pcm_params *params;
struct pcm_config config;
unsigned int device;
unsigned int card;
} mpg123_tinyalsa_t;
static int initialize_device(out123_handle *ao)
{
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
ta->config.channels = ao->channels;
ta->config.rate = ao->rate;
ta->config.period_size = 1024;
ta->config.period_count = 4;
ta->config.format = PCM_FORMAT_S16_LE;
ta->config.start_threshold = 0;
ta->config.stop_threshold = 0;
ta->config.silence_threshold = 0;
ta->pcm = pcm_open(ta->card, ta->device, PCM_OUT, &ta->config);
if (!ta->pcm || !pcm_is_ready(ta->pcm))
{
if(!AOQUIET)
error3( "(open) Unable to open card %u PCM device %u (%s)\n"
, ta->card, ta->device, pcm_get_error(ta->pcm) );
return -1;
}
return 0;
}
static int open_tinyalsa(out123_handle *ao)
{
debug("open_tinyalsa()");
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
if (ao->format != -1)
{
/* we're going to play: initalize sample format */
return initialize_device(ao);
}
else
{
/* query mode; sample format will be set for each query */
return 0;
}
}
static int get_formats_tinyalsa(out123_handle *ao)
{
debug("get_formats_tinyalsa()");
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
if ( ao->rate >= pcm_params_get_min(ta->params, PCM_PARAM_RATE) && ao->rate <= pcm_params_get_max(ta->params, PCM_PARAM_RATE) && ao->channels >= pcm_params_get_min(ta->params, PCM_PARAM_CHANNELS) && ao->channels <= pcm_params_get_max(ta->params, PCM_PARAM_CHANNELS) )
{
return MPG123_ENC_SIGNED_16;
}
else
{
return 0;
}
}
static int write_tinyalsa(out123_handle *ao, unsigned char *buf, int bytes)
{
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
if (ta->pcm)
{
if(pcm_write(ta->pcm, buf, bytes))
{
if(!AOQUIET)
error("Error playing sample\n");
return -1;
}
}
return bytes;
}
static void flush_tinyalsa(out123_handle *ao)
{
debug("flush_tinyalsa()");
}
static int close_tinyalsa(out123_handle *ao)
{
debug("close_tinyalsa()");
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
if (ta->pcm)
{
pcm_close(ta->pcm);
}
return 0;
}
static int deinit_tinyalsa(out123_handle* ao)
{
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
/* Free up card/device parameters */
pcm_params_free(ta->params);
/* Free up memory */
if(ao->userptr)
{
free( ao->userptr );
ao->userptr = NULL;
}
/* Success */
return 0;
}
static int init_tinyalsa(out123_handle* ao)
{
if (ao==NULL) return -1;
/* Set callbacks */
ao->open = open_tinyalsa;
ao->flush = flush_tinyalsa;
ao->write = write_tinyalsa;
ao->get_formats = get_formats_tinyalsa;
ao->close = close_tinyalsa;
ao->deinit = deinit_tinyalsa;
/* Allocate memory for data structure */
ao->userptr = malloc( sizeof( mpg123_tinyalsa_t ) );
if(ao->userptr==NULL)
{
if(!AOQUIET)
error("failed to malloc memory for 'mpg123_tinyalsa_t'");
return -1;
}
memset( ao->userptr, 0, sizeof(mpg123_tinyalsa_t) );
/* Set card and device */
mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;
ta->card = 0;
ta->device = 0;
if (ao->device)
{
char *ptr = ao->device;
ta->card = (unsigned int)strtol(ptr, &ptr, 10);
if (strlen(ptr) > 0)
{
ta->device = (unsigned int)strtol(++ptr, &ptr, 10);
}
}
/* Get card/device parameters */
ta->params = pcm_params_get(ta->card, ta->device, PCM_OUT);
if (ta->params == NULL)
{
if(!AOQUIET)
error2( "(params) Unable to open card %u PCM device %u.\n"
, ta->card, ta->device );
return -1;
}
/* Success */
return 0;
}
/*
Module information data structure
*/
mpg123_module_t mpg123_output_module_info = {
/* api_version */ MPG123_MODULE_API_VERSION,
/* name */ "tinyalsa",
/* description */ "Output audio using TINY Advanced Linux Sound Architecture (TINYALSA).",
/* revision */ "$Rev:$",
/* handle */ NULL,
/* init_output */ init_tinyalsa,
};