从时间序列数据生成MIDI文件。你可以控制可以控制你想要的八度音阶和八度音阶。
miditime的Python项目详细描述
你有时间序列数据要作为音乐播放吗?当然 是的
MIDITime将任何类型的时间序列数据转换为音高、速度和 持续时间值基于您设置的音乐选项,然后输出 .mid文件。
从技术上讲,MIDI文件不是音频文件,而是有关如何 应该演奏软件乐器。你可以播放.mid文件 直接在某些音乐应用程序中,或将其导入多种音乐中 音乐编辑(如ProTools,Ableton,MaxMSP)和添加大量铃声 吹口哨准备广播音频。
我们使用miditime在this episode of Reveal中生成数据声音化。 音乐的轨道-没有谈话-is here。
安装
pipinstallmiditime
用法
非常基本:
frommiditime.miditimeimportMIDITime# Instantiate the class with a tempo (120bpm is the default) and an output file destination.mymidi=MIDITime(120,'myfile.mid')# Create a list of notes. Each note is a list: [time, pitch, velocity, duration]midinotes=[[0,60,127,3],#At 0 beats (the start), Middle C with velocity 127, for 3 beats[10,61,127,4]#At 10 beats (12 seconds from start), C#5 with velocity 127, for 4 beats]# Add a track with those notesmymidi.add_track(midinotes)# Output the .mid filemymidi.save_midi()
多点乐趣,多点控制:
用一个速度(默认为120bpm)实例化类,输出 文件目标,要表示一年的秒数 最后一首歌(默认为5秒/年),基本八度音阶(C5是中间C, 所以默认值是5,你希望输出的八度音程是多少 结束(默认值为1)
frommiditime.miditimeimportMIDITimemymidi=MIDITime(120,'myfile.mid',5,5,1)
带来一些数据(这是一些地震)。我想你的数据是 已经按日期顺序排列,从最早到最新。
my_data=[{'event_date':<datetimeobject>,'magnitude':3.4},{'event_date':<datetimeobject>,'magnitude':3.2},{'event_date':<datetimeobject>,'magnitude':3.6},{'event_date':<datetimeobject>,'magnitude':3.0},{'event_date':<datetimeobject>,'magnitude':5.6},{'event_date':<datetimeobject>,'magnitude':4.0}]
将日期/时间数据转换为整数,如从纪元开始的天数 (1970年1月1日)。您可以使用days_since_epoch()helper方法,或者 不:
my_data_epoched=[{'days_since_epoch':mymidi.days_since_epoch(d['event_date']),'magnitude':d['magnitude']}fordinmy_data]
将您的整数日期/时间转换为歌曲的合理日期/时间。为了 例如,以每分钟120次的速度,您需要将数据缩小到 如果你的数据跨越数年,那就避免唱很长的歌。它使用 你在顶部设置的秒/年属性,所以如果你的日期是 转换成你可能需要自己做的事情 转换但是如果你的数据集跨越了几年,而且你的日期是以天为单位的 (分数可以),使用beat()helper方法。
my_data_timed=[{'beat':mymidi.beat(d['days_since_epoch']),'magnitude':d['magnitude']}fordinmy_data_epoched]
获取序列中的最早日期,以便在 MIDI:
start_time=my_data_timed[0]['beat']
设置一些函数来缩放其他变量(我们的 大小写)以匹配所需的模式/键和倍频程范围。有帮手 方法来帮助这种缩放,非常类似于 D3号您可以选择线性或对数刻度
defmag_to_pitch_tuned(magnitude):# Where does this data point sit in the domain of your data? (I.E. the min magnitude is 3, the max in 5.6). In this case the optional 'True' means the scale is reversed, so the highest value will return the lowest percentage.scale_pct=mymidi.linear_scale_pct(3,5.7,magnitude)# Another option: Linear scale, reverse order# scale_pct = mymidi.linear_scale_pct(3, 5.7, magnitude, True)# Another option: Logarithmic scale, reverse order# scale_pct = mymidi.log_scale_pct(3, 5.7, magnitude, True)# Pick a range of notes. This allows you to play in a key.c_major=['C','D','E','F','G','A','B']#Find the note that matches your data pointnote=mymidi.scale_to_note(scale_pct,c_major)#Translate that note to a MIDI pitchmidi_pitch=mymidi.note_to_midi_pitch(note)returnmidi_pitch
现在建立你的笔记列表
note_list=[]fordinmy_data_timed:note_list.append([d['beat']-start_time,mag_to_pitch_tuned(d['magnitude']),100,# velocity1# duration, in beats])
完成
# Add a track with those notesmymidi.add_track(note_list)# Output the .mid filemymidi.save_midi()
许可证
这个软件是在麻省理工学院的许可下发布的。那太好了 如果你把揭秘和迈克尔·科里归功于 太棒了。
学分
非常感谢茱莉亚史密斯帮助我理解音乐剧 按键/模式更好。
MIDITime是一个包装器,它包裹着midi,使 midiutil,由Mark Conway Wirt生产我有 此包中包含midiutil per his recommendation。