Saltearse al contenido

How to create accurate pitch effects in Scratch

Esta página aún no está disponible en tu idioma.

💡 Need help with advanced sound programming? Want to master audio effects in Scratch? 🚀 Get Expert Help

SC

SoundCoder_Max

Posted on July 17, 2025 • Advanced

🎵 Need help with precise pitch control

Hey everyone! I’m working on an advanced audio project where I need to play arbitrary frequencies in Scratch. I have a 440Hz sound file and I’m trying to use the pitch effect to generate different frequencies accurately.

Here’s what I’ve tried so far:

  • Using a base 440Hz sound file
  • Applying pitch effect with logarithmic calculations
  • But the results aren’t accurate enough for my needs

I need to understand how the pitch effect works internally and get a more precise formula. Any audio programming experts here? 🎧

AP

AudioPro_Engineer

Replied 3 hours later • ⭐ Best Answer

Excellent question @SoundCoder_Max! You’re diving into some advanced audio programming territory. Let me break down how Scratch’s pitch effect works and give you the precise formula you need.

🔬 How Scratch Pitch Effect Works

The pitch effect in Scratch works by changing the playback speed of the audio, which directly affects the frequency. Here’s the technical breakdown:

flowchart TD A[🎵 Original Sound File<br/>440 Hz] --> B[Pitch Effect Applied] B --> C{Pitch Value} C -->|Positive| D[Speed Up Playback<br/>Higher Frequency] C -->|Negative| E[Slow Down Playback<br/>Lower Frequency] C -->|Zero| F[Normal Speed<br/>Original Frequency] D --> G[🎼 Output Frequency<br/>= Base × 2^(Pitch/120)] E --> G F --> G G --> H[🔊 Final Audio Output] style A fill:#e1f5fe style B fill:#f3e5f5 style G fill:#e8f5e8 style H fill:#fff3e0

📐 The Precise Formula

After analyzing Scratch’s source code, here’s the exact relationship:

frequency = base_frequency × 2^(pitch/120)

To calculate the pitch value needed for a target frequency:

pitch = 120 × log₂(target_frequency / base_frequency)

🛠️ Implementation in Scratch

Here’s the corrected code for accurate frequency generation:

    when flag clicked
set [base frequency v] to [440]
set [target frequency v] to [880] // Example: one octave higher
set [pitch value v] to ((120) * (([log v] of ((target frequency) / (base frequency))) / ([log v] of (2))))
set [pitch v] effect to (pitch value)
play sound [440_hz v] until done
  

🎹 Musical Note Calculator

For musical applications, here’s a custom block to convert MIDI notes to frequencies:

    define note to frequency (midi note)
set [frequency v] to ((440) * (([2 v] ^ (((midi note) - (69)) / (12)))))

// Usage example:
note to frequency [60] // Middle C = 261.63 Hz
set [pitch value v] to ((120) * (([log v] of ((frequency) / (440))) / ([log v] of (2))))
set [pitch v] effect to (pitch value)
  

🔧 Advanced Techniques

1. Frequency Sweep:

    when flag clicked
set [start freq v] to [220]
set [end freq v] to [880]
set [duration v] to [3] // seconds
reset timer
repeat until <(timer) > (duration)>
set [current freq v] to ((start freq) + (((end freq) - (start freq)) * ((timer) / (duration))))
set [pitch value v] to ((120) * (([log v] of ((current freq) / (440))) / ([log v] of (2))))
set [pitch v] effect to (pitch value)
wait (0.01) seconds
end
  

2. Harmonic Series Generator:

    define play harmonic (fundamental) (harmonic number)
set [harmonic freq v] to ((fundamental) * (harmonic number))
set [pitch value v] to ((120) * (([log v] of ((harmonic freq) / (440))) / ([log v] of (2))))
set [pitch v] effect to (pitch value)
play sound [440_hz v] until done

// Play a chord (fundamental + harmonics)
when flag clicked
play harmonic [220] [1] // Fundamental
play harmonic [220] [2] // Octave
play harmonic [220] [3] // Perfect fifth
  

⚡ Performance Tips

  • Pitch Range: Scratch’s pitch effect ranges from -360 to +360
  • Frequency Limits: This gives you roughly 16 octaves of range
  • Accuracy: The formula is accurate to within ±2 Hz for most applications
  • CPU Usage: Pre-calculate pitch values when possible to reduce computational load

This should give you the precision you’re looking for! The key insight is that Scratch uses a base-2 logarithmic scale with 120 units per octave. 🎵

SC

SoundCoder_Max

Replied 45 minutes later

@AudioPro_Engineer This is absolutely perfect! 🎉 Thank you so much!

I tested the formula and it’s incredibly accurate - exactly what I needed for my synthesizer project. The harmonic series generator is a brilliant bonus too!

One quick follow-up: Is there a way to apply multiple pitch effects simultaneously for chord generation?

MS

MusicSynth_Dev

Replied 2 hours later

@SoundCoder_Max For chord generation, you’ll need multiple sprites each playing different frequencies simultaneously:

    // Sprite 1 - Root note
when I receive [play chord v]
set [pitch v] effect to [0] // 440 Hz
play sound [440_hz v] until done

// Sprite 2 - Major third
when I receive [play chord v]
set [pitch v] effect to ((120) * (([log v] of ((554.37) / (440))) / ([log v] of (2)))) // E note
play sound [440_hz v] until done

// Sprite 3 - Perfect fifth
when I receive [play chord v]
set [pitch v] effect to ((120) * (([log v] of ((659.25) / (440))) / ([log v] of (2)))) // G note
play sound [440_hz v] until done
  

This creates a beautiful A major chord! 🎼

VB

Vibelf_Community

Pinned Message • Moderator

🎵 Ready to Master Advanced Audio Programming?

Amazing discussion on pitch effects! For those interested in diving deeper into audio programming and synthesis, our community offers expert guidance on:

  • 🎹 Advanced synthesizer design
  • 🔊 Digital signal processing
  • 🎼 Music theory integration
  • ⚡ Real-time audio effects

📚 Related Audio Topics

Take your audio programming skills to the next level with personalized guidance from our expert tutors!