Primary Frequency (Composition by smokris)

Author: smokris
License: MIT
Date: 2010.03.17
Compatibility: 10.5, 10.6
Categories:
Required plugins:
(none)

Given a Frequency Structure (from Kineme AudioTools), finds the primary frequency, using either a simple maximum, or the Harmonic Product Spectrum algorithm.

(The Clip itself should work on 10.5, but the demo requires 10.6.)

PreviewAttachmentSize
Primary Frequency.qtz3.5 KB
Primary Frequency Demo.qtz15.86 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

franz's picture
Re: Primary Frequency (Composition by smokris)

this is brilliant ! i wonder what are the math involved ... ? Is there a way to explode your /primary frequency macro, to see what's inside ?

Congrats, this comp is really helpful.

smokris's picture
Re: Primary Frequency (Composition by smokris)

The first composition ("Primary Frequency.qtz") is the macro itself. In 10.6, you can either install it, then click Edit from the context menu in the Patch Creator, or just open the composition and peruse the contents.

AFAIK there's no way to extract a virtual macro from a composition that embeds it, without splicing plists.

gtoledo3's picture
Re: Primary Frequency (Composition by smokris)

CompositionTool.app can "flatten" or basically extract the macro, can't it? (It's been a long day, maybe I'm misunderstanding).

usefuldesign.au's picture
Re: Primary Frequency (Composition by smokris)

Is this SL only? I can open the Primary_Frequency patch and i have placed it in the Patches Folder. When I open Primary Frequency_Demo.qtz I get errors:

> (null)
<QCNodeManager | namespace = "com.apple.QuartzComposer" | 549 nodes>: There is already a node registered with name "/json_to_structure"
 
> (null)
<QCNodeManager | namespace = "com.apple.QuartzComposer" | 615 nodes>: Patch with name "/primary frequency" is missing
 
> Macro Patch
Cannot create node of class "/primary frequency" and identifier "(null)"
 
> Macro Patch
Cannot create connection from ["Frequency" @ "Patch_3"] to ["inputNumber" @ "NumberFormatter_1"]
 
> Macro Patch
Cannot create connection from ["outputFrequency" @ "AudioFileInputPatch_1"] to ["Frequency_Structure" @ "Patch_3"]
 
> Macro Patch
Cannot create connection from ["output" @ "Splitter_2"] to ["Sample_Rate" @ "Patch_3"]
 
> Macro Patch
Cannot create connection from ["Frequency" @ "Patch_3"] to ["a" @ "Expression_1"]
 
> (null)
State restoration failed on <QCPatch = 0x085571C0 "(null)">

Okay now I've renamed Primary Structure.qtz to take out the underscore and I only get these errors:

> (null)
<QCNodeManager | namespace = "com.apple.QuartzComposer" | 550 nodes>: There is already a node registered with name "/json_to_structure"
 
> Macro Patch
Cannot create connection from ["outputFrequency" @ "AudioFileInputPatch_1"] to ["Frequency_Structure" @ "Patch_3"]
 
> (null)
State restoration failed on <QCPatch = 0x06B18040 "(null)">

Then I removed the JSON_To_Structure.qtz patch (how this could help I don't understand :/) and I am down to two messages:

> Macro Patch
Cannot create connection from ["outputFrequency" @ "AudioFileInputPatch_1"] to ["Frequency_Structure" @ "Patch_3"]
 
> (null)
State restoration failed on <QCPatch = 0x09332FB0 "(null)">

This looks like an interesting comp any hints as to what's wrong for me.

OS 10.5.8 G5 NV GeForce FX 5200

dust's picture
Re: Primary Frequency (Composition by smokris)

sick macro smorkis. i like macros. you can right click the primary frequency macro and edit it to see its java script source.

check out my note 2 midi note number 2 frequency macro. the demo file shows how you would use the harmonic primary frequency to detect pitch in lets say an audio file.

smokris's picture
Re: Primary Frequency (Composition by smokris)

(Note that the original post says "(The Clip itself should work on 10.5, but the demo requires 10.6.)")

I've attached a flattened version of the demo, which might work on 10.5.

PreviewAttachmentSize
Primary_Frequency_Demo_flattened.qtz23.04 KB

usefuldesign.au's picture
Re: Primary Frequency (Composition by smokris)

Oh yeah right :] (oops)

no joy with flattened one either but thanks for trying :)

franz's picture
Re: Primary Frequency / live input ?

Hi, i tryied to plug your freq analysis - pitch tracker to a live input, however, it doesn't seem to be working. i got crazy values, and bulk stuff. Did you test it with live input, to track voice tone already ?

dust's picture
Re: Primary Frequency / live input ?

