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

ソング:シーケンスを作成する処理を関数化してRENDER関数の外に出す #2275

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
Clipper,
Limiter,
NoteEvent,
NoteSequence,
OfflineTransport,
PolySynth,
Sequence,
Expand Down Expand Up @@ -329,6 +330,49 @@ const deleteSequence = (sequenceId: SequenceId) => {
}
};

/**
* ノートシーケンスを生成する。
*/
const generateNoteSequence = (
notes: Note[],
tempos: Tempo[],
tpqn: number,
trackId: TrackId,
): NoteSequence & { trackId: TrackId } => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const noteEvents = generateNoteEvents(notes, tempos, tpqn);
const polySynth = new PolySynth(audioContext);
return {
type: "note",
instrument: polySynth,
noteEvents,
trackId,
};
};

/**
* オーディオシーケンスを生成する。
*/
const generateAudioSequence = async (
startTime: number,
blob: Blob,
trackId: TrackId,
): Promise<AudioSequence & { trackId: TrackId }> => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const audioEvents = await generateAudioEvents(audioContext, startTime, blob);
const audioPlayer = new AudioPlayer(audioContext);
return {
type: "audio",
audioPlayer,
audioEvents,
trackId,
};
};

/**
* `tracks`と`trackChannelStrips`を同期する。
* シーケンスが存在する場合は、ChannelStripとシーケンスの接続・接続の解除を行う。
Expand Down Expand Up @@ -1535,11 +1579,6 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
};

const render = async () => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const audioContextRef = audioContext;

const firstRestMinDurationSeconds = 0.12;

// レンダリング中に変更される可能性のあるデータのコピー
Expand Down Expand Up @@ -1735,20 +1774,15 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
}

// ノートシーケンスを作成して登録し、プレビュー音が鳴るようにする
const noteEvents = generateNoteEvents(
// ノートシーケンスを生成して登録し、プレビュー音が鳴るようにする
const sequenceId = SequenceId(uuid4());
const noteSequence = generateNoteSequence(
phrase.notes,
snapshot.tempos,
snapshot.tpqn,
phrase.trackId,
);
const polySynth = new PolySynth(audioContextRef);
const sequenceId = SequenceId(uuid4());
registerSequence(sequenceId, {
type: "note",
instrument: polySynth,
noteEvents,
trackId: phrase.trackId,
});
registerSequence(sequenceId, noteSequence);
mutations.SET_SEQUENCE_ID_TO_PHRASE({ phraseKey, sequenceId });
}
}
Expand Down Expand Up @@ -1791,7 +1825,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
}

// オーディオシーケンスを作成して登録する
// オーディオシーケンスを生成して登録する
const singingVoiceKey = getPhraseSingingVoiceKey(phraseKey);
if (singingVoiceKey == undefined) {
throw new Error("singingVoiceKey is undefined.");
Expand All @@ -1800,19 +1834,13 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phraseSingingVoices,
singingVoiceKey,
);
const audioEvents = await generateAudioEvents(
audioContextRef,
const sequenceId = SequenceId(uuid4());
const audioSequence = await generateAudioSequence(
phrase.startTime,
singingVoice,
phrase.trackId,
);
const audioPlayer = new AudioPlayer(audioContext);
const sequenceId = SequenceId(uuid4());
registerSequence(sequenceId, {
type: "audio",
audioPlayer,
audioEvents,
trackId: phrase.trackId,
});
registerSequence(sequenceId, audioSequence);
mutations.SET_SEQUENCE_ID_TO_PHRASE({ phraseKey, sequenceId });

mutations.SET_STATE_TO_PHRASE({
Expand Down
Loading