-
Notifications
You must be signed in to change notification settings - Fork 31
/
ao.c
178 lines (157 loc) · 4.48 KB
/
ao.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
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
/*
* This file is part of dsp.
*
* Copyright (c) 2014-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ao/ao.h>
#include "ao.h"
#include "util.h"
#include "sampleconv.h"
struct ao_state {
ao_device *dev;
struct ao_enc_info *enc_info;
};
struct ao_enc_info {
const char *name;
int bytes, prec;
void (*write_func)(sample_t *, void *, ssize_t);
};
static const char codec_name[] = "ao";
static int ao_open_count = 0;
static struct ao_enc_info encodings[] = {
{ "s16", 2, 16, write_buf_s16 },
{ "u8", 1, 8, write_buf_u8 },
{ "s32", 4, 32, write_buf_s32 },
};
static struct ao_enc_info * ao_get_enc_info(const char *enc)
{
int i;
if (enc == NULL)
return &encodings[0];
for (i = 0; i < LENGTH(encodings); ++i)
if (strcmp(enc, encodings[i].name) == 0)
return &encodings[i];
return NULL;
}
ssize_t ao_write(struct codec *c, sample_t *buf, ssize_t frames)
{
struct ao_state *state = (struct ao_state *) c->data;
state->enc_info->write_func(buf, buf, frames * c->channels);
if (ao_play(state->dev, (char *) buf, frames * c->channels * state->enc_info->bytes) == 0) {
LOG_FMT(LL_ERROR, "%s: ao_play(): write failed", codec_name);
return 0;
}
return frames;
}
ssize_t ao_seek(struct codec *c, ssize_t pos)
{
return -1;
}
ssize_t ao_delay(struct codec *c)
{
return 0;
}
void ao_drop(struct codec *c)
{
/* do nothing */
}
void ao_pause(struct codec *c, int p)
{
/* do nothing */
}
void ao_destroy(struct codec *c)
{
struct ao_state *state = (struct ao_state *) c->data;
ao_close(state->dev);
--ao_open_count;
if (ao_open_count == 0)
ao_shutdown();
free(state);
}
struct codec * ao_codec_init(const struct codec_params *p)
{
int driver;
struct ao_enc_info *enc_info;
struct ao_sample_format format;
ao_device *dev = NULL;
struct ao_state *state = NULL;
struct ao_option *opts = NULL;
struct codec *c = NULL;
char buf_time_str[12];
if (ao_open_count == 0)
ao_initialize();
if ((driver = ao_default_driver_id()) == -1) {
LOG_FMT(LL_OPEN_ERROR, "%s: error: failed get default driver id", codec_name);
goto fail;
}
if ((enc_info = ao_get_enc_info(p->enc)) == NULL) {
LOG_FMT(LL_ERROR, "%s: error: bad encoding: %s", codec_name, p->enc);
goto fail;
}
format.bits = enc_info->prec;
format.rate = p->fs;
format.channels = p->channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = NULL;
const double buf_time = MINIMUM(1000.0 * p->block_frames * p->buf_ratio / p->fs, 1000.0);
snprintf(buf_time_str, sizeof(buf_time_str), "%.2f", buf_time);
if (LOGLEVEL(LL_VERBOSE))
ao_append_option(&opts, "verbose", "");
if (strcmp(p->path, CODEC_DEFAULT_DEVICE) != 0)
ao_append_option(&opts, "dev", p->path);
ao_append_option(&opts, "client_name", dsp_globals.prog_name);
ao_append_option(&opts, "buffer_time", buf_time_str);
if ((dev = ao_open_live(driver, &format, opts)) == NULL) {
LOG_FMT(LL_OPEN_ERROR, "%s: error: could not open device", codec_name);
goto fail;
}
ao_free_options(opts);
state = calloc(1, sizeof(struct ao_state));
state->dev = dev;
state->enc_info = enc_info;
c = calloc(1, sizeof(struct codec));
c->path = p->path;
c->type = p->type;
c->enc = enc_info->name;
c->fs = p->fs;
c->channels = p->channels;
c->prec = enc_info->prec;
c->hints |= CODEC_HINT_CAN_DITHER; /* all formats are fixed-point LPCM */
c->hints |= CODEC_HINT_INTERACTIVE;
c->frames = -1;
c->write = ao_write;
c->seek = ao_seek;
c->delay = ao_delay;
c->drop = ao_drop;
c->pause = ao_pause;
c->destroy = ao_destroy;
c->data = state;
++ao_open_count;
return c;
fail:
ao_free_options(opts);
if (ao_open_count == 0)
ao_shutdown();
return NULL;
}
void ao_codec_print_encodings(const char *type)
{
int i;
for (i = 0; i < LENGTH(encodings); ++i)
fprintf(stdout, " %s", encodings[i].name);
}