Yes I have tested this with live input. If you check my midi note number and frequency converter macro's demo source you will find a calibrated primary harmonic frequency pitch detection patch. basically i did the calibration off 12 piano notes saved as audio files.

the pitch detection program runs the audio file in the background for a given pitch and checks to see if a input file is in the same pitch. when i say calibrated i mean this macro is awesome but when playing a specific pitch lets note a4 the harmonic primary frequency in 440hz and this macro reports 431 which is very close but not perfect. im not complaining because this is brilliant smorkis is accessing the bin directly from javascript patch. i have been doing this by dumping a 255 band frequency down to individual structures to compare index by index of a known pitch which worked but was not elegant like this solution. so even though lets say a 440hz is off by 9 hz it is consistently off which is a good thing as its correctable without even touching the source.

i can't remember what i did now will have to look at the file but i think i maxed the samples and did a logarithmic average frequency mode as that is how temporal frequencies are derived logarithmically.

i don't want to get into semantics but i think 432 is a better tuning. hers why.

from james furia. james is a bit out there but carl munks code is actually brilliant.

"The work of these men, Kepler, Pythagoras and Hawkins is pure genius. However their ratios for the intervals in the diatonic scale are non-symmetrical and slightly simplified. They have used the ratio 27/24 for the whole step which for one is incorrect. Following this logic, in the 880 octave and in others we have a full 1.76868 left over leaving the octave highly off its mark. These mathematical giants 4/3 ratio for the perfect 4th is also a full 1.009 hz off the mark as well. They have taken the symmetrical chromatic scale out of phase as to simplify to the ideal of whole numbers. This is still ok as I will also take our symmetrical chromatic scale out of phase to a degree of only .0001 to .01hz . My main contention is not this but rather the fact that they are using only 7 notes, when any musician knows there are 12 notes in each octave. (in western music theory, some other cultures have more) What this author has done is to include these neglected sharps and flats which I will prove are astoundingly important to the conversion of geometry into music. "

so back to harmonic frequency.

here are some screen shots. ironically with live input the built in mic on my mbp which blows ass is right next to some kind of fan and takes horrible recordings is actually better with my tests. don't know what apple was thinking when they put the mic next to the fan ? the mac book doesn't have this problem. its funny the guy that made quicktime quit apple for these kind of reasons. anyways i guess they think if you can afford a 3k pro laptop you can buy a decent mic.

well i have a bunch of mics the second screen shot is a blue mic. the worlds first all digital mic thats why i got it. anyways i get better results with built-in mic. i will try with a few others later as i have a bunch of mics.

you will not get these kind of results if you do not know how to sing. or well you can do like a long ohmmm constant tone. etc.. your results will be all over the place if your just talking into the mic.

what i do to test is use auto tune. actually to make these i had a sure mic attached to my ipod and t-pain running off my ipod out a tiny altec portable speaker. sitting in my lap. now with auto tune one if t-pain and all those rappers sound like shit cause one they can't sing and there "chering" it out with computer trying to correct off voice and thats what gives computer sounding auto tune that is big with rappers. well if you know how to sing and you use auto tune in lets say your natural key there isn't any of that computer correction rapper sounding stuff. anyways you see my voices natural key in these pics which is a bit off as i'm showing how to do this from the smorkis demo.

so later i will use a much better mic and run auto tune off my computer via soundflower to get a cleaner signal into the kineme audio input patch but just to show you how can get consistent linear results with live input. so i use auto tune to test keys and i think its a wonderful tool to teach your self to sing. if you get that computer corrected glitch out of auto tune then your way off.

but now that smorkis made this awesome macro that people have been asking if possible in qc to pitch detect for as long as i have been using is now a possibility for everyone. so this means you can make a guitar tuner in QC. if you check the demo i mentioned above you can also add a file player to play the base notes the calibration was made off so if you where tunning a guitar you could here the piano note at the same time watch the harmonic frequency to tell if you need to pitch up or down if your so inclined to use a440 tuning. i say use your key frequency what sounds right to you and everyone else tune off that or use 432 but blah blah.

thanks smorkis this is pretty sick. i feel really dumb at how i was approaching this problem. hey im new to qc it ok to be daft.

kineme rules

; ) have tuning.

PreviewAttachmentSize
Primary_Frequency_AIO_dust.qtz38.09 KB
builtIN.png
builtIN.png1.02 MB
blueBall.png
blueBall.png1.04 MB

smokris's picture
Re: Primary Frequency / live input ?

dust wrote:
when i say calibrated i mean this macro is awesome but when playing a specific pitch lets note a4 the harmonic primary frequency in 440hz and this macro reports 431 which is very close but not perfect.

