output data to audio

Discussion in 'Perl Misc' started by Alx, May 22, 2004.

  1. Alx

    Alx Guest

    I was playing around yesterday, and I came out with a Perlish version
    of a simple Cellular Automata.
    Apart from visualizing the result (thank you, GD), I wanted to
    "listen" the result of the evolution of the automata as an audio file.
    After a day browsing CPAN, I say that I'm a little disappointed: I
    didn't find any <simple> way to output data (e.g. some @CA=(numbers,
    numbers, numbers,...) in .wav or other format.
    Yes, there is a ton of Audio::... modules.
    Yes, there are Audio::Wav and Audio::Data.
    Yet, for a simple layman as me who does not recognize a bitrate from
    some other beast, the docs of those modules are really hard to master.

    Do you know of some other way to do it?

    Thanks!

    Alessandro Magni
     
    Alx, May 22, 2004
    #1
    1. Advertising

  2. Alx

    rduke15 Guest


    > Apart from visualizing the result (thank you, GD), I wanted to
    > "listen" the result of the evolution of the automata as an audio file.
    > After a day browsing CPAN, I say that I'm a little disappointed: I
    > didn't find any <simple> way to output data (e.g. some @CA=(numbers,
    > numbers, numbers,...) in .wav or other format.
    > Yes, there is a ton of Audio::... modules.
    > Yes, there are Audio::Wav and Audio::Data.
    > Yet, for a simple layman as me who does not recognize a bitrate from
    > some other beast, the docs of those modules are really hard to master.
    >
    > Do you know of some other way to do it?


    I suppose your problem is not putting out the audio itself, but
    packaging it into some standard container like WAV?

    WAV is one of the types that can go into a RIFF file. It would not be
    hard to build the file yourself.

    You can start here:

    http://ccrma.stanford.edu/courses/422/projects/WaveFormat/

    or here:

    http://netghost.narod.ru/gff/graphics/summary/micriff.htm

    You may also find riffwalk.exe useful for debugging if you have a
    Windows box. There seems to be a copy here:
    http://www.xiph.org/archives/vorbis-dev/200108/att-0246/01-RIFFWALK.EXE
     
    rduke15, May 22, 2004
    #2
    1. Advertising

  3. Alx

    Ben Morrow Guest

    Quoth (Alx):
    > I was playing around yesterday, and I came out with a Perlish version
    > of a simple Cellular Automata.
    > Apart from visualizing the result (thank you, GD), I wanted to
    > "listen" the result of the evolution of the automata as an audio file.
    > After a day browsing CPAN, I say that I'm a little disappointed: I
    > didn't find any <simple> way to output data (e.g. some @CA=(numbers,
    > numbers, numbers,...) in .wav or other format.
    > Yes, there is a ton of Audio::... modules.
    > Yes, there are Audio::Wav and Audio::Data.
    > Yet, for a simple layman as me who does not recognize a bitrate from
    > some other beast, the docs of those modules are really hard to master.


    I would say from a quick look at the docs that you might want something
    like this:

    use Audio::Data;

    # $rate gives the number of samples per second: in other words, with a
    # sample rate of 44100 (which is standard CD-quality) a data list of
    # 44100 points will create one second of audio. Making this quantity
    # larger will make the sound both faster, higher in pitch and better
    # quality (there will be less 'buzz' in the sound); making it smaller
    # will do the reverse.

    my $rate = 44100;

    # As Audio::Data deals with float values rather than fixed- or
    # variable-sized int values you don't have to worry about bitrates.

    my $au = Audio::Data->new(rate => $rate);

    my @data = (list of floating-point values between -1.0 and 1.0);
    $au->data(@data);

    # The above will use the numbers directly as samples. You may get more
    # harmonious results (you will also lose the dependancy on sample rate
    # mentioned above) by using $au->tone to create, say, tenth-second
    # samples whose pitch depends on your automaton and then adding all the
    # samples for each time period with the overloaded + and joining the
    # time periods together with the overloaded . . Say your automaton is a
    # function which can be called
    # @state = autom @state;
    # to derive the state at a given time-period from the one before; then
    # you could do something like

    use List::Util qw/reduce/;

    @state = (initial conditions);
    for (1..100) {
    my @au = map {
    my $new = Audio::Data->new(rate => $rate);
    $new->tone($_, 0.1, 0.5);
    $new;
    } @state;
    $au .= reduce { $a + $b } @au;
    @state = autom @state;
    }

    # Note also that human perception of both pitch and amplitude is
    # logarithmic, so you may well find ->tone(2**$_, works better...
    # obviously this will all need lots of seasoning to taste :)

    {
    # This creates a Sun .au file. If you need M$ wav then you can use
    # sox to convert it.

    open my $AU, '>', 'file.au' or die "can't create file.au: $!";
    $au->Save($AU);
    }

    # Caveat: the above is all *entirely* untested.

    Ben

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
     
    Ben Morrow, May 23, 2004
    #3
  4. Alx

    Alx Guest

    Thank you people, you already have been more than useful - Ben, I find
    now after your explanation that Audio::Data can be simpler than I
    thought (but what an awful perldoc!).
    Going on harder and harder questions ... mind you, I know nothing
    about audio so the question may be silly:
    if you had to generate sounds from data with the timber of a given
    instrument - what would you do? I'd really like my cellular automata
    to play the sax à la Lester Young !

    Thx

    Alessandro Magni
     
    Alx, May 23, 2004
    #4
  5. Alx

    Alx Guest

    ..
    ..
    ..

    P.S. I might add that I cannot play MIDI files on my soundcard - so
    writing to MIDI cannot work for me!


    Alessandro Magni
     
    Alx, May 23, 2004
    #5
  6. Alx

    Ben Morrow Guest

    Quoth (Alx):
    > if you had to generate sounds from data with the timber of a given


    (just curious: did you mean 'timbre' or do Americans spell it that way?)

    > instrument - what would you do? I'd really like my cellular automata
    > to play the sax à la Lester Young !


    A simple approach is to make a short recording of a single note on the
    instrument concerned and loop it to the appropriate length. The
    beginning and end of the note should be cut off, and stuck back on after
    the looping, so that it sounds (relatively) smooth. The pitch can be
    varied with the $au * $scalar and $au / $scalar operators; though the
    calculation of the factors required may get a little involved.

    I know there are many more sophisticated techniques, and there may be
    ways of achieving a better result with less work, but at this point I'm
    really getting out of my depth...

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, May 23, 2004
    #6
  7. Alx

    Alythh Guest

    Ben Morrow <> wrote in message news:<c8puai$b$>...
    > Quoth (Alx):
    > > if you had to generate sounds from data with the timber of a given

    >
    > (just curious: did you mean 'timbre' or do Americans spell it that way?)


    Italians that do not know well english language often use dict:
    dict timber
    ....
    5: (music) the distinctive property of a complex sound (a voice
    or noise or musical sound); "the timbre of her soprano was
    rich and lovely"; "the muffled tones of the broken bell
    summoned them to meet" [syn: {timbre}, {quality}, {tone}]

    BUT, checking dict timbre I saw that same definition - so we have a
    case similar to neighbor/neighbout I guess...

    PS thanks for your help on Audio::Data ! Unfortunately I cannot make
    it on my Knoppix machine (due to bug
    http://rt.cpan.org/NoAuth/Bug.html?id=5384),
    I'll see if Audio::Wav works better

    Alessandro Magni
     
    Alythh, May 24, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    466
  2. Pan Xingzhi

    Python audio output switch

    Pan Xingzhi, Jul 25, 2006, in forum: Python
    Replies:
    2
    Views:
    451
    Pan Xingzhi
    Jul 26, 2006
  3. aeonsun
    Replies:
    2
    Views:
    434
    Luc The Perverse
    Mar 11, 2007
  4. Udhay
    Replies:
    3
    Views:
    355
    Ian Collins
    Apr 12, 2007
  5. sick dick
    Replies:
    0
    Views:
    393
    sick dick
    Apr 3, 2006
Loading...

Share This Page