electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
SndBuf: syntax for reading the number of samples
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [13 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Sun Nov 12, 2006 10:18 pm    Post subject: SndBuf: syntax for reading the number of samples
Subject description: part 1 in a newb's series of SndBuf questions
Reply with quote  Mark this post and the followings unread

hi,
i'm new to chuck and find it very interesting. the fact that it works in real-time makes it very inspiring to experiment with code(i've been trying to learn c++ for 7 years and am still a newb for this reason)

anyway, to my question:
i dont know the syntax to get the samples from a sound in SndBuf.
let's say, for example, that i wanted to use C:\samples\drumloop

how would i type that correctly using the buf.samples command to find the number of samples?

-edit-
and are those number of samples in ms?

i've been trying a few things and i got this i think it works

Code:

// the patch
SndBuf buf => dac;
// load the file
"E:\samples\AmenMRZ2-180bpm.wav" => buf.read;
buf.samples() => int mysamps;
<<<mysamps>>>;
// time loop
while( true )
{
    0 => buf.pos;
   
    1325::ms => now;
}


wich gives me this output
Code:

[chuck](VM): replacing shred 3 (unnamed1) with 3 (unnamed1)...
58800 :(int)

now the value 58800 seems to be the samples but this is a 1bar loop at 180bpm, and although i havent taken the time to figure out how to calculate the exact time of a 4 beat bar at 180Bpm, i know that 1325 is VERY close. so why does 58800 get ouptut?
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Nov 13, 2006 4:54 am    Post subject: Re: SndBuf: syntax for reading the number of samples
Subject description: part 1 in a newb's series of SndBuf questions
Reply with quote  Mark this post and the followings unread

laserbeak wrote:
how would i type that correctly using the buf.samples command to find the number of samples?


