summaryrefslogtreecommitdiff
path: root/src/audioio.cc
blob: bb047627685a93ea74df19c33ab1cf3be9844582 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            audioio.cc
 *
 *  Thu Sep 25 15:55:14 CEST 2014
 *  Copyright 2014 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of LibAudioIO.
 *
 *  LibAudioIO is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  
 *  LibAudioIO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with LibAudioIO; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "audioio.h"

#include "device.h"
#include "mixer.h"
#include "source.h"
#include "sink.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
#define NO_ERROR                     0

#define OUT_OF_MEMORY              -101
#define MISSING_HANDLE             -102

#define COULD_NOT_OPEN_DEVICE      -203
#define COULD_NOT_SET_HW_PARAMS    -204
#define COULD_NOT_INIT_PARAMS      -205
#define COULD_NOT_SET_ACCESS_MODE  -206
#define COULD_NOT_SET_FORMAT       -207
#define COULD_NOT_SET_CHANNELS     -208
#define COULD_NOT_SET_SAMPLE_RATE  -209
#define COULD_NOT_SET_PERIOD_SIZE  -210

#define BUFFER_TOO_SMALL           -305
#define BUFFER_OVERRUN             -306
#define READ_ERROR                 -307
#define SHORT_READ                 -308

#define MIXER_INIT_FAILED          -400

#define MIXER_NOT_INITIALISED      -401
#define INVALID_MIXER_LEVEL        -402
#define INVALID_CHANNEL_NUMBER     -403
#define COULD_NOT_SET_MIXER_LEVEL  -404
*/

#define MAGIC 0xdeadbeef

struct aio_t {
  unsigned int magic;

  Device *device;

  Source *source;
  Mixer *source_mixer;

  Sink *sink;
  Mixer *sink_mixer;
};

// Check if 'h' is a valid aio_t handle and report/return error if not.
#define CHECK_HANDLE(h)                            \
  do {                                             \
    if(!h || h->magic != MAGIC) {                  \
      return MISSING_HANDLE;                       \
    }                                              \
  } while(0)
  
struct aio_t *aio_init(int *err,
                       const char *playback_device,
                       const char *playback_mixer,
                       const char *capture_device,
                       const char *capture_mixer,
                       unsigned int samplerate,
                       unsigned int channels)
{
  *err = NO_ERROR;

  const char *device = playback_device;
  if(device == NULL || strlen(device) == 0) device = capture_device;
  if(device == NULL || strlen(device) == 0) {
    *err = COULD_NOT_OPEN_DEVICE;
    return NULL;
  }

  struct aio_t *h = new aio_t;
  memset(h, 0, sizeof(struct aio_t));
  h->magic = MAGIC;
  

  if(h == NULL) {
    *err = OUT_OF_MEMORY;
    return NULL;
  }

  h->device = new Device(device);

  if(playback_device != NULL && strlen(playback_device) > 0) {
    h->sink = h->device->getSink(playback_device, samplerate, channels);
    if(h->sink == NULL) {
      *err = COULD_NOT_OPEN_DEVICE;
    }
  }

  if(playback_mixer != NULL && strlen(playback_mixer) > 0) {
    h->sink_mixer = h->device->getMixer(playback_mixer);
    if(h->sink_mixer == NULL) {
      *err = MIXER_INIT_FAILED;
    }
  }

  if(capture_device != NULL && strlen(capture_device) > 0) {
    h->source = h->device->getSource(capture_device, samplerate, channels);
    if(h->sink == NULL) {
      *err = COULD_NOT_OPEN_DEVICE;
    }
  }

  if(capture_mixer != NULL && strlen(capture_mixer) > 0) {
    h->source_mixer = h->device->getMixer(capture_mixer);
    if(h->source_mixer == NULL) {
      *err = MIXER_INIT_FAILED;
    }
  }

  if(*err != NO_ERROR) {
    h->magic = 0;
    if(h->source) delete h->source;
    if(h->source_mixer) delete h->source_mixer;
    if(h->sink) delete h->sink;
    if(h->sink_mixer) delete h->sink_mixer;
    delete h;
    return NULL;
  }

  return h;
}

int aio_read(struct aio_t *h, char *pcm, unsigned int maxsize)
{
  CHECK_HANDLE(h);

  if(h->source) {
    return h->source->readSamples(pcm, maxsize);
  }

  return MISSING_HANDLE;
}

int aio_write(struct aio_t *h, const char *pcm, unsigned int size)
{
  CHECK_HANDLE(h);

  if(h->sink) {
    return h->sink->writeSamples(pcm, size);
  }

  return MISSING_HANDLE;
}

int aio_set_playback_mixer_level(struct aio_t *h, unsigned int c, float level)
{
  CHECK_HANDLE(h);

  if(h->sink_mixer) {
    h->sink_mixer->setLevel(c, level);
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_get_playback_mixer_level(struct aio_t *h, unsigned int c, float *level)
{
  CHECK_HANDLE(h);

  if(h->sink_mixer) {
    float l = h->sink_mixer->level(c);
    if(l < -1000) return INVALID_MIXER_LEVEL;
    *level = l;
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_get_playback_mixer_level_range(struct aio_t *h, float *min, float *max)
{
  CHECK_HANDLE(h);

  if(h->sink_mixer) {
    Mixer::range_t r = h->sink_mixer->range();
    *min = r.min;
    *max = r.max;
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_set_capture_mixer_level(struct aio_t *h, unsigned int c, float level)
{
  CHECK_HANDLE(h);

  if(h->source_mixer) {
    h->source_mixer->setLevel(c, level);
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_get_capture_mixer_level(struct aio_t *h, unsigned int c, float *level)
{
  CHECK_HANDLE(h);

  if(h->source_mixer) {
    float l = h->source_mixer->level(c);
    if(l < -1000) return INVALID_MIXER_LEVEL;
    *level = l;
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_get_capture_mixer_level_range(struct aio_t *h, float *min, float *max)
{
  CHECK_HANDLE(h);

  if(h->source_mixer) {
    Mixer::range_t r = h->source_mixer->range();
    *min = r.min;
    *max = r.max;
    return 0;
  }

  return MISSING_HANDLE;
}

int aio_get_samplerate(struct aio_t *h)
{
  CHECK_HANDLE(h);

  if(h->source) return h->source->samplerate();
  else if(h->sink) return h->sink->samplerate();
    
  return MISSING_HANDLE;
}

int aio_close(struct aio_t *h)
{
  CHECK_HANDLE(h);

  h->magic = 0;
  if(h->source) delete h->source;
  if(h->source_mixer) delete h->source_mixer;
  if(h->sink) delete h->sink;
  if(h->sink_mixer) delete h->sink_mixer;
  delete h;

  return 0;
}

#ifdef __cplusplus
}
#endif