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



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

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

#startObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/portaudio/portaudio.c', line 124

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/portaudio/portaudio.c', line 139

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

#write(buffer) ⇒ Object



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

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

#ifdef RUBY_UBF_IO
  rb_thread_blocking_region(portaudio_wait, portaudio, RUBY_UBF_IO, NULL);
#else
  portaudio_wait(portaudio);
#endif

  return self;
}