Hi, Laserbeak!
(wasn't that one of the Decepticons that changed into a casette tape?)

You get the number of samples exactly like in your example;

buf.samples() => int NumberOfSamples;

So; to play a file in reverse;

//create a sndbuf
SndBuf buf => dac;
//load some sample
"boom.wav" => buf.read;
//get the number of samples and use it to set the buffer's position to the end
buf.samples() => buf.pos
//reverse the playback direction
-1 => buf.rate;

Quote:
and are those number of samples in ms?


No, those are in "samples" so the number you get is the exact number of values that defines your wave file.

This means that in a cd quality audio file a sample that lasts one second will have 44100 samples.

It also means that if you would have some realy lo-fi wave sampled at 1000Hz the number of samples would also be the number of ms.

180BPM means 1.5 beats every second so (asuming cd quality audio) that means a "beat" lasts (44100 / 1.5) = 29400 samples.

29400 * 2 = 58800 samples so you count your beats twice as fast as the bpm would indicate, it also means you have a wave file that's cut very precisely! (often they have a few samples of rounding error.)

Does that help?

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Mon Nov 13, 2006 7:16 am    Post subject: Re: SndBuf: syntax for reading the number of samples
Subject description: part 1 in a newb's series of SndBuf questions
Reply with quote  Mark this post and the followings unread

Kassen wrote:

Hi, Laserbeak!
(wasn't that one of the Decepticons that changed into a casette tape?)


Yes Smile

Kassen wrote:

Quote:
and are those number of samples in ms?


No, those are in "samples" so the number you get is the exact number of values that defines your wave file.


Oh yeah!! Forgot about that! Smile

Kassen wrote:

180BPM means 1.5 beats every second so (asuming cd quality audio) that means a "beat" lasts (44100 / 1.5) = 29400 samples.

29400 * 2 = 58800 samples so you count your beats twice as fast as the bpm would indicate


So does that mean that 58800 samples is 3000ms? I think that i would probably be easier to create a function or class that would convert samples to milliseconds for me to read from although math is math in any form.

Kassen wrote:

it also means you have a wave file that's cut very precisely! (often they have a few samples of rounding error.)


Hehe, yes i use FLStudio to cut my loops. Havent figured out what im gonna use in Linux yet though. Maybe EnergyXT2 when that gets released.
Back to top
View user's profile Send private message
kijjaz



Joined: Sep 20, 2004
Posts: 765
Location: bangkok, thailand
Audio files: 4

PostPosted: Mon Nov 13, 2006 12:09 pm    Post subject: Reply with quote  Mark this post and the followings unread

Laserbeak: It's nice seeing you here in chuck forum, my csound brother.. haha
ok anyway, let's play more with what you're playing.

now let's talk more about 'BPM'
we'll need a few mathematics.

ok.. let us have float bpm as the tempo of the music.
it's bpm beats per minute
= bpm beats per 60 seconds
= (bpm / 60) beats per second
so for one second, we've got (bpm/60) beats passed.

but what we're gonna use is the 'second passed.
so we need to calculate how long each beat takes.
1 second = bpm/60 beats
60 second = bpm beats
(60 / bpm) second = 1 beat

so actually, 1 beat equals 60/bpm
- - -

my idea is this:
// first, initiate BPM
180.0 => float BPM;
// calculate beat
60.0 / BPM :: second => dur beat;

// you can even calculate bar by define how many beats per bar
beat * 4 => dur bar;
- - -

with this, you can use beat or bar as the duration depending on the init value of BPM.
- - -

laserbeak: So does that mean that 58800 samples is 3000ms? I think that i would probably be easier to create a function or class that would convert samples to milliseconds for me to read from although math is math in any form.

kijjaz: you might have to check out Kassen's idea.
first, you'll need to find out the samplerate of the wavefile you're playing.

let's say we have SndBuf buf;
the sample rate of buf would be 'how many samples per second'
so that would be buf.samples() / buf.length() - - - (1)

then check out the current real-time sample rate
it's second / samp - - - (2)

to be able to play the wave with original speed,
we'll need to find the ratio between the sample's rate and the virtual machine's rate..
the ratio would be (1) / (2)

ok.. let's set the buf's read rate.
(buf.samples() / buf.length()) / (second / samp) => buf.rate;

and now you can play the file at the original rate without having to care about virtual machine's different sample rate.

!! ... this is my idea, still haven't been tested well
is my calculations correct?
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
kijjaz



Joined: Sep 20, 2004
Posts: 765
Location: bangkok, thailand
Audio files: 4

PostPosted: Mon Nov 13, 2006 12:35 pm    Post subject: Reply with quote  Mark this post and the followings unread

i was wrong!

in chuck manual, SndBuf's length() fetches length in seconds
but actually ..

SndBuf buf => dac;
buf.read("special:glot_ahh");
<<< buf.length() >>>;


prints out: 992.000000 :(dur)

so this is one thing that needs to be changed in the manual.
now i can check out sample rate by..
<<< buf.samples() / (buf.length() / second) >>>;

prints out: 44100.000000 :(float) (works okay..)
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Mon Nov 13, 2006 12:51 pm    Post subject: Reply with quote  Mark this post and the followings unread

where'd you find the .length() function????
Back to top
View user's profile Send private message
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Mon Nov 13, 2006 1:06 pm    Post subject: Reply with quote  Mark this post and the followings unread

so buf.length() / ms => float length; wil return the value in milliseconds. cool!!

thanks guys!!
Back to top
View user's profile Send private message
spencer



Joined: Aug 16, 2006
Posts: 53
Location: northern california

PostPosted: Tue Nov 14, 2006 12:31 am    Post subject: Reply with quote  Mark this post and the followings unread

laserbeak wrote:
where'd you find the .length() function????

check out this:
http://chuck.cs.princeton.edu/doc/program/ugen.html
and/or this:
http://chuck.cs.princeton.edu/release/files/chuck_manual.pdf (ChucK manual pdf)

kijjaz wrote:
so this is one thing that needs to be changed in the manual.

Cool, thanks! fixed in CVS...

spencer
Back to top
View user's profile Send private message
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Tue Nov 14, 2006 3:45 pm    Post subject: Reply with quote  Mark this post and the followings unread

spencer wrote:
laserbeak wrote:
where'd you find the .length() function????

check out this:
http://chuck.cs.princeton.edu/doc/program/ugen.html
and/or this:
http://chuck.cs.princeton.edu/release/files/chuck_manual.pdf (ChucK manual pdf)

kijjaz wrote:
so this is one thing that needs to be changed in the manual.

Cool, thanks! fixed in CVS...

spencer


ok i found it on the webpage already, thanks. didnt think it was in the manual. but i'll look again
Back to top
View user's profile Send private message
majutsu



Joined: Jun 18, 2006
Posts: 151
Location: New York
Audio files: 1

PostPosted: Tue Nov 14, 2006 8:36 pm    Post subject: Reply with quote  Mark this post and the followings unread

actually, even more interestingly, length() seems to return something very close to the number of samples despite the manual saying otherwise. but samples() returns a number of samples DIFFERENT than length(), in my experience a few thousand different. And neither matches the samples length as they were generated in me by soundforge or whatever. this makes beat slicing in chuck problem-ridden at this point. I either use single shot samples or slice the sample on the beat in chuck and play the slices forward or backward at different speeds. More beat-slicing accuracy than this in chuck is not had at this time (1.2.7b)
_________________
All phenomena are atoms in association and dissociation.
Back to top
View user's profile Send private message
laserbeak



Joined: Nov 12, 2006
Posts: 15
Location: East Coast U.S.Ass

PostPosted: Tue Nov 14, 2006 9:08 pm    Post subject: Reply with quote  Mark this post and the followings unread

thats disapointing and you might be right if a bar at 180bpm is 1.5ms and not 1.3

i guess i could try slicing my loops and sequencing them that way
Back to top
View user's profile Send private message
kijjaz



Joined: Sep 20, 2004
Posts: 765
Location: bangkok, thailand
Audio files: 4

PostPosted: Wed Nov 15, 2006 7:33 am    Post subject: Reply with quote  Mark this post and the followings unread

hmm.. is that so?
i didn't test .length() in many cases..
but i guess since there might be a problem with length,
we should stick to .samp() for now..

eh .. and is there any way to retrieve SampleRate without using .length() ?
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Tue Nov 21, 2006 11:42 am    Post subject: Reply with quote  Mark this post and the followings unread

majutsu wrote:
And neither matches the samples length as they were generated in me by soundforge or whatever.


Hmmmm, that sounds like a issue or bug somewhere?

What you can always do is take the number of samples as given by chuck and take the number of samples in the time period you want the loop to span. You then devide those and send the result to SndBuf.rate to sync it turntable style. Quick&dirty and it might give tuning issues for melodic material but it's cheaper then a copy of Ableton Live too Wink.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [13 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use