unsafe public float pitchDetect(double[] pre_ptr, int sample_count) { int SAMPLE_RATE = RATE; int SAMPLE_COUNT = sample_count; int BLOCK_SIZE = 8192; int STEP_SIZE = 512; // Prepare objects pyinc_init(SAMPLE_RATE, BLOCK_SIZE, STEP_SIZE); float *ptr = stackalloc float[SAMPLE_COUNT]; for (int i = 0; i < SAMPLE_COUNT; i++) { ptr[i] = (float)pre_ptr[i]; } // Mine pitches pyinc_pitch_range pitches = pyinc_feed(ptr, SAMPLE_COUNT); float ret_ptr = 0.0F; int num_of_pitches = 0; // Go through and print the pitches float *res_ptr = pitches.begin; //if (*res_ptr != -1) //Console.WriteLine(*res_ptr); while (res_ptr != pitches.end) { if (*res_ptr != -1) { ret_ptr += *res_ptr; num_of_pitches++; } res_ptr++; } ret_ptr = ret_ptr / num_of_pitches; res_ptr = pitches.begin; // Release the data pyinc_clear(); return(ret_ptr); }
// Use this for initialization unsafe static public void use() { int SAMPLE_RATE = 44100; int SAMPLE_COUNT = 10000; int BLOCK_SIZE = 2048; int STEP_SIZE = 512; // Prepare objects pyinc_init(SAMPLE_RATE, BLOCK_SIZE, STEP_SIZE); float *ptr = stackalloc float[SAMPLE_COUNT]; // Generate a 440 herz sine wave double freq = 440; double angle_speed = 2 * Math.PI * (freq / SAMPLE_RATE); for (int i = 0; i < SAMPLE_COUNT; i++) { ptr[i] = (float)Math.Sin(angle_speed * i); } // Mine pitches pyinc_pitch_range pitches = pyinc_feed(ptr, SAMPLE_COUNT); // Go through and print the pitches float *res_ptr = pitches.begin; while (res_ptr != pitches.end) { #if UNITY_EDITOR Debug.Log(*res_ptr); #else Console.Write(*res_ptr); #endif res_ptr++; } // Release the data pyinc_clear(); }