[comp.music] Csound and samples again

jalkio@cc.helsinki.fi (06/24/91)

I have still some trouble on playing samples at the correct pitch with
the Csound.

If I first load a sample to a table (length > the length of the
samplefile) and then play that sample using the "oscil"-generator, what
is the frequency I am supposed to give to that oscillator so that the
sample would sound exactly the same as it was in the file? Does this
depend on the table lenght? (I have found that .18 Hz or so sounds quite
correct but I would like to know what it should be _exactly_ and based
on what.)

		Thanks,   Jouni

ts09+@andrew.cmu.edu (Thomas Sullivan) (06/25/91)

If you look in your Csound manual, you will see the following for
oscils:

kr	oscil(i)	kamp, kcps, ifn[, iphs]
ar	oscil(i)	xamp, xcps, ifn[, iphs]

These are the control rate and audio rate oscillators (the 'i' means
interpolated, if you wish a linear interpolation between samples,
instead of
a sample and hold type of behavoir).

ifn is the function table number.  Csound interprets this as ONE CYCLE
of the oscillator waveform, regardless of its size.  So, if you load
exactly one second of sound into the table (must be same sample rate as
the sample rate given for this orchestra) and give the cps field as 1,
then it will play back exactly.  Now, since people don't tend to use
power of 2 sampling rates, the chances of you loading exactly one second
of sound into a table are slim!  So, you have to fool with the cycles
per second field if you want to do things this way. 

From your post, it looks like you loaded some sound of length X samples
into a table of length Y (with the excess padded with zeros).   If the
sample 
rate of the original sound is R, then if you were to just play the X
samples of the original sound at rate R, it would take X/R seconds.  We
want things such that when one cycle of this oscillator is played
through, you get the original sound back.  this instrument will do this:

;; R is the sample rate of the original sound.  X is the number of samples of
;; the original sound used, Y is the the table length.  X and Y can be made
;; into 'p' parameters if you wish to use different tables with this same 
;; instrument.

	srate	R
	krate	R/N
	ksmps	N
	nchnls	1

	instr	1
;;  p3 is the amplitude, p4 is the warped cps, and p5 is the table number.
;;  so if p4 = 1, this will play the sound so it will come out normal., if you 

;;  make p4 = 2 then the sound will be an octave higher and so on.
a1	oscil	p3,  p4 * (R/Y), p5 
	out	a1
	endin

Basically, all that is going on here is that we are scaling the cps
field by the 
ratio between the real sample rate of the original sound, and the
"internal sample rate" of one cycle of a waveform, namely the table
length.  If you divide your table length into your original sound's
sample rate, do you get something in the neighborhood of the 0.18 that
you reported?

I haven't tested this, so let me know if it works.  It has been a while since 
I've done any serious Csound work.

--Tom
(tms@speech1.cs.cmu.edu)

jalkio@cc.helsinki.fi (06/26/91)

In article <4cNnxvy00WB4AGm0Ug@andrew.cmu.edu>, ts09+@andrew.cmu.edu (Thomas Sullivan) writes:


> a1	oscil	p3,  p4 * (R/Y), p5 
> 	out	a1
> 	endin
> 
> Basically, all that is going on here is that we are scaling the cps
> field by the 
> ratio between the real sample rate of the original sound, and the
> "internal sample rate" of one cycle of a waveform, namely the table
> length.  If you divide your table length into your original sound's
> sample rate, do you get something in the neighborhood of the 0.18 that
> you reported?
> 
> --Tom
> (tms@speech1.cs.cmu.edu)

Yup! This was quite helpful. Dividing the sample rate of my sample with
the table length was the trick. BTW, is there any way to detect from the
orchestra what is the table length of one particular table? If not, I
will have to manually give the numbers in the scorefile.

				Jouni

cook@roger (Doug Cook 756-1460) (06/27/91)

In article <1991Jun23.224435.1@cc.helsinki.fi> jalkio@cc.helsinki.fi writes:
>If I first load a sample to a table (length > the length of the
>samplefile) and then play that sample using the "oscil"-generator, what
>is the frequency I am supposed to give to that oscillator so that the
>sample would sound exactly the same as it was in the file? Does this
>depend on the table lenght? (I have found that .18 Hz or so sounds quite
>correct but I would like to know what it should be _exactly_ and based
>on what.)

You might try the oscil1 (or oscil1i) oscillator. This will loop through your 
sample once over the duration of the note, which is what I assume you want. 
Make the duration parameter the same as the duration of the original sample
if you're resynthesizing at the same sample rate as the sample you're loading
was sampled at. (Does that make sense, or am I being nonsensical again?) 

	-Doug

Doug Cook				|"Much is being said, my lords,
Dept. of Electrical Engineering and CS	| but nothing is being done."
University of California		| 		-Jeff Beck
Davis, CA				|

cook@roger (Doug Cook 756-1460) (06/27/91)

In article <1991Jun26.002918.1@cc.helsinki.fi> jalkio@cc.helsinki.fi writes:
>[...] BTW, is there any way to detect from the
>orchestra what is the table length of one particular table? If not, I
>will have to manually give the numbers in the scorefile.

You can use the ftlen() function to get the length of a table. If the 
actual sample is shorter than the table, and the rest is padded with zeros,
then ftlen() won't be of much help...

	-Doug

Doug Cook				|"Much is being said, my lords,
Dept. of Electrical Engineering and CS	| but nothing is being done."
University of California		| 		-Jeff Beck
Davis, CA				|

ts09+@andrew.cmu.edu (Thomas Sullivan) (06/27/91)

well, actually ftlen() would be of help if you are using oscil as pointed in my
previous post.  You scale the cps field by samplerate/table_length.  You
know srate (you set it yourself!) and then make a call to ftlen(px)
where 'px' is a parameter you pass from the scorefile that is the table
number you require.

so;

a1 	oscil	p3, p4 * srate/ftlen(p5), p5
	out	a1
	endin

p3 = amplitude, p4 = scaled cps (a 1 means play as normal, a 2 one
octave higher, etc), and p5 is the table number.

--tom