Class: Portaudio

Inherits:
Object
  • Object
show all
Defined in:
ext/portaudio/portaudio.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(framesPerBuffer, deviceName) ⇒ Object



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
# File 'ext/portaudio/portaudio.c', line 56

VALUE rb_portaudio_new(VALUE klass, VALUE framesPerBuffer, VALUE deviceName)
{
  PaStreamParameters outputParameters;
  const PaDeviceInfo *deviceInfo;
  PaError err;
  int device, numDevices, foundDevice = -1;
  VALUE self;
  Portaudio *portaudio = (Portaudio *) malloc(sizeof(Portaudio));

  pthread_mutex_init(&portaudio->mutex, NULL);
  pthread_cond_init(&portaudio->cond, NULL);

  portaudio->size = FIX2INT(framesPerBuffer) * 2;
  portaudio->buffer = (float *) malloc(sizeof(float) * portaudio->size);
	numDevices = Pa_GetDeviceCount();
	if( numDevices < 0 ) {
		printf( "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
		err = numDevices;
	}

	for ( device=0; device<numDevices; device++ ) {
		deviceInfo = Pa_GetDeviceInfo( device );
		if (strcmp(deviceInfo->name, StringValueCStr(deviceName)) == 0) {
			foundDevice = device;
			break;
		}
	}

	if (foundDevice == -1) {
		err = Pa_OpenDefaultStream(&portaudio->stream,
				0,           /* no input channels */
				2,           /* stereo output */
				paFloat32,   /* 32 bit floating point output */
				44100,       /* sample rate*/
				FIX2INT(framesPerBuffer),
				paCallback,
				(void*) portaudio);
	} else {
		bzero( &outputParameters, sizeof( outputParameters ) );
		outputParameters.device = foundDevice;
		outputParameters.channelCount = 2;
		outputParameters.sampleFormat = paFloat32;
		outputParameters.suggestedLatency = Pa_GetDeviceInfo(foundDevice)->defaultLowOutputLatency ;

		err = Pa_OpenStream(&portaudio->stream,
				NULL,
				&outputParameters,
				44100,
				FIX2INT(framesPerBuffer),
				paNoFlag,
				paCallback,
				(void*) portaudio);
	}

	if (err != paNoError) {
		rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
	}

  self = Data_Wrap_Struct(rb_cPortaudio, 0, free_portaudio, portaudio);

  return self;
}

Instance Method Details

#abortObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'ext/portaudio/portaudio.c', line 236

VALUE rb_portaudio_abort(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  int err = Pa_AbortStream(portaudio->stream);

  pthread_cond_broadcast(&portaudio->cond);

  if (err != paNoError) {
    rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
  }

  return self;
}

#closeObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/portaudio/portaudio.c', line 266

VALUE rb_portaudio_close(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  int err = Pa_CloseStream(portaudio->stream);

  pthread_cond_broadcast(&portaudio->cond);

  if (err != paNoError) {
    rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
  }

  return self;
}

#rmsObject



198
199
200
201
202
203
# File 'ext/portaudio/portaudio.c', line 198

VALUE rb_portaudio_rms(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  return rb_float_new(portaudio->rms);
}

#startObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'ext/portaudio/portaudio.c', line 205

VALUE rb_portaudio_start(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  int err = Pa_StartStream(portaudio->stream);

  pthread_cond_broadcast(&portaudio->cond);

  if (err != paNoError) {
    rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
  }

  return self;
}

#stopObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/portaudio/portaudio.c', line 220

VALUE rb_portaudio_stop(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  int err = Pa_StopStream(portaudio->stream);

  pthread_cond_broadcast(&portaudio->cond);

  if (err != paNoError) {
    rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
  }

  return self;
}

#stopped?Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'ext/portaudio/portaudio.c', line 251

VALUE rb_portaudio_stream_stopped(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);
  int err = Pa_IsStreamStopped(portaudio->stream);

  if (err == 1) {
    return Qtrue;
  } else if (err == 0) {
    return Qfalse;
  }

  rb_raise(rb_eStandardError, "%s", Pa_GetErrorText(err));
}

#waitObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'ext/portaudio/portaudio.c', line 164

VALUE rb_portaudio_wait(VALUE self)
{
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);

#ifdef RUBY_UBF_IO
  rb_thread_call_without_gvl(portaudio_wait, portaudio, RUBY_UBF_IO, NULL);
#else
  portaudio_wait(portaudio);
#endif
  return self;
}

#write(buffer) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/portaudio/portaudio.c', line 177

VALUE rb_portaudio_write(VALUE self, VALUE buffer)
{
  int i, len;
  Portaudio *portaudio;
  Data_Get_Struct(self, Portaudio, portaudio);

  Check_Type(buffer, T_ARRAY);

  len = RARRAY_LEN(buffer);

  if (len != portaudio->size) {
    rb_raise(rb_eStandardError, "array length does not match buffer size: %d != %d", len, portaudio->size);
  }

  for (i = 0; i < len; i++) {
    portaudio->buffer[i] = NUM2DBL(rb_ary_entry(buffer, i));
  }

  return self;
}

#write_from_mpg(mpg) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/portaudio/portaudio.c', line 141

VALUE rb_portaudio_write_from_mpg(VALUE self, VALUE mpg)
{
  int err;
  size_t done = 0;
  Portaudio *portaudio;
  mpg123_handle *mh = NULL;

  Data_Get_Struct(self, Portaudio, portaudio);
  Data_Get_Struct(mpg, mpg123_handle, mh);

  err = mpg123_read(mh, (unsigned char *) portaudio->buffer, portaudio->size * sizeof(float), &done);

  portaudio->rms = rms(portaudio->buffer, portaudio->size);

  switch (err) {
    case MPG123_OK: return ID2SYM(rb_intern("ok"));
    case MPG123_DONE: return ID2SYM(rb_intern("done"));
    case MPG123_NEED_MORE: return ID2SYM(rb_intern("need_more"));
  }

  rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err));
}