source: roaraudio/roard/driver_portaudio.c @ 4855:a55e1c122d46

Last change on this file since 4855:a55e1c122d46 was 4855:a55e1c122d46, checked in by phi, 13 years ago

first set of changes for PA v.19

File size: 5.5 KB
Line 
1//driver_portaudio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *      Copyright (C) Hans-Kristian 'maister' Arntzen - 2010
6 *
7 *  This file is part of roard a part of RoarAudio,
8 *  a cross-platform sound system for both, home and professional use.
9 *  See README for details.
10 *
11 *  This file is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License version 3
13 *  as published by the Free Software Foundation.
14 *
15 *  RoarAudio is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this software; see the file COPYING.  If not, write to
22 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 *  Boston, MA 02110-1301, USA.
24 *
25 */
26
27#include "roard.h"
28
29#ifdef _DRIVER_PORTAUDIO_CAN_OPERATE
30
31int driver_portaudio_open(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) {
32 struct driver_portaudio * self;
33 PaSampleFormat fmt;
34#ifdef ROAR_HAVE_LIBPABLIO
35 long flags = PABLIO_WRITE;
36#elif defined(ROAR_HAVE_PA19_VERSION_19)
37 PaError err;
38 PaStreamParameters params;
39#endif
40
41 if ( fh != -1 )
42  return -1;
43
44 switch (info->bits) {
45  case 8:
46    switch (info->codec) {
47     case ROAR_CODEC_PCM_S_LE:
48     case ROAR_CODEC_PCM_S_BE:
49     case ROAR_CODEC_PCM_S_PDP:
50       fmt = paInt8;
51      break;
52     case ROAR_CODEC_PCM_U_LE:
53     case ROAR_CODEC_PCM_U_BE:
54     case ROAR_CODEC_PCM_U_PDP:
55       fmt = paUInt8;
56      break;
57     default:
58       return -1;
59      break;
60    }
61   break;
62  case 16:
63    if ( info->codec != ROAR_CODEC_DEFAULT )
64     return -1;
65    fmt = paInt16;
66   break;
67  case 24:
68    if ( info->codec != ROAR_CODEC_DEFAULT )
69     return -1;
70    fmt = paPackedInt24;
71   break;
72  case 32:
73    if ( info->codec != ROAR_CODEC_DEFAULT )
74     return -1;
75    fmt = paInt32;
76   break;
77  default:
78    return -1;
79 }
80
81 if ( (self = roar_mm_malloc(sizeof(struct driver_portaudio))) == NULL )
82  return -1;
83
84 memset(self, 0, sizeof(struct driver_portaudio));
85
86 memset(inst, 0, sizeof(struct roar_vio_calls));
87
88 inst->inst  = self;
89 inst->close = driver_portaudio_close;
90 inst->write = driver_portaudio_write;
91
92 Pa_Initialize();
93
94#ifdef ROAR_HAVE_LIBPABLIO
95 switch (info->channels) {
96  case 1: flags |= PABLIO_MONO;   break;
97  case 2: flags |= PABLIO_STEREO; break;
98  default:
99    roar_mm_free(self);
100    Pa_Terminate();
101    return -1;
102 }
103
104 if ( OpenAudioStream(&(self->ostream), info->rate, fmt, flags) != paNoError ) {
105  roar_mm_free(self);
106  Pa_Terminate();
107  return -1;
108 }
109
110 return 0;
111#elif defined(ROAR_HAVE_PA19_VERSION_19)
112 params.device                    = Pa_GetDefaultOutputDevice();
113 params.channelCount              = info->channels;
114 params.sampleFormat              = fmt;
115 params.suggestedLatency          = Pa_GetDeviceInfo(params.device)->defaultLowOutputLatency;
116 params.hostApiSpecificStreamInfo = NULL;
117
118 // TODO: FIXME: use libroar for this.
119 self->framesize = info->bits * info->channels / 8;
120
121 // Sets up blocking I/O stream.
122 err = Pa_OpenStream(&(self->stream),
123                     NULL,
124                     &params,
125                     info->rate,
126                     128 /*FIXME:frames*/,
127                     paClipOff,
128                     NULL,
129                     NULL
130                    );
131
132 if ( err != paNoError ) {
133  ROAR_ERR("driver_portaudio_open(*): Could not open PortAudio device: \"%s\".", Pa_GetErrorText(err));
134  roar_mm_free(self);
135  return -1;
136 }
137
138 err = Pa_StartStream(self->stream);
139
140 if ( err != paNoError ) {
141  ROAR_ERR("driver_portaudio_open(*): Could not start stream: \"%s\".", Pa_GetErrorText(err));
142  roar_mm_free(self);
143  return -1;
144 }
145
146 return 0;
147#else
148 return -1;
149#endif
150}
151
152int     driver_portaudio_close        (struct roar_vio_calls * vio) {
153 struct driver_portaudio * self = vio->inst;
154
155 // TODO: cleanup common code.
156
157#ifdef ROAR_HAVE_LIBPABLIO
158 CloseAudioStream(self->ostream);
159
160 Pa_Terminate();
161
162 roar_mm_free(self);
163
164 return 0;
165#elif defined(ROAR_HAVE_PA19_VERSION_19)
166 if ( (self != NULL) && (self->stream != NULL) ) {
167  Pa_StopStream(self->stream);
168  Pa_CloseStream(self->stream);
169 }
170
171 roar_mm_free(self);
172
173 Pa_Terminate();
174
175 return 0;
176#else
177 return -1;
178#endif
179}
180
181ssize_t driver_portaudio_write        (struct roar_vio_calls * vio, void *buf, size_t count) {
182 struct driver_portaudio * self = vio->inst;
183#ifdef ROAR_HAVE_PA19_VERSION_19
184 size_t write_frames = count / self->framesize;
185 PaError err;
186#endif
187
188 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
189
190#ifdef ROAR_HAVE_LIBPABLIO
191 count /= self->ostream->bytesPerFrame; // TODO: FIXME: do not access private members
192 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, count=%llu) = ? // PABLIO mode", vio, buf, (long long unsigned int)count);
193 return WriteAudioStream(self->ostream, buf, count) * self->ostream->bytesPerFrame;
194#elif defined(ROAR_HAVE_PA19_VERSION_19)
195
196 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, size=%llu) = ?", vio, buf, (long long unsigned int)size);
197
198 // I'm not 100% sure if you could write arbitrary number of frames to Pa_WriteStream(), but it seems to be backend dependent.
199 err = Pa_WriteStream(self->stream, buf, write_frames);
200
201 if ( err < 0 && err != paOutputUnderflowed )
202  return -1;
203
204 // PA always seems to write requested size, or it will error out.
205 return count;
206#else
207 return -1;
208#endif
209}
210
211#endif
212
213//ll
Note: See TracBrowser for help on using the repository browser.