output data to audio

A

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.

Do you know of some other way to do it?

Thanks!

Alessandro Magni
 
R

rduke15

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
 
B

Ben Morrow

Quoth (e-mail address removed) (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
 
A

Alx

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
 
A

Alx

..
..
..

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


Alessandro Magni
 
B

Ben Morrow

Quoth (e-mail address removed) (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
 
A

Alythh

Ben Morrow said:
Quoth (e-mail address removed) (Alx):

(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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top