Skip to content
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

Music: fix preview plays #58491

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions 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),
preview: soundData.type === 'preview',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead check this in calculatePitchShift and just return a pitch shift of 0 if soundData.type === preview (similar to how we do for 'beat')?

Looks like we'd still need some field to also indicate that we don't want tempo adjustment but then maybe we can rename this to disableTempoAdjustment or similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. Done.

},
];
} else if (event.type === 'pattern') {
Expand Down
9 changes: 7 additions & 2 deletions apps/src/music/player/ToneJSPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,16 @@ class ToneJSPlayer implements AudioPlayer {
return;
}

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

const pitchShift = sample.preview ? 0 : sample.pitchShift;

const player = this.createPlayer(
buffer,
playbackRate,
sample.pitchShift
pitchShift
).toDestination();

player.onstop = () => {
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 the event is a preview that should be played in original form.
preview?: boolean;
}

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