0
votes

I'm attempting to use the ruby-alsa gem to provide audio playback on the server. Unfortunately, I keep getting an uninitialized constant MyClass::Playback exception when attempting to do so.

I'm very new to Ruby and Rails, so I'm not sure how to resolve this issue. The following has been added to my Gemfile and I have run a bundle install:

gem 'ruby-alsa'

My controller code looks like this (though I can't even begin to guarantee the validity of the code):

# Test audio playback
file = File.open("sample.wav")
#ALSA::PCM::Playback.open do |playback| # This line is commented out because it didn't work
Playback.open do |playback|
  playback.write do |length|
    file.read length
  end
end

file.close

Update: If I uncomment the following line, I get the same exception (except ALSA is the uninitialized constant):

ALSA::PCM::Playback.open do |playback|
4

4 Answers

2
votes

Looking at this briefly, it looks like your Gemfile needs to be:

gem 'ruby-alsa', :require => 'alsa'
1
votes

It looks as if your commented-out code is correct; you should be using ALSA::PCM::Playback.

Your next problem is that write is an instance method of that class. As shown on the page you linked to, it appears that correct usage might be more like:

ALSA::PCM::Playback.open do |playback|
  playback.write do |length|
    file.read length
  end
end

(Caveat: I know nothing about ALSA or this gem, so I have no idea what the above should do.)

1
votes

Your code is inside a class so you need to do this:

::ALSA::PCM::Playback.open do |playback|

Note the preceding double colon.

1
votes

Did you try to require it ?

require 'ruby-alsa'

edit:

Try to require rubygems first

require 'rubygems'
require 'ruby-alsa'