Aller au contenu

How to detect multiple frequencies in Scratch audio projects

Ce contenu n’est pas encore disponible dans votre langue.

💡 Struggling with audio programming and frequency detection in Scratch? 🚀 Get Expert Help

AS

AudioCoder_Sam

Posted on January 22, 2024 • Advanced

🎵 Need help with frequency detection in Scratch

Hi everyone! I’m working on an audio-reactive project where I need to detect different frequency ranges (low, mid, high) from microphone input or music playback. I want to create:

  • Visual effects that respond to bass frequencies
  • Different animations for mid-range frequencies
  • Special effects for high frequencies
  • Real-time frequency analysis display

I know about the loudness sensor, but I’m not sure how to separate different frequency ranges. Any advanced techniques would be greatly appreciated! 🎧

AP

AudioPro_Expert

Replied 3 hours later • ⭐ Best Answer

Excellent question @AudioCoder_Sam! Frequency detection in Scratch is challenging but definitely possible. Here’s a comprehensive approach:

🎛️ Audio Processing Flow

Here’s how frequency detection works in Scratch:

flowchart TD A[🎤 Audio Input] --> B[Loudness Sensor] B --> C[Sample Audio Data] C --> D[Low Frequency Analysis] C --> E[Mid Frequency Analysis] C --> F[High Frequency Analysis] D --> G[🔊 Bass Detection] E --> H[🎵 Mid-Range Detection] F --> I[🎶 Treble Detection] G --> J[Visual Bass Effects] H --> K[Mid-Range Animations] I --> L[High-Frequency Effects] J --> M[🎨 Combined Audio Visualization] K --> M L --> M style A fill:#e1f5fe style B fill:#f3e5f5 style G fill:#e8f5e8 style H fill:#fff3e0 style I fill:#fce4ec style M fill:#f0f4c3

🔧 Method 1: Threshold-Based Detection

The simplest approach uses loudness thresholds to approximate frequency ranges:

    when flag clicked
forever
set [current loudness v] to (loudness)

// Low frequency detection (bass)
if <(current loudness) > [20]> then
if <(current loudness) < [40]> then
broadcast [bass detected v]
set [bass level v] to (current loudness)
end
end

// Mid frequency detection
if <(current loudness) > [40]> then
if <(current loudness) < [70]> then
broadcast [mid detected v]
set [mid level v] to (current loudness)
end
end

// High frequency detection (treble)
if <(current loudness) > [70]> then
broadcast [treble detected v]
set [treble level v] to (current loudness)
end

wait (0.05) seconds
end
  

🎵 Method 2: Multi-Sprite Frequency Analysis

Create separate sprites for each frequency range analysis:

Bass Analyzer Sprite:

    // Bass Frequency Sprite
when flag clicked
forever
play sound [low tone 100Hz v] until done
set [bass response v] to (loudness)

if <(bass response) > [25]> then
change [bass counter v] by (1)
set [bass intensity v] to ((bass response) / [50])
broadcast [bass beat v]
end

wait (0.1) seconds
end
  

Mid-Range Analyzer Sprite:

    // Mid Frequency Sprite
when flag clicked
forever
play sound [mid tone 1000Hz v] until done
set [mid response v] to (loudness)

if <(mid response) > [30]> then
change [mid counter v] by (1)
set [mid intensity v] to ((mid response) / [60])
broadcast [mid beat v]
end

wait (0.1) seconds
end
  

Treble Analyzer Sprite:

    // High Frequency Sprite
when flag clicked
forever
play sound [high tone 5000Hz v] until done
set [treble response v] to (loudness)

if <(treble response) > [20]> then
change [treble counter v] by (1)
set [treble intensity v] to ((treble response) / [40])
broadcast [treble beat v]
end

wait (0.1) seconds
end
  

🎨 Method 3: Advanced Sampling Technique

For more accurate detection, use rapid sampling and analysis:

    // Advanced Frequency Detection
define analyze frequencies
set [sample count v] to [0]
set [low sum v] to [0]
set [mid sum v] to [0]
set [high sum v] to [0]

repeat (10)
set [current sample v] to (loudness)

// Analyze sample characteristics
if <(current sample) < [30]> then
change [low sum v] by (current sample)
end

if <(current sample) > [30]> then
if <(current sample) < [60]> then
change [mid sum v] by (current sample)
end
end

if <(current sample) > [60]> then
change [high sum v] by (current sample)
end

change [sample count v] by (1)
wait (0.01) seconds
end

// Calculate averages
set [bass average v] to ((low sum) / (sample count))
set [mid average v] to ((mid sum) / (sample count))
set [treble average v] to ((high sum) / (sample count))
  

🎭 Visual Response System

Create visual effects that respond to each frequency range:

    // Bass Response Visualization
when I receive [bass beat v]
set size to ((bass intensity) * [150])%
set [color v] effect to (([bass intensity v] * [50]) + [0])
glide (0.2) secs to x: (0) y: (0)
change size by (-20)

// Mid Response Visualization  
when I receive [mid beat v]
repeat (5)
turn right ((mid intensity) * [36]) degrees
move ((mid intensity) * [10]) steps
wait (0.05) seconds
end

// Treble Response Visualization
when I receive [treble beat v]
repeat (10)
change [brightness v] effect by ((treble intensity) * [10])
wait (0.02) seconds
change [brightness v] effect by (((treble intensity) * [10]) * [-1])
end
  

🔊 Real-Time Frequency Display

Create a visual frequency analyzer display:

    // Frequency Display Sprite
when flag clicked
forever
clear

// Draw bass bar
set pen color to [#ff0000]
pen up
go to x: [-100] y: [-100]
pen down
go to x: [-100] y: ([-100] + ([bass level v] * [3]))

// Draw mid bar
set pen color to [#00ff00]
pen up
go to x: [0] y: [-100]
pen down
go to x: [0] y: ([-100] + ([mid level v] * [3]))

// Draw treble bar
set pen color to [#0000ff]
pen up
go to x: [100] y: [-100]
pen down
go to x: [100] y: ([-100] + ([treble level v] * [3]))

wait (0.1) seconds
end
  

This creates a real-time frequency analyzer that shows bass (red), mid (green), and treble (blue) levels! 🎵

AS

AudioCoder_Sam

Replied 45 minutes later

@AudioPro_Expert This is absolutely incredible! 🤩 The multi-sprite approach is genius!

I implemented the bass detection and it’s working perfectly. Quick question - how can I make the frequency detection more sensitive to subtle changes in the audio?

SE

SoundEngineer_Maya

Replied 1 hour later

@AudioCoder_Sam For more sensitivity, try adjusting the sampling rate and using smoothing:

    // Enhanced Sensitivity Detection
define smooth detection (input) (smoothing factor)
set [smoothed value v] to (((smoothed value) * (smoothing factor)) + ((input) * ((1) - (smoothing factor))))

// Use in your main loop
when flag clicked
forever
smooth detection (loudness) [0.8]
// Use [smoothed value] instead of direct loudness
wait (0.02) seconds // Faster sampling
end
  

This creates much smoother and more responsive frequency detection! ✨

VB

Vibelf_Community

Pinned Message • Moderator

🎵 Ready to Master Audio Programming?

Amazing discussion on frequency detection! For those wanting to explore even more advanced audio programming techniques:

  • 🎛️ Real-time audio effects processing
  • 🎼 Music visualization systems
  • 🔊 Advanced sound synthesis
  • 🎧 Interactive audio experiences

📚 Related Audio Topics

Ready to create professional-level audio experiences? Get personalized guidance from our audio programming experts!