Get average Frequency of a sound file

29 views
Skip to first unread message

Lorris TOSCANO

unread,
Oct 25, 2021, 12:06:32 PM10/25/21
to pyo-discuss
Hi I'm new to Pyo and audio programing.
So what I'm trying to do is get the average frequency of sound file, for that I'm using a reading table, the Yin function and then i process all that in a Sine. But then when i try  to get the value of the frequency back i'm stuck because i get an Yin object instead
here is the consol output :

" F min = <_pyo.Yin_base object at 0x7f44c3eb9c80> F max = <_pyo.Yin_base object at 0x7f44bc814f20>"

And here's the code :
----------------------------
from pyo import *
import time

s = Server(audio="jack").boot()

path = "soundexample.wav"

tableinit = SndTable(path)

tableR = TableRead(tableinit).play()

fundamental = Yin(input =tableR , winsize=512)

s.start()

freqSine = Sine(fundamental)

time.sleep(3)

sp = Spectrum(freqSine,size=8192,wintype=4)


print("F min = " + str(freqSine.freq[0]) + " F max = " + str(freqSine.freq[1]))

s.gui(locals())

----------------------------
Thanks in advance for the time you took reading this and the help

Olivier Bélanger

unread,
Oct 25, 2021, 12:58:45 PM10/25/21
to pyo-d...@googlegroups.com
Hi,

There's a couple of assumptions here...

1) Saying freqSine.freq[0] and freqSine.freq[1] assumes that your sound is stereo and the Yin object truy receives 2 audio streams from the table reading.
2) Saying "F min = freqSine.freq[0]" and "F max = freqSine.freq[1]" assumes that the frequency in the left channel is lower than the one in the right channel. 

As for your question, what has been given to the freq argument of the Sine object *is* a Yin object, that's why you are given that back when you inspect the "freq" attribute of the object. If you want the frequency itself as a float, you can retrieve it directly from the Yin object.

fr = fundamental.get(all=True) will return you a list of two floats (for a stereo file), one per channel. These will be the first value in the audio buffer streams at the moment you call it.

If you want the average frequency over a given duration, you'll have to accumulate the frequencies in a list and do the average when done. Something like (doing it mono for simplicity):

freqs = []
def accumulate():
    freqs.append(fundamental.get())

pat = Pattern(function=accumulate, time=0.01).play()

# some time later
average = sum(freqs) / len(freqs)


Olivier


--
You received this message because you are subscribed to the Google Groups "pyo-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyo-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyo-discuss/47efe1fa-2df6-4b77-ad19-20ba9cd943b2n%40googlegroups.com.

Lorris TOSCANO

unread,
Oct 26, 2021, 12:24:27 AM10/26/21
to pyo-discuss

Now i see that there's a get method that come from PyoObject missed it TY
Reply all
Reply to author
Forward
0 new messages