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) ⇒ Object



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

VALUE rb_portaudio_new(VALUE klass, VALUE framesPerBuffer)
{
  PaStreamParameters outputParameters;
  const PaDeviceInfo *deviceInfo;
  PaError err;
  int device, numDevices;
  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);

  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);

  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

#rmsObject



167
168
169
170
171
172
# File 'ext/portaudio/portaudio.c', line 167

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

#startObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/portaudio/portaudio.c', line 174

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/portaudio/portaudio.c', line 189

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;
}

#waitObject



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/portaudio/portaudio.c', line 132

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

  Data_Get_Struct(self, Portaudio, portaudio);
#ifdef RUBY_UBF_IO
  rb_thread_blocking_region(portaudio_wait, portaudio, RUBY_UBF_IO, NULL);
#else
  portaudio_wait(portaudio);
#endif
  return self;
}

#write(buffer) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/portaudio/portaudio.c', line 146

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/portaudio/portaudio.c', line 110

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"));
  }

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