Discussion:
[chuck-users] New to ChucK: can this be done easily?
Forrest Cahoon
2018-06-10 21:35:19 UTC
Permalink
Hi ChucKers!

I have a sonification project that I'm wondering if it makes sense to do in
ChucK.

I have a csv file with frequencies and amplitudes for 4 different sine
waves, representing values at (say) half-second intervals. I want to read
in a line, and then smoothly change the frequency and amplitude values
using linear interpolation over the course of 24000 samples (a half second
at 48k samples per second), then read in the next line, and smoothly change
to those values over the next half-second, etc. until the data runs out.

I could code this in C using libsndfile, but if I didn't have to worry
about converting frequencies into phase distances between samples I'd
consider that a win.

I noticed Chuck has FileIO methods to read input files (although these are
very poorly documented) so thought it should be possible to read in a csv
file.

I could calculate the amount of frequency and amplitude change per sample
and adjust these in a 1-sample loop, and that's already simpler than coding
in C. But I'm hoping there's some way to tell ChucK "I want this (e.g.
frequency) value to change smoothly from its current value to f1 over the
course of a half-second". If that were possible, ChucK would be a clear
choice for my project.

Thanks for any response!

Forrest Cahoon
mario.buoninfante
2018-06-10 22:08:55 UTC
Permalink
Hi,

I've never tried to use CSV files with ChucK, an alternative could be use a common txt file with FileIO like you said.
About the interpolation an easy way could be using Step and Envelope to set both freq and amplitude of the oscillators.

Cheers,
Mario
Post by Forrest Cahoon
Hi ChucKers!
I have a sonification project that I'm wondering if it makes sense to do in ChucK.
I have a csv file with frequencies and amplitudes for 4 different sine waves, representing values at (say) half-second intervals. I want to read in a line, and then smoothly change the frequency and amplitude values using linear interpolation over the course of 24000 samples (a half second at 48k samples per second), then read in the next line, and smoothly change to those values over the next half-second, etc. until the data runs out.
I could code this in C using libsndfile, but if I didn't have to worry about converting frequencies into phase distances between samples I'd consider that a win.
I noticed Chuck has FileIO methods to read input files (although these are very poorly documented) so thought it should be possible to read in a csv file.
I could calculate the amount of frequency and amplitude change per sample and adjust these in a 1-sample loop, and that's already simpler than coding in C. But I'm hoping there's some way to tell ChucK "I want this (e.g. frequency) value to change smoothly from its current value to f1 over the course of a half-second". If that were possible, ChucK would be a clear choice for my project.
Thanks for any response!
Forrest Cahoon
Stephen D Beck
2018-06-10 22:15:41 UTC
Permalink
This should be very easy. CSV files are common text files, with data separated by commas, hence the name Comma-Separated-Values. I've never done this before, but it seems like this would be pretty straight forward, and something I should have done!

Let us know how it works.
Steve
________________________________________
From: chuck-users-***@lists.cs.princeton.edu <chuck-users-***@lists.cs.princeton.edu> on behalf of mario.buoninfante <***@gmail.com>
Sent: Sunday, June 10, 2018 5:08:55 PM
To: Forrest; chuck-users
Subject: Re: [chuck-users] New to ChucK: can this be done easily?

Hi,

I've never tried to use CSV files with ChucK, an alternative could be use a common txt file with FileIO like you said.
About the interpolation an easy way could be using Step and Envelope to set both freq and amplitude of the oscillators.

Cheers,
Mario

Sent from my Wiko ROBBY

On Jun 10, 2018 22:35, Forrest Cahoon <***@gmail.com> wrote:
Hi ChucKers!

I have a sonification project that I'm wondering if it makes sense to do in ChucK.

I have a csv file with frequencies and amplitudes for 4 different sine waves, representing values at (say) half-second intervals. I want to read in a line, and then smoothly change the frequency and amplitude values using linear interpolation over the course of 24000 samples (a half second at 48k samples per second), then read in the next line, and smoothly change to those values over the next half-second, etc. until the data runs out.

I could code this in C using libsndfile, but if I didn't have to worry about converting frequencies into phase distances between samples I'd consider that a win.

I noticed Chuck has FileIO methods to read input files (although these are very poorly documented) so thought it should be possible to read in a csv file.

I could calculate the amount of frequency and amplitude change per sample and adjust these in a 1-sample loop, and that's already simpler than coding in C. But I'm hoping there's some way to tell ChucK "I want this (e.g. frequency) value to change smoothly from its current value to f1 over the course of a half-second". If that were possible, ChucK would be a clear choice for my project.

