而且这是我第一次在这里张贴,所以很抱歉,如果我搞砸了什么。我不知道该怎么称呼这个,但我的课程计划,但我蛮力地迫使我今天的问题,并想知道是否有人有一个更有效的方法来做我需要的。我花了两个小时的时间在这上面,很想看看你们会怎么处理
背景:使用midi库,您可以在播放时提取音频曲目的音符。工作之后,你会得到这样的结果:
notes = ['B3', 'C4', 'G2', 'G3', 'B3', 'D4', 'F4', 'G2', 'D4', 'F4']
ticks = [0, 1, 12, 12, 12, 15, 15, 22, 22,]
记号基本上是midi的时域。它表示该音符第一次播放的时间,并且列表ticks
对应于notes
(notes[0]
在时间ticks[0]
播放)。出于我的目的,我一次只能通过4台设备中的1台播放一个音符。因此,当我重复滴答声时,我需要同时弹奏一个和弦或多个音符。默认情况下,设备(channel0)播放一个音符,channel1播放两个音符,依此类推
例如,上面的音轨是这样播放的
问题:给定notes
和ticks
,用正确的时间和注释为通道0-3创建指令。如果一个和弦有4个以上的音符,我就省略它们,因为我对它们无能为力。基本上,我需要数据结构如下所示:
channel0 = ['B3', 'C4', 'G2', 'D4']
channel1 = [ 0, 0, 'G3', 'F4']
channel2 = [ 0, 0, 'B3', 0 ]
channel3 = [ 0, 0, 0, 0 ]
我第一次尝试用一个由if语句组成的怪物来暴力攻击它,但最终还是想到了这个
解决方案:首先,我创建了一个元组列表来比较轨迹中每个勾号出现的次数,但这是毫无意义的。我刚把它做成一个数组
res = []
for i in ticks:
if i not in res:
res.append(i)
counts = [(ticks.count(x)) for x in res]
然后,我列了一个频道列表。我把它们倒着点,因为从下到上的“矩阵”比较容易
ch0, ch1, ch2, ch3 = ([] for i in range(4))
final = [ch3, ch2, ch1, ch0]
最后,这件可耻的事情终于奏效了
countsidx = -1
while breakMe:
countsidx += 1
for finalidx, vchan in enumerate(final):
if int(finalidx) >= counts[countsidx]:
vchan.append(0)
else:
vchan.append(notes[notesidx])
notesidx += 1
if notesidx == len(notes):
return final
breakMe = False
return final
所以我的问题是:我怎么能用更少的代码来完成这个任务?有没有人有更简单的方法来做我做的同样的事?我喜欢学习最佳实践。我觉得我在这件简单的事情上花了太多时间
完整代码
import matplotlib.pyplot as plt
import argparse
import sys
import collections
from mido import MidiFile
from midiutil import MIDIFile
import sys
NOTES = ['C', 'CS', 'D', 'DS', 'E', 'F', 'FS', 'G', 'GS', 'A', 'AS', 'B']
OCTAVES = list(range(11))
NOTES_IN_OCTAVE = len(NOTES)
def countShit(ticks,notes):
breakMe = True
res = []
for i in ticks:
if i not in res:
res.append(i)
counts = [(ticks.count(x)) for x in res]
print('Counts (ticks,occurences) == ', counts)
ch0,ch1,ch2,ch3, = ([] for i in range(4))
final =[ch3,ch2,ch1,ch0]
notesidx = 0
countsidx = -1
while breakMe:
countsidx += 1
for finalidx, vchan in enumerate(final):
if int(finalidx) >= counts[countsidx]:
vchan.append(0)
else:
vchan.append(notes[notesidx])
notesidx += 1
if notesidx == len(notes):
return final
breakMe = False
return final
def convertTuple(tup):
str = ''.join(tup)
return str
def number_to_note(number: int) -> tuple:
octave = number // NOTES_IN_OCTAVE
assert octave in OCTAVES, errors['notes']
assert 0 <= number <= 127, errors['notes']
note = NOTES[number % NOTES_IN_OCTAVE]
return str(note),str(octave-2)
song = midi.read_midifile('mario_06.mid')
song.make_ticks_abs()
tracks =[]
trackNotes =[]
trackTime = []
trackTicks=[]
for track in song:
notes = [note for note in track if note.name == 'Note On']
notes2 = [note for note in track if note.name == 'Note Off']
pitch = [note.pitch for note in notes]
tick = [note.tick for note in notes]
trackTime =[b.tick - a.tick for a,b in zip(notes,notes2)]
tracks += [tick, pitch]
trackNotes += pitch
trackTicks += tick
trackNotesFinal =[]
for i in trackNotes:
k = str(convertTuple(number_to_note(i)))
trackNotesFinal.append(k)
trackNotesFinal
channels = countShit(trackTicks,trackNotesFinal)
下面是一个使用列表理解的解决方案,使用} 。我假设
zip
和^{ticks
数组总是有序的我用
None
而不是0
来表示没有注释,因为这更像是python(而且作为一个额外的优点,在打印时会使矩阵列很好地排列起来)由于您可能还想知道矩阵中每列的计时,因此可以使用另一个列表:
相关问题 更多 >
编程相关推荐