Skip to content

Commit

Permalink
Merge pull request #58491 from code-dot-org/music-fix-preview-plays
Browse files Browse the repository at this point in the history
  • Loading branch information
breville committed May 11, 2024
2 parents 1d62c2a + 16e608d commit e897c0e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion apps/src/music/player/MusicPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export default class MusicPlayer {
effects: soundEvent.effects,
originalBpm: soundData.bpm || DEFAULT_BPM,
pitchShift: this.calculatePitchShift(soundData),
disableTempoAdjustment: soundData.type === 'preview',
},
];
} else if (event.type === 'pattern') {
Expand Down Expand Up @@ -550,7 +551,7 @@ export default class MusicPlayer {
}

private calculatePitchShift(soundData: SoundData) {
if (soundData.type === 'beat') {
if (['beat', 'preview'].includes(soundData.type)) {
return 0;
}
const diff = this.key - (soundData.key || Key.C);
Expand Down
5 changes: 4 additions & 1 deletion apps/src/music/player/ToneJSPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ class ToneJSPlayer implements AudioPlayer {
return;
}

const playbackRate = Transport.bpm.value / sample.originalBpm;
const playbackRate = sample.disableTempoAdjustment
? 1
: Transport.bpm.value / sample.originalBpm;

const player = this.createPlayer(
buffer,
playbackRate,
Expand Down
2 changes: 2 additions & 0 deletions apps/src/music/player/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export interface SampleEvent {
effects?: Effects;
// Length in measures to play the sample for
length?: number;
// Whether tempo should not be adjusted.
disableTempoAdjustment?: boolean;
}

/** A sequence of notes played on a sampler instrument */
Expand Down

0 comments on commit e897c0e

Please sign in to comment.