Skip to content

Commit

Permalink
Add save button for saving to MIDI
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Louie committed Nov 18, 2021
1 parent c07f6fa commit de3d40b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/components/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import {
SelectAll,
Undo,
Redo,
Save,
} from '@material-ui/icons';
import { observer } from 'mobx-react';

import { generator, player, layout, editor, EditorTool, undo } from '../core';
import saveload from '../core/save-load';
import * as theme from '../core/theme';
import { Voice } from '../core/note';

Expand Down Expand Up @@ -83,6 +85,16 @@ export class Controls extends React.Component<{}, State> {
{showPlay ? <PlayArrow /> : <Stop />}
</Button>
</div>
<div>
<Button
disabled={playDisabled}
variant="outlined"
color="primary"
onClick={() => saveload.saveMIDI()}
>
<Save />
</Button>
</div>
<div>
<Button
disabled={!undo.canUndo}
Expand Down
16 changes: 15 additions & 1 deletion src/core/save-load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { undo } from '../core';
import { undo, editor, player } from '../core';
import { getDateString } from './utils';
import featureFlags from './feature-flags';
import logging, { Events } from './logging';
import { sequenceProtoToMidi } from '@magenta/music';
import { getMagentaNoteSequence } from './magenta-utils';

export class SaveLoad {
saveJSON() {
Expand All @@ -13,6 +15,18 @@ export class SaveLoad {
this.downloadBlob(blob, `cococo_state_${getDateString()}_${ffMeta}`);
}

saveMIDI() {
const inputNotes = [...editor.allNotes];
const sequence = getMagentaNoteSequence(
inputNotes,
player.bpm,
editor.totalSixteenths,
true
);
const blob = new Blob([sequenceProtoToMidi(sequence)], { type: 'audio/midi' });
this.downloadBlob(blob, `cococo_music_${getDateString()}`);
}

loadJSON() {
const input = document.createElement('input');
input.type = 'file';
Expand Down

0 comments on commit de3d40b

Please sign in to comment.