перевод Magenta. js RNN в React - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь понять, как перевести простую программу javascript в ReactJS. Я думаю, что приближаюсь, но мне нужен пу sh. У меня есть класс, который должен возвращать последовательность мелодий, но сейчас он просто бесконечно играет 3 назначенные ему ноты.

Класс экспорта по умолчанию содержит всего несколько внутренних частей:

1, конструктор (с виду большой-i sh)

constructor(props) {
    super(props);

    this.improvCheckpoint = 'https://storage.googleapis.com/magentadata/js/checkpoints/music_rnn/chord_pitches_improv'
    this.improvRNN = new mm.MusicRNN(this.improvCheckpoint)
    this.improvRNN.initialize();

    this.synth = new Tone.Synth().toMaster()
    const { midi, Note } = Tonal

    this.player = new mm.Player();
    this.state = {
      BEATBEAT: {
        notes: [
          { pitch: 60, startTime: 0, endTime: 1 },
          { pitch: 72, startTime: 1, endTime: 3.5 },
          { pitch: 65, startTime: 3.5, endTime: 4 },
        ],
        ticksPerQuarter: 220,
        totalTime: 3,
        timeSignatures: [
          {
            time: 0,
            numerator: 4,
            denominator: 4
          }
        ],
        quantizationInfo: {stepsPerQuarter:5},
        tempos: [{time: 0, qpm: 180}],
        totalQuantizedSteps: 5
      }
    }

    this.quantizedSequence = mm.sequences.quantizeNoteSequence(this.state, 1)

    this.startProgram = async () => {

  try {
    await this.improvRNN.initialize()

    let improvisedMelody = await this.improvRNN.continueSequence(this.quantizedSequence, 60, 1.1, ['Bm', 'Bbm', 'Gb7', 'F7', 'Ab'])

    const playOriginalMelody = () => {
      this.state.notes.forEach(note => {
        this.synth.triggerAttackRelease(Note.fromMidi(note.pitch), note.endTime - note.startTime, note.startTime)
      })
    }

    const playGeneratedMelody = () => {
      improvisedMelody.notes.forEach(note => {
        this.synth.triggerAttackRelease(Note.fromMidi(note.pitch), note.quantizedEndStep - note.quantizedStartStep, note.quantizedStartStep)
      })
    }

  } catch (error) {
    console.error(error)
  }
}

2, проигрыватель, который может выводить заметки:

  function playRNN() {
      if (this.player.playOriginalMelody()) {
        this.player.stop();
        return;
      }
    }

3, componentDidMounts

  componentDidMount() {
    this.player.start(this.state.BEATBEAT);
  }

  componentWillUnmount() {
    this.player.stop();
  }

Мне кажется, что у меня есть что-то в конструкторе, но я рад принять любые и все ответы :)

исходный код, который я пытаюсь перевести:

https://github.com/FunmiOjo/pep-magenta/blob/master/index.js

...