首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为制作的音乐向函数piano_roll_to_pretty_midi添加pitch_bend更改?

如何为制作的音乐向函数piano_roll_to_pretty_midi添加pitch_bend更改?
EN

Stack Overflow用户
提问于 2020-04-24 05:33:08
回答 2查看 79关注 0票数 0

我想使用from函数piano_roll_to_pretty_midi来转换属性音乐,但我不知道如何将pitch_bend更改添加到此函数,这是cod piano_roll:

代码语言:javascript
复制
def piano_roll_to_pretty_midi(piano_roll, fs=100, program=0):
    '''Convert a Piano Roll array into a PrettyMidi object
     with a single instrument.

    Parameters
    ----------
    piano_roll : np.ndarray, shape=(128,frames), dtype=int
        Piano roll of one instrument
    fs : int
        Sampling frequency of the columns, i.e. each column is spaced apart
        by ``1./fs`` seconds.
    program : int
        The program number of the instrument.

    Returns
    -------
    midi_object : pretty_midi.PrettyMIDI
        A pretty_midi.PrettyMIDI class instance describing
        the piano roll.

    '''
    notes, frames = piano_roll.shape
    pm = pretty_midi.PrettyMIDI()
    instrument = pretty_midi.Instrument(program=program)

    # pad 1 column of zeros so we can acknowledge inital and ending events
    piano_roll = np.pad(piano_roll, [(0, 0), (1, 1)], 'constant')

    # use changes in velocities to find note on / note off events
    velocity_changes = np.nonzero(np.diff(piano_roll).T)

    # keep track on velocities and note on times
    prev_velocities = np.zeros(notes, dtype=int)
    note_on_time = np.zeros(notes)

    for time, note in zip(*velocity_changes):
        # use time + 1 because of padding above
        velocity = piano_roll[note, time + 1]
        time = time / fs
        if velocity > 0:
            if prev_velocities[note] == 0:
                note_on_time[note] = time
                prev_velocities[note] = velocity
        else:
            pm_note = pretty_midi.Note(
                velocity=prev_velocities[note],
                pitch=note,
                start=note_on_time[note],
                end=time)
            instrument.notes.append(pm_note)
            prev_velocities[note] = 0
    pm.instruments.append(instrument)
    return pm
EN

回答 2

Stack Overflow用户

发布于 2020-05-22 07:05:55

您可以将PitchBend对象添加到仪器的pitch_bends列表。这是一个MWE,它可以添加钢琴卷上的音符,也可以弯曲它们:

代码语言:javascript
复制
from pretty_midi import *
from numpy import *

roll = [[1, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 1]]
roll = array(roll).T

notes, frames = roll.shape
pm = PrettyMIDI()
instr = Instrument(program = 80)
roll = pad(roll, [(0, 0), (1, 1)], 'constant')
velocity_changes = nonzero(np.diff(roll).T)
prev_velocities = zeros(notes, dtype=int)
note_on_time = zeros(notes)
for time, note in zip(*velocity_changes):
    velocity = roll[note, time + 1]
    if velocity > 0:
        if prev_velocities[note] == 0:
            note_on_time[note] = time
            prev_velocities[note] = velocity
    else:
        start_time = note_on_time[note]
        pm_note = Note(
            velocity = 120,
            pitch = note + 40,
            start = start_time,
            end=time)
        instr.notes.append(pm_note)
        for i in range(20):
            time = start_time + i / 20
            bend = PitchBend(400 * i, time)
            instr.pitch_bends.append(bend)
        prev_velocities[note] = 0
pm.instruments.append(instr)
pm.write('test.mid')
票数 0
EN

Stack Overflow用户

发布于 2020-06-11 20:52:33

代码语言:javascript
复制
import tensorflow as tf
from tensorflow.keras import backend as K
import glob
import random
from pretty_midi import* 
import IPython
import numpy as np
#from tqdm import tnrange, tqdm_notebook, tqdm
from random import shuffle, seed
import numpy as np
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Nadam
import numpy as np
from numpy.random import choice
import pickle
import matplotlib.pyplot as plt
from tqdm._tqdm_notebook import tqdm_notebook,tnrange, tqdm_notebook, tqdm
import unicodedata
import re
import numpy as np
import os
import io
import time
from numpy import *
#stragglers = {}
#channel==0
piano_roll = [[1, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 1]]
piano_roll = array(piano_roll).T
note_name=['A5','B5','C5','D5','E5','F5','G5']
def piano_roll_to_pretty_midi(piano_roll, fs=100, program=80):
notes, frames = piano_roll.shape
pm = pretty_midi.PrettyMIDI()
instrument = pretty_midi.Instrument(program=program)


#semitone_range=2.
#for pitch_bend in range (len(utilities.semitones_to_pitch_bend(semitones,                                                                                                                                        semitone_range=2.))):
#tone=[-8192,8192]
#quarter_tone==tone%4
notes, frames = piano_roll.shape
pm = PrettyMIDI()
#instr = Instrument(program = 80)
piano_roll = pad(piano_roll, [(0, 0), (1, 1)], 'constant')
velocity_changes = nonzero(np.diff(piano_roll).T)
prev_velocities = zeros(notes, dtype=int)
note_on_time = zeros(notes)
for time, note in zip(*velocity_changes):
    velocity = piano_roll[note, time + 1]
    if velocity > 0:
        if prev_velocities[note] == 0:
            note_on_time[note] = time
            prev_velocities[note] = velocity
    else:
        start_time = note_on_time[note]
        pm_note = Note(
            velocity = 10,
            pitch = note +10,
            start = start_time,
            end=time)
        instrument.notes.append(pm_note)
        for i in range(20):
            if note_name=='B5':
                time = start_time + i / 20
                bend = PitchBend(102 * i, time)
                instrument.pitch_bends.append(bend)
            prev_velocities[note] = 0
pm.instruments.append(instrument)
return pm
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61397365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档