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

  • SEARCH
  • Home
  • 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 8720597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:04:42+00:00 2026-06-13T07:04:42+00:00

I’ve been playing around with ruby recently, and I decided to start a simple

  • 0

I’ve been playing around with ruby recently, and I decided to start a simple project to write a ruby script that records line-in sound to a .wav file. I discovered that ruby doesn’t provide very good access to hardware devices (and it probably shouldn’t), but that PortAudio does, and I discovered a great wrapper for PA here (it is not a gem, I think because it uses ruby’s ffi to attach to PortAudio, and the PA library could be in a variety of places). I’ve been muddling through PortAudio’s documentation and examples to figure out how PA works. I haven’t written or read C in years.

I’m running into difficulty with what parameters I should be passing to a stream during creation, and a buffer during creation. For example, what exactly is a frame, and how is it related to other parameters like channel and sample rate. I’m totally new to audio programming in general as well, so if anyone could point me to some general tutorials, etc, about device level audio, I’d appreciate it.

ruby-portaudio provides a single example that creates a stream and a buffer, writes a sin wave to the buffer, then sends the buffer to the stream to be played. Some of the ruby I’m having trouble with in the example, specifically the loop block.

  PortAudio.init

  block_size = 1024
  sr   = 44100
  step = 1.0/sr
  time = 0.0

  stream = PortAudio::Stream.open(
             :sample_rate => sr,
             :frames => block_size,
             :output => {
               :device => PortAudio::Device.default_output,
               :channels => 1,
               :sample_format => :float32
              })

  buffer = PortAudio::SampleBuffer.new(
             :format   => :float32,
             :channels => 1,
             :frames   => block_size)

  playing = true
  Signal.trap('INT') { playing = false }
  puts "Ctrl-C to exit"

  stream.start

  loop do
    stream << buffer.fill { |frame, channel|
      time += step
      Math.cos(time * 2 * Math::PI * 440.0) * Math.cos(time * 2 * Math::PI)
    }

    break unless playing
  end

  stream.stop

If I’m going to be recording, I should be reading a stream into a buffer, then manipulating that buffer and writing it to file, right?

Also, if I’m barking up the wrong tree here, and there is an easier way to do this (in ruby), some direction would be nice.

  • 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-06-13T07:04:43+00:00Added an answer on June 13, 2026 at 7:04 am

    Let’s first clarify the terms you were asking about. For this purpose i will try to explain the audio pipeline in a simplified way. When you are generating a sound as in your example, your sound card periodically requests frames (= buffers = blocks) from your code, which you fill with your samples. The sampling rate defines how many samples you provide within a second and thus the speed with which your samples are played back. The frame size (= buffer size = block size) determines how many samples you provide in one request from the sound card. A buffer is typically quite small, because the buffer size directly affects the latency (large buffer => high latency) and large arrays can be slow (especially ruby arrays are slow).

    Similar things happen when you are recording sound from your sound card. Your function gets called every now and then, and the samples from the microphone are typically passed in as an argument to the function (or even just a reference to such a buffer). You are then expected to process these samples, e.g. by writing them to disk.

    I know that the thought of “doing everything in Ruby” is quite tempting, because it is such a beautiful language. When you are planning on doing audio processing in real time, i would recommend to switch to a compiled language (C, C++, Obj-C, …) though. These can handle audio much better, because they’re much closer to the hardware than Ruby and thus generally faster, which can be quite an issue in audio processing. This is probably also the reason why there are so few Ruby audio libraries around, so maybe Ruby just isn’t the right tool for the job.

    By the way, i tried out ruby-portaudio, ffi-portaudio as well as ruby-audio and none of them were working properly on my Macbook (tried to generate a sine wave) which sadly shows again, how Ruby is not capable of handling this stuff (yet?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.