Thanks for any response!

Forrest Cahoon
Joel Matthys
2018-06-10 22:57:21 UTC
Permalink
Hello. The StringTokenizer will preserve the comma (it looks to be hard-coded to separate at spaces) but it looks like Std.atof will ignore it.
<<< Std.atof( "100," ) >>>;
100.000000 :(float)
Here's a way to do those interpolating SinOscs:
/////////////////////////////
4 => int numOsc;
200::ms => dur rampSpeed;

SinOsc osc[numOsc];
Step st[numOsc];
Envelope freq[numOsc];
Envelope amp[numOsc];

Gain g => dac;
1.0 / numOsc => g.gain;

// initialize and connect oscillators
for (int i; i<numOsc; i++) {
st[i] => freq[i] => osc[i] => amp[i] => g;
1 => st[i].next; // need to send a 1 into the envelope to multiply freq values
rampSpeed => freq[i].duration => amp[i].duration;
}

while (true) {
for (int i; i<numOsc; i++) {
Math.random2f(200,800) => freq[i].target;
Math.random2f(0,1) => amp[i].target;
}
500::ms => now;
}

///////////////////////////
This should be very easy. CSV files are common text files, with data separated by commas, hence the name Comma-Separated-Values. I've never done this before, but it seems like this would be pretty straight forward, and something I should have done!
Let us know how it works.
Steve
________________________________________
Sent: Sunday, June 10, 2018 5:08:55 PM
To: Forrest; chuck-users
Subject: Re: [chuck-users] New to ChucK: can this be done easily?
Hi,
I've never tried to use CSV files with ChucK, an alternative could be use a common txt file with FileIO like you said.
About the interpolation an easy way could be using Step and Envelope to set both freq and amplitude of the oscillators.
Cheers,
Mario
Sent from my Wiko ROBBY
Hi ChucKers!
I have a sonification project that I'm wondering if it makes sense to do in ChucK.
I have a csv file with frequencies and amplitudes for 4 different sine waves, representing values at (say) half-second intervals. I want to read in a line, and then smoothly change the frequency and amplitude values using linear interpolation over the course of 24000 samples (a half second at 48k samples per second), then read in the next line, and smoothly change to those values over the next half-second, etc. until the data runs out.
I could code this in C using libsndfile, but if I didn't have to worry about converting frequencies into phase distances between samples I'd consider that a win.
I noticed Chuck has FileIO methods to read input files (although these are very poorly documented) so thought it should be possible to read in a csv file.
I could calculate the amount of frequency and amplitude change per sample and adjust these in a 1-sample loop, and that's already simpler than coding in C. But I'm hoping there's some way to tell ChucK "I want this (e.g. frequency) value to change smoothly from its current value to f1 over the course of a half-second". If that were possible, ChucK would be a clear choice for my project.
Thanks for any response!
Forrest Cahoon
_______________________________________________
chuck-users mailing list
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Forrest Cahoon
2018-06-19 15:50:11 UTC
Permalink
My sonification project is done! You can hear the results at
https://soundcloud.com/fcahoon/shumann-resonance-at-tomsk .

I've been manipulating the data a lot, so it was simple to switch to
tab-separated instead of comma-separated values, which
ChucK reads without issue. The first few lines of my data looks like

7.8975 6.0000 13.6870 4.6500 19.0720 4.7200 25.5000 2.6200
7.9165 6.0000 13.6030 4.6500 19.0300 4.7200 25.5500 2.6200
7.9355 7.2000 13.5400 4.1200 18.9670 4.3800 25.6000 2.6200
7.9640 7.2000 13.4980 4.1200 18.9250 4.3800 25.6250 2.6200
...
These are four frequency/amplitude pairs on each line.

I still did my interpolation sample-by-sample, but was not too difficult.
Here is my ChucK code (it seems short enough to post):

16.0 => float FREQ_MULT;
0.008 => float GAIN_MULT;
441 => int SAMPLES_PER_DATAPOINT;

SinOsc sig[4];

for ( 0 => int i ; i < sig.cap() ; i++) {
sig[i] => dac;
sig[i].gain(0);
}
// open file
me.sourceDir() + "tomsk_2017-05-10_000.dat" => string fname;
FileIO fio;
fio.open(fname, FileIO.READ);
if( !fio.good() )
{
cherr <= "can't open file: " <= fname <= " for reading..."
<= IO.newline();
me.exit();
} float old_freq[4];
float old_gain[4];
float new_freq[4];
float new_gain[4];
float freq_incr[4];
float gain_incr[4];

