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: use pack bpm/key after choosing #57913

Merged
merged 2 commits into from
Apr 12, 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
22 changes: 19 additions & 3 deletions apps/src/music/player/MusicLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,28 @@ export default class MusicLibrary {
return foldersCopy;
}

// Returns the library BPM, or the BPM of the currently selected pack if present.
getBPM(): number | undefined {
return this.bpm;
if (!this.currentPackId) {
return this.bpm;
}
const folder = this.getFolderForFolderId(this.currentPackId);
// Read BPM from the folder, or the first sound that has a BPM if not present.
Copy link
Member

Choose a reason for hiding this comment

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

super nit: "if not present" refers to "the folder"?

return (
folder?.bpm || folder?.sounds.find(sound => sound.bpm !== undefined)?.bpm
);
}

// Returns the library key, or the key of the currently selected pack if present.
getKey(): Key | undefined {
return this.key;
if (!this.currentPackId) {
return this.key;
}
const folder = this.getFolderForFolderId(this.currentPackId);
// Read key from the folder, or the first sound that has a key if not present.
return (
folder?.key || folder?.sounds.find(sound => sound.key !== undefined)?.key
);
}
}

Expand Down Expand Up @@ -276,7 +292,7 @@ export interface SoundFolder {
restricted?: boolean;
sounds: SoundData[];
bpm?: number;
key?: string;
key?: Key;
}

export type LibraryJson = {
Expand Down
5 changes: 5 additions & 0 deletions apps/src/music/player/ToneJSPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ class ToneJSPlayer implements AudioPlayer {
}

setBpm(bpm: number) {
if (Transport.bpm.value === bpm) {
return;
}
Transport.bpm.value = bpm;
// We need to regenerate all effect busses when BPM changes as some effects (e.g. delay) are BPM-dependent.
this.generateEffectBusses();
}

scheduleSample(sample: SampleEvent) {
Expand Down
8 changes: 5 additions & 3 deletions apps/src/music/views/hooks/useUpdatePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function useUpdatePlayer(player: MusicPlayer) {
state => state.music.startingPlayheadPosition
);
const isPlaying = useAppSelector(state => state.music.isPlaying);
const packId = useAppSelector(state => state.music.packId);

const dispatch = useAppDispatch();
const library = MusicLibrary.getInstance();
Expand All @@ -35,13 +36,14 @@ export default function useUpdatePlayer(player: MusicPlayer) {
}
const bpm = library.getBPM();
const key = library.getKey();
if (bpm) {
if (bpm !== undefined) {
dispatch(setBpm(bpm));
}
if (key) {
if (key !== undefined) {
dispatch(setKey(key));
}
}, [library, dispatch]);
// Update BPM and key whenever current pack ID changes.
}, [library, dispatch, packId]);

useEffect(() => {
player.setLoopEnabled(loopEnabled);
Expand Down