personal ruby buddha machine

Nov 25 2008 Published by jone under Musikk, programming, ruby

Some days ago I came across a post on the FM3 buddha machine. Finding it a fascinating concept, and being able to download their samples I sprinkled a few lines of ruby over them an stirred. Voilà – my own personal buddha machine playing wonderful ambient soundscapes in a random fashion. Using the sdl_ruby library, the whole thing became surprisingly simple:

#!/usr/bin/ruby -w
require 'sdl'
SDL.init(SDL::INIT_AUDIO)
SDL::Mixer.open(44100,SDL::Mixer::DEFAULT_FORMAT,2,4096)
 
clips = Dir.new('audio').entries.delete_if {|x| ! (x =~ /wav$/)}
while true
        for i in 1..SDL::Mixer.allocate_channels(4)
                        if not SDL::Mixer.play?(i)
                                SDL::Mixer.playChannel(i,SDL::Mixer::Wave.load("audio/"+clips[rand(clips.length)]),rand(30))
                        end
        end
        sleep 0.5
end

No responses yet

Numerical sorting in ruby

Nov 19 2008 Published by jone under ruby

Quick note on how to do numerical sorting in ruby.
When calling sort, ruby will perform a lexicographical sorting by default, meaning that the following array

1
2
a=["0","10","9","1"]
a.sort.each{|val| p val}

will output

"0"
"1"
"10"
"9"

If this is not what you want, this might be it:

1
a.sort{|x,y| x.to_i < => y.to_i}.each { |val| p val }

outputs:

"0"
"1"
"9"
"10"

Yay, blocks to the rescue:)

No responses yet

Modular Architectures with Ruby

May 15 2007 Published by jone under Uncategorized

Modular Architectures with Ruby

Litt om factory-pattern

No responses yet

Ruby – inherited

May 10 2007 Published by jone under Uncategorized

Av og til kommer man over småting i nye språk som er ganske kjekke: Class.inherited i ruby er en metode som kalles i superklassen til et arvet object, noe som gir muligheter til å gjøre ting som er kule. La meg tenke litt på det, så kommer det nok noen rare eksempler:)

No responses yet

Obligatoriske eclipseplugins

Jan 27 2007 Published by jone under Uncategorized

No responses yet

Hvor skal jeg putte koden min?

Nov 01 2006 Published by jone under Uncategorized

Her får du noen idèer: the { buckblogs :here }: Skinny Controller, Fat Model

No responses yet

[Rails] select-multiple for has_many association

Nov 01 2006 Published by jone under Uncategorized

[Rails] select-multiple for has_many association
Kort gjenfortalt :

< %= collection_select :person, :task_ids,
Task.find(:all), :id, :description, {},
:multiple => true,
:name => 'person[task_ids][]' %>

No responses yet

Acts As Dropdown

Jul 19 2006 Published by jone under Uncategorized

Acts As Dropdown er en rails-plugin som gjør det svært enkelt (og lesbart) å lage select-menyer. Får min foreløpige anbefaling:)

No responses yet