Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:16:09+00:00 2026-05-19T02:16:09+00:00

I finally got my code to reference the ruby-alsa library , but I’m stuck

  • 0

I finally got my code to reference the ruby-alsa library, but I’m stuck again. Eventually, what I’d like to happen is have an audio file play on the server when an action is invoked from the client side. As such, I have the following code in my controller:

File.open('./public/audio/test.wav', 'r') do |f|
  ALSA::PCM::Playback.open do |playback|
    playback.write do |length|
      f.read length
    end
  end
end

By reading the RDoc for the ALSA library, I get the impression I should be using the write_in_background method from the ALSA::PCM::Playback class. Unfortunately, I can’t get the syntax right for getting this method to work. To simply verify I have ALSA working properly, I attempted to use the Playback.write method (note code above). The above code is syntactically correct; however, it plays a sound only for a microsecond, then stops. My guess is the request ends so quickly, it doesn’t have enough time to play anything recognizable.

As previously mentioned, my ultimate goal is to have an end-user invoke an action that plays audio back on the server. The file should not stop playing at the end of the HTTP request –it should continue until another action is invoked that stops playback. Knowing that, could somebody please help me with getting the syntax and parameters right for calling the write_in_background method? I’m afraid the ruby-alsa documentation I have at this time hasn’t been helpful enough for me (as a complete newbie at Ruby).

Update: If I replace the above call to the write method with a call to the write_to_background method, I get the following runtime error: cannot add pcm handler (Function not implemented)

Update 2: I tried this with a different WAV file and the following code and it plays at warp speed.

File.open('./public/audio/sample.wav', 'r') do |f|
  ALSA::PCM::Playback.open do |playback|
    playback.write do |length|
      @temp = length
      f.read length
    end
    sleep 4
  end
end

It appears there may be a combination of things going on here. I believe the first is in reference to the sample rate (length == 44100, which is CD quality). I’ll have to look into how to play back audio files at a different rate. Beyond that, however, I’m still stuck on how to get it to play in the background. While the sleep proves ALSA is working, it won’t work well in a real-world scenario.

Update 3: Got the sample rate bit working even though it temporarily relies on a magic number:

File.open('./public/audio/sample.wav', 'r') do |f|
  ALSA::PCM::Playback.open "default", {:channels => 1, :sample_rate => 11025} do |playback|
    playback.write do |length|
      @temp = length
      f.read length
    end
    sleep 4
  end
end

At this point, I have my ruby code playing audio through ALSA, but I’m not sure how to make it play continuously in the background without requiring the sleep.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:16:10+00:00Added an answer on May 19, 2026 at 2:16 am

    How about kicking it off on its own thread?

    Thread.new do
      File.open('myfile.wav', 'rb') do |f|
        ALSA::PCM::Playback.open do |playback|
          playback.write do |length|
            f.read length
         end
        end
      end
    end.join
    

    Edit:
    OK, if that doesn’t work, I’m guessing it kicks off its own thread already. How about sleeping for the duration? First try:

      File.open('myfile.wav', 'rb') do |f|
        ALSA::PCM::Playback.open do |playback|
          playback.write do |length|
            f.read length
         end
        end
      end
      sleep 4 # seconds
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing server side code i finally got my eyes X-crossed trying to write -
I was given this code a while back. I finally got around to testing
I have finally got the green light to use Memcached in our project (after
I've have finally got the datepicker to work on my MVC demo site. One
I finally got my group to switch from SourceSafe to Subversion. Unfortunately, my manager
I've finally got the datepicker to work on my MVC demo site and also
So I've finally got my WCF service setup and talking to browser via ASP.NET
I finally have a hardware guy that is insterested in controlling the firmware. This
I'm finally getting my team to embrace source code management now that we're working
I've finally got Intellisense working for JQuery by applying patch KB958502 to Visual Studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.