source: roaraudio/roard/driver_portaudio.c @ 4857:de849b04df05

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

test if paPackedInt24 is supported (defined)

File size: 5.6 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#ifdef paPackedInt24
68  case 24:
69    if ( info->codec != ROAR_CODEC_DEFAULT )
70     return -1;
71    fmt = paPackedInt24;
72   break;
73#endif
74  case 32:
75    if ( info->codec != ROAR_CODEC_DEFAULT )
76     return -1;
77    fmt = paInt32;
78   break;
79  default:
80    return -1;
81 }
82
83 if ( (self = roar_mm_malloc(sizeof(struct driver_portaudio))) == NULL )
84  return -1;
85
86 memset(self, 0, sizeof(struct driver_portaudio));
87
88 memset(inst, 0, sizeof(struct roar_vio_calls));
89
90 inst->inst  = self;
91 inst->close = driver_portaudio_close;
92 inst->write = driver_portaudio_write;
93
94 Pa_Initialize();
95
96#ifdef ROAR_HAVE_LIBPABLIO
97 switch (info->channels) {
98  case 1: flags |= PABLIO_MONO;   break;
99  case 2: flags |= PABLIO_STEREO; break;
100  default:
101    roar_mm_free(self);
102    Pa_Terminate();
103    return -1;
104 }
105
106 if ( OpenAudioStream(&(self->ostream), info->rate, fmt, flags) != paNoError ) {
107  roar_mm_free(self);
108  Pa_Terminate();
109  return -1;
110 }
111
112 return 0;
113#elif defined(ROAR_HAVE_PA19_VERSION_19)
114 params.device                    = Pa_GetDefaultOutputDevice();
115 params.channelCount              = info->channels;
116 params.sampleFormat              = fmt;
117 params.suggestedLatency          = Pa_GetDeviceInfo(params.device)->defaultLowOutputLatency;
118 params.hostApiSpecificStreamInfo = NULL;
119
120 // TODO: FIXME: use libroar for this.
121 self->framesize = info->bits * info->channels / 8;
122
123 // Sets up blocking I/O stream.
124 err = Pa_OpenStream(&(self->stream),
125                     NULL,
126                     &params,
127                     info->rate,
128                     128 /*FIXME:frames*/,
129                     paClipOff,
130                     NULL,
131                     NULL
132                    );
133
134 if ( err != paNoError ) {
135  ROAR_ERR("driver_portaudio_open(*): Could not open PortAudio device: \"%s\".", Pa_GetErrorText(err));
136  roar_mm_free(self);
137  return -1;
138 }
139
140 err = Pa_StartStream(self->stream);
141
142 if ( err != paNoError ) {
143  ROAR_ERR("driver_portaudio_open(*): Could not start stream: \"%s\".", Pa_GetErrorText(err));
144  roar_mm_free(self);
145  return -1;
146 }
147
148 return 0;
149#else
150 return -1;
151#endif
152}
153
154int     driver_portaudio_close        (struct roar_vio_calls * vio) {
155 struct driver_portaudio * self = vio->inst;
156
157 // TODO: cleanup common code.
158
159#ifdef ROAR_HAVE_LIBPABLIO
160 CloseAudioStream(self->ostream);
161
162 Pa_Terminate();
163
164 roar_mm_free(self);
165
166 return 0;
167#elif defined(ROAR_HAVE_PA19_VERSION_19)
168 if ( (self != NULL) && (self->stream != NULL) ) {
169  Pa_StopStream(self->stream);
170  Pa_CloseStream(self->stream);
171 }
172
173 roar_mm_free(self);
174
175 Pa_Terminate();
176
177 return 0;
178#else
179 return -1;
180#endif
181}
182
183ssize_t driver_portaudio_write        (struct roar_vio_calls * vio, void *buf, size_t count) {
184 struct driver_portaudio * self = vio->inst;
185#ifdef ROAR_HAVE_PA19_VERSION_19
186 size_t write_frames = count / self->framesize;
187 PaError err;
188#endif
189
190 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
191
192#ifdef ROAR_HAVE_LIBPABLIO
193 count /= self->ostream->bytesPerFrame; // TODO: FIXME: do not access private members
194 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, count=%llu) = ? // PABLIO mode", vio, buf, (long long unsigned int)count);
195 return WriteAudioStream(self->ostream, buf, count) * self->ostream->bytesPerFrame;
196#elif defined(ROAR_HAVE_PA19_VERSION_19)
197
198 ROAR_DBG("driver_portaudio_write(vio=%p, buf=%p, size=%llu) = ?", vio, buf, (long long unsigned int)size);
199
200 // I'm not 100% sure if you could write arbitrary number of frames to Pa_WriteStream(), but it seems to be backend dependent.
201 err = Pa_WriteStream(self->stream, buf, write_frames);
202
203 if ( err < 0 && err != paOutputUnderflowed )
204  return -1;
205
206 // PA always seems to write requested size, or it will error out.
207 return count;
208#else
209 return -1;
210#endif
211}
212
213#endif
214
215//ll
Note: See TracBrowser for help on using the repository browser.