public static void AdjustPitch(AudioClip clip, SoundPlayer player) { int key = KeyFinder.KeyFind(clip); Logger.log.Error("the key of song is " + key); int sampleKey = 21; //GMinor int minMajDiff = (sampleKey % 2 == 0) ? -3 : 3; minMajDiff = ((key % 2) == (sampleKey % 2)) ? 0 : minMajDiff; int diff = ((int)(key - sampleKey) / 2 + minMajDiff + 24) % 12; float pitch = 1f; if (diff <= 7) { for (int i = 0; i < diff; i++) { pitch *= KeyFinder.SEMITONE; } } else { for (int i = 0; i < 12 - diff; i++) { pitch /= KeyFinder.SEMITONE; } } player.SetPitch(pitch); Logger.log.Error("the key diff is " + diff); }
public static float AdjustPitch(AudioClip song, int sampleKey) { int key = KeyFinder.KeyFind(song); Log.Write("the key of song is " + key); //int sampleKey = 21; //GMinor int minMajDiff = (sampleKey % 2 == 0) ? -3 : 3; minMajDiff = ((key % 2) == (sampleKey % 2)) ? 0 : minMajDiff; Log.Write("minMajDiff is " + minMajDiff); int diff = ((int)(key - sampleKey) / 2 + minMajDiff + 24) % 12; float pitch = 1f; if (diff <= 7) { for (int i = 0; i < diff; i++) { pitch *= SEMITONE; } } else { for (int i = 0; i < 12 - diff; i++) { pitch /= SEMITONE; } } return(pitch); }