On Monday 27 February 2012 at 18:09, joachim heintz wrote:
hi ed -i would like to test your code, but get a lot of errors - probably dueto copying from the email. can you attach the .csd?thanks -joachimAm 27.02.2012 16:04, schrieb Edward Costello:Hey List,I made a UDO over the weekend implementing Yin pitch detectionalgorithm, with a good bit of help from looking at the Aubio library'sversion, it seems to work pretty well and is comparable in accuracy withthe pvspitch and ptrack opcodes, it doesn't use too much cpu either.Anyways heres the code with an example, the threshold value for theopcode is a number between 0 and 1,<CsoundSynthesizer><CsOptions></CsOptions><CsInstruments>sr = 44100ksmps = 1024nchnls = 20dbfs = 1opcode QuadraticInterpolation, k, iki_YinBuffer, k_Period xink_x0 = k_Period < 1 ? k_Period : k_Period - 1k_x2 = k_Period + 1 < ftlen(i_YinBuffer) ? k_Period + 1 : k_Periodif k_x0 == k_Period thenk_bufferAtPeriod tab k_Period, i_YinBufferk_bufferAtx2 tab k_x2, i_YinBufferk_Pitch = k_bufferAtPeriod <= k_bufferAtx2 ? k_Period : k_x2elseif k_x2 == k_Period thenk_bufferAtPeriod tab k_Period, i_YinBufferk_bufferAtx0 tab k_x0, i_YinBufferk_Pitch = k_bufferAtPeriod <= k_bufferAtx0 ? k_Period : k_x0elsek_s0 tab k_x0, i_YinBufferk_s1 tab k_Period, i_YinBufferk_s2 tab k_x2, i_YinBufferk_Pitch = k_Period + 0.5 * (k_s2 - k_s0) / (k_s2 - 2.0 * k_s1 + k_s0)endifxout k_Pitchendopopcode Yin, k, aiia_In, i_YinBuffer, i_Threshold xink_tmp2 = 0k_Tau = 1k_PitchFound = 0until k_Tau >= ftlen(i_YinBuffer) || k_PitchFound == 1 dok_Index = 0until k_Index >= ftlen(i_YinBuffer) dok_1 vaget k_Index, a_Ink_2 vaget k_Index + k_Tau, a_Ink_tmp = k_1 - k_2k_3 tab k_Tau, i_YinBuffertabw k_3 + (k_tmp * k_tmp), k_Tau, i_YinBufferk_Index = k_Index + 1odk_1 tab k_Tau, i_YinBufferk_tmp2 = k_tmp2 + k_1k_2 = k_1 * (k_Tau / k_tmp2)tabw k_2, k_Tau, i_YinBufferk_Period = k_Tau - 3if k_Tau > 4 thenk_BufferAtPeriod tab k_Period, i_YinBufferk_BufferAtPeriodPlusOne tab k_Period + 1, i_YinBufferif k_BufferAtPeriod < i_Threshold && k_BufferAtPeriod <k_BufferAtPeriodPlusOne thenk_Pitch QuadraticInterpolation i_YinBuffer, k_Periodk_Pitch = sr / k_Pitchk_PitchFound = 1elsek_PitchFound = 0endifendifk_Tau = k_Tau + 1odif k_PitchFound == 0 thenk_Index = 0k_Period = 0k_tmp tab 0, i_YinBufferuntil k_Index >= ftlen(i_YinBuffer) dok_0 tab k_Index, i_YinBufferk_Period = k_tmp < k_0 ? k_Period : k_Indexk_tmp = k_tmp < k_0 ? k_tmp : k_0k_Index = k_Index + 1odk_Pitch QuadraticInterpolation i_YinBuffer, k_Periodendifxout k_Pitchendopinstr Init, 1i_WindowSize = ksmpsi_Threshold = .04gi_AudioBuffer ftgen 0, 0, i_WindowSize, 16, 0, 0gi_YinBuffer ftgen 0, 0, i_WindowSize / 2, 16, 0, 0Sfile = "Sound.wav"i_Length filelen Sfilegi_AudioIn ftgen 0, 0, -(sr * i_Length), 1, Sfile, 0, 0, 0a_Line line 0, i_Length, ftlen(gi_AudioIn)a_Out table a_Line, gi_AudioInk_Pitch Yin a_Out, gi_YinBuffer, i_Thresholdk_Pitch tonek k_Pitch, 10a_Tone vco2 .2, k_Pitch , 12outs a_Out, a_Toneendin</CsInstruments><CsScore>i "Init" 0 10</CsScore></CsoundSynthesizer>Cheers,EdSend bugs reports to the Sourceforge bug trackerDiscussions of bugs and features can be posted hereTo unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"