-
Notifications
You must be signed in to change notification settings - Fork 24
[WIP] Add audio time-scale and pitch modification functionality #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
11
commits into
main
Choose a base branch
from
copilot/add-audio-time-scale-modification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e928c15
Initial plan
Copilot de322b5
Add audio pitch shifting functionality with interpolation
Copilot 4b129a2
Add documentation for pitch shifting feature
Copilot 385cc49
Fix code review issues - correct playback position tracking and reduc…
Copilot f65f161
Changes before error encountered
Copilot 3f01ba9
sl
charneykaye 75e747a
Fix CI build errors - update SetFireWithPitch API to include ADSR par…
Copilot 503b190
Apply PR review feedback - fix bugs and improve API design
Copilot c2d6231
Apply second round of PR review feedback
Copilot ba308c8
Apply third round of PR review feedback
Copilot dbf2aba
Apply fourth round of PR review feedback
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Audio Pitch Shifting | ||
|
|
||
| Mix now supports pitch shifting audio without manual resampling, allowing you to change the pitch of audio sources dynamically. | ||
|
|
||
| ## Usage | ||
|
|
||
| Use `SetFireWithPitch()` instead of `SetFire()` to enable pitch shifting: | ||
|
|
||
| ```go | ||
| import ( | ||
| "time" | ||
| "github.com/go-mix/mix" | ||
| ) | ||
|
|
||
| // Normal playback | ||
| mix.SetFire("sound.wav", time.Duration(0), 0, 1.0, 0) | ||
|
|
||
| // Pitch shifted up one octave (2.0x pitch) | ||
| mix.SetFireWithPitch("sound.wav", time.Duration(0), 0, 1.0, 0, 2.0, 1.0) | ||
|
|
||
| // Pitch shifted down one octave (0.5x pitch) | ||
| mix.SetFireWithPitch("sound.wav", time.Duration(0), 0, 1.0, 0, 0.5, 1.0) | ||
|
|
||
| // Pitch shifted up a perfect fifth (~1.5x pitch) | ||
| mix.SetFireWithPitch("sound.wav", time.Duration(0), 0, 1.0, 0, 1.5, 1.0) | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| ```go | ||
| func SetFireWithPitch( | ||
| source string, // path to audio file | ||
| begin time.Duration, // start time | ||
| sustain time.Duration, // sustain duration (0 = play full file) | ||
| volume float64, // volume (0 to 1) | ||
| pan float64, // pan (-1 to +1) | ||
| pitch float64, // pitch multiplier (1.0 = no change) | ||
| timeStretch float64 // time stretch multiplier (currently unused) | ||
| ) *fire.Fire | ||
| ``` | ||
|
|
||
| ## Pitch Multiplier | ||
|
|
||
| The `pitch` parameter is a multiplier: | ||
| - `1.0` = original pitch (no change) | ||
| - `2.0` = up one octave (twice the frequency) | ||
| - `0.5` = down one octave (half the frequency) | ||
| - `1.5` = up a perfect fifth | ||
| - `0.75` = down a perfect fourth | ||
|
charneykaye marked this conversation as resolved.
|
||
|
|
||
| ## How It Works | ||
|
|
||
| The implementation uses sample-rate modification with linear interpolation: | ||
| - Higher pitch values increase playback speed (shorter duration) | ||
| - Lower pitch values decrease playback speed (longer duration) | ||
| - Linear interpolation between samples ensures smooth playback without artifacts | ||
|
|
||
| ## Note on Time Stretching | ||
|
|
||
| Currently, changing pitch also changes playback speed proportionally. True independent time-stretching (changing speed without affecting pitch) would require more advanced algorithms like phase vocoding or time-domain harmonic scaling, which may be added in future versions. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Pitch Shifting a Drum Loop | ||
|
|
||
| ```go | ||
| // Play a drum loop at different pitches for variation | ||
| mix.SetFireWithPitch("drums.wav", 0*time.Second, 0, 1.0, 0, 1.0, 1.0) // original | ||
| mix.SetFireWithPitch("drums.wav", 4*time.Second, 0, 1.0, 0, 0.9, 1.0) // slightly lower | ||
| mix.SetFireWithPitch("drums.wav", 8*time.Second, 0, 1.0, 0, 1.1, 1.0) // slightly higher | ||
| ``` | ||
|
|
||
| ### Creating Harmonies | ||
|
|
||
| ```go | ||
| // Play the same sample at different pitches to create harmony | ||
| baseTime := 2 * time.Second | ||
| mix.SetFireWithPitch("note.wav", baseTime, 0, 0.8, -0.5, 1.0, 1.0) // root (left) | ||
| mix.SetFireWithPitch("note.wav", baseTime, 0, 0.8, 0, 1.25, 1.0) // major third (center) | ||
| mix.SetFireWithPitch("note.wav", baseTime, 0, 0.8, 0.5, 1.5, 1.0) // perfect fifth (right) | ||
| ``` | ||
|
|
||
| ### Bass Drop Effect | ||
|
|
||
| ```go | ||
| // Create a bass drop by pitch shifting down over time | ||
| for i := 0; i < 10; i++ { | ||
| pitch := 1.0 - float64(i)*0.1 // gradually lower pitch | ||
| mix.SetFireWithPitch("bass.wav", time.Duration(i)*200*time.Millisecond, | ||
| 200*time.Millisecond, 1.0, 0, pitch, 1.0) | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.