Fourier analysis suffers from the Heisenberg Uncertainty Principle --- as you increase frequency resolution, you lose time resolution, and vice versa. It's therefore not great for pitch detection; you're probably better off using an autocorrelation algorithm if you want frequency-accuracy.

If you increase the "Sample Buffer" size on the Audio File Input patch, the FFT window increases accordingly, and you'll get more accurate pitches (at the expense of computational time and accuracy of the timing of the pitches).

smokris's picture
Re: Primary Frequency / live input ?

franz wrote:
Hi, i tryied to plug your freq analysis - pitch tracker to a live input, however, it doesn't seem to be working. i got crazy values, and bulk stuff. Did you test it with live input, to track voice tone already ?

For me it basically works, although since the Audio Input patch's buffer size is fixed at 512 samples, it doesn't provide great resolution.

dust's picture
Re: Primary Frequency / live input ?

im saying 432 with circle of fiths makes some very cool geometric shapes and colors like the note b is infrared. so if the computer thinks 431 thats closer. actually offsetting the tuning frequency is a really great to do binaural synthesis. by offsetting or phasing mono sine frequencies in each ear in something like the 8hz disparity range will do a surround sound wobble in your head and put your brain in theta state. which we need more off.

opps that was and off rant.

on topic maybe the internal built in mic combined with the computers residual frequency have some normalizing noise compression effect on the oscilloscope read out because .

i tried with some really hi res bits like 96bit recordings 24bit recordings i tried different sample rates but it seems for the results i was looking for or the best file format i found is 44100 320kps mp3 compression

or 80kbs mp3 its the compression of the audio file gives better results for me but the built in mic is the best flat line oscilloscope read from the primary harmonic frequency. as i did that with screen shot with my voice but now that im thinking a sine loop audio file or bin structure save is a better way than a piano sample as as per the sustain issues as where as a constant sample with no fall off decay would give you better flat lines which are better to use for variable pitch detection

marcofilipevieira's picture
Re: Primary Frequency (Composition by smokris)

Hi, can you give me a help please? I'm new in this, and I have a project which I have to detect pitches from a live performance of a mandolin. And I have to use Quartz Composer. So this Primary Frequency Demo can help a lot with this. But I can't open it. The Primary Frequency file I can open with no problems, but when I try to open the Primary Frequency Demo.qtz appears this error message:

(null) : Patch with name "StructureMaker:KinemeNamedStructureMaker" is missing

Macro Patch Cannot create node of class "StructureMaker" and identifier "KinemeNamedStructureMaker"

Macro Patch Cannot create connection from ["output" @ "Splitter_6"] to ["Input 3" @ "StructureMaker_KinemeNamedStructureMaker_1"]

Macro Patch Cannot create connection from ["outputStructure" @ "StructureMaker_KinemeNamedStructureMaker_1"] to ["inputValue" @ "Queue_1"]

Macro Patch Cannot create connection from ["outputValue" @ "Math_1"] to ["Input 1" @ "StructureMaker_KinemeNamedStructureMaker_1"]

Macro Patch Cannot create connection from ["output" @ "Splitter_3"] to ["Input 2" @ "StructureMaker_KinemeNamedStructureMaker_1"]

(null) State restoration failed on

(null) : Patch with name "AudioInputPatch" is missing

Macro Patch Cannot create node of class "AudioInputPatch" and identifier "(null)"

(null) : Patch with name "AudioDeviceInfoPatch" is missing

Macro Patch Cannot create node of class "AudioDeviceInfoPatch" and identifier "(null)"

Macro Patch Cannot create connection from ["outputInputDevices" @ "AudioDeviceInfoPatch_3"] to ["inputStructure" @ "StructureMember_index_1"]

Macro Patch Cannot create connection from ["outputInputDevices" @ "AudioDeviceInfoPatch_3"] to ["inputStructure" @ "StructureCount_1"]

(null) State restoration failed on

Macro Patch Cannot create connection from ["UID" @ "Patch_1"] to ["inputDeviceUID" @ "AudioInputPatch_1"]

Macro Patch Cannot create connection from ["outputFrequency" @ "AudioInputPatch_1"] to ["Frequency_Structure" @ "Patch_4"]

(null) State restoration failed on

What am I doing wrong?

Cheers

marcofilipevieira's picture
Re: Primary Frequency (Composition by smokris)

Now I can open it without errors messages (I don't know why)! But I don't understand how it works. In the viewer window is always 0 Hz. Can please someone tell me, what am I missing? Thank you very much for your time.

Cheers,

marcofilipevieira's picture
Re: Primary Frequency (Composition by smokris)

Instead of using the Maximum value, if you do an interpolation to find the primary frequency, wouldn't it be more accurate?