Ruby Audio Library

advice an easy ruby multiplatform audio library or jruby 's java libs utilization?

just hacked one up: https://github.com/rdp/jruby-swing-helpers/blob/master/spec/play_mp3_audio.spec.rb

Possibly a error within ruby2d's library method regarding Audio input?

It's not a bug.

The Ruby2D::Sound class doesn't have a #pause method.

# frozen_string_literal: true

# Ruby2D::Sound

module Ruby2D
# Sounds are intended to be short samples, played without interruption, like an effect.
class Sound
attr_reader :path
attr_accessor :loop, :data

#
# Load a sound from a file
# @param [String] path File to load the sound from
# @param [true, false] loop If +true+ playback will loop automatically, default is +false+
# @raise [Error] if file cannot be found or music could not be successfully loaded.
def initialize(path, loop: false)
raise Error, "Cannot find audio file `#{path}`" unless File.exist? path

@path = path
@loop = loop
raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
end

# Play the sound
def play
ext_play
end

# Stop the sound
def stop
ext_stop
end

# Returns the length in seconds
def length
ext_length
end

# Get the volume of the sound
def volume
ext_get_volume
end

# Set the volume of the sound
def volume=(volume)
# Clamp value to between 0-100
ext_set_volume(volume.clamp(0, 100))
end

# Get the volume of the sound mixer
def self.mix_volume
ext_get_mix_volume
end

# Set the volume of the sound mixer
def self.mix_volume=(volume)
# Clamp value to between 0-100
ext_set_mix_volume(volume.clamp(0, 100))
end
end
end

Note:

Sounds are intended to be short samples, played without interruption, like an effect.

If you look at the linked example they are using the Music class which does.

song = Music.new('song.mp3')

# Play the music
song.play

# Pause the music
song.pause

The top level doc also states:

Music is for longer pieces which can be played, paused, stopped, resumed, and faded out, like a background soundtrack.

Gem installation ruby-audio-1.6.1 error(using Mac OS 10.9 / homebrew)

You probably need to install libsndfile as a universal library - by default it doesn't compile a 32-bit slice, which is required for compatibility with the system ruby.

brew uninstall libsndfile
brew install libsndfile --universal
sudo gem install ruby-audio

Using PortAudio wrapper in ruby to record sound to .wav

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?).

Read audio file metadata with ruby

Taglib has Ruby bindings and does what you want.

Gem installation ruby-audio failing

So, we got it working together, and I thought I'd share the steps I had to take to make this work. Thanks to everyone who commented!

  1. The problem was that while installing the gem, it couldn't find the libsndfile
  2. To install it, I had to run brew install libsndfile (install homebrew first if you don't have it already), which didn't work, so I had to...
  3. ... update Xcode to the latest version and install Xcodes command line tool (don't ask me why, but I had to.)
  4. Then I ran brew install libsndfile, which worked
  5. And last but not least I ran bundle install again, which finally installed the desired gem.


Related Topics



Leave a reply



Submit