// read first line
for (0 => int i; i < 4; i++) {
fio => old_freq[i];
fio => old_gain[i];
}

while (fio.more()) {
// read next line
for (0 => int i; i < 4; i++) {
fio => new_freq[i];
fio => new_gain[i];
}

// calculate linear interpolation increments
for (0 => int i; i < 4; i++) {
(new_freq[i] - old_freq[i])/SAMPLES_PER_DATAPOINT => freq_incr[i];
(new_gain[i] - old_gain[i])/SAMPLES_PER_DATAPOINT => gain_incr[i];
}
// Interpolate all samples between datapoints:
for (0 => int step; step < SAMPLES_PER_DATAPOINT; step++) {
// calculate interpolated freqs and gains for this sample
for (0 => int i; i < 4; i++) {
(old_freq[i] + (step*freq_incr[i])) * FREQ_MULT => sig[i].freq;
(old_gain[i] + (step*gain_incr[i])) * GAIN_MULT => sig[i].gain;
}
//render the sample
1::samp => now;
}
// save last datapoint values read as "old" in preparation for
// reading the next set of "new" datapoint values
for (0 => int i; i < 4; i++) {
new_freq[i] => old_freq[i];
new_gain[i] => old_gain[i];
}
}

I'm sure my code must have some non-idiomatic or inefficient things in it;
it is my first real ChucK program. Any advice would be appreciated.
Joel Matthys
2018-06-10 22:13:49 UTC
Permalink
Hi Forrest.

Yes, it's quite possible in ChucK.
The FileIO method has several examples in the io/ folder. It's quite straightforward to use.
You may have to hack a tokenizer for csv. Have a look at strops.ck and strops2.ck in the string/ examples folder to see what's possible.
Joel
Post by Forrest Cahoon
Hi ChucKers!
I have a sonification project that I'm wondering if it makes sense to do in ChucK.
I have a csv file with frequencies and amplitudes for 4 different sine waves, representing values at (say) half-second intervals. I want to read in a line, and then smoothly change the frequency and amplitude values using linear interpolation over the course of 24000 samples (a half second at 48k samples per second), then read in the next line, and smoothly change to those values over the next half-second, etc. until the data runs out.
I could code this in C using libsndfile, but if I didn't have to worry about converting frequencies into phase distances between samples I'd consider that a win.
I noticed Chuck has FileIO methods to read input files (although these are very poorly documented) so thought it should be possible to read in a csv file.
I could calculate the amount of frequency and amplitude change per sample and adjust these in a 1-sample loop, and that's already simpler than coding in C. But I'm hoping there's some way to tell ChucK "I want this (e.g. frequency) value to change smoothly from its current value to f1 over the course of a half-second". If that were possible, ChucK would be a clear choice for my project.
Thanks for any response!
Forrest Cahoon
_______________________________________________
chuck-users mailing list
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Morgan Utting
2018-06-10 22:50:00 UTC
Permalink
Hey there,
I did a csv project a while back at uni for a sonification class. It's not
quite the same thing as what you need but I think it might help. The
project took the csv lat and long of photos from the first ISS mission and
sonified them into sines. The project writeup in the folder explains more,
but the main part is that fileIO worked really nicely and should work well
for what you need.

Hope this helps!
-Morgan
Utting 311 as2.zip
<https://drive.google.com/file/d/0Bw3YJ7ED-iZ1anJTV2pKTFBib0E/view?usp=drivesdk>
Post by Forrest Cahoon
Hi ChucKers!
I have a sonification project that I'm wondering if it makes sense to do
in ChucK.
I have a csv file with frequencies and amplitudes for 4 different sine
waves, representing values at (say) half-second intervals. I want to read
in a line, and then smoothly change the frequency and amplitude values
using linear interpolation over the course of 24000 samples (a half second
at 48k samples per second), then read in the next line, and smoothly change
to those values over the next half-second, etc. until the data runs out.
I could code this in C using libsndfile, but if I didn't have to worry
about converting frequencies into phase distances between samples I'd
consider that a win.
I noticed Chuck has FileIO methods to read input files (although these are
very poorly documented) so thought it should be possible to read in a csv
file.
I could calculate the amount of frequency and amplitude change per sample
and adjust these in a 1-sample loop, and that's already simpler than coding
in C. But I'm hoping there's some way to tell ChucK "I want this (e.g.
frequency) value to change smoothly from its current value to f1 over the
course of a half-second". If that were possible, ChucK would be a clear
choice for my project.
Thanks for any response!
Forrest Cahoon
_______________________________________________
chuck-users mailing list
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Continue reading on narkive:
Loading...