如何将一个Numpy数组转换为和弦音乐? [英] How do I translate a numpy array to polyphonic music?

查看:123
本文介绍了如何将一个Numpy数组转换为和弦音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数组播放音乐/声音.输出的音乐/声音需要是复音的.

I want to use an array to play music/sounds. The output music/sounds needs to be polyphonic.

我尝试过:

from scipy.io.wavfile import write
import numpy as np

duration=0.24
amp=1E4
rate=44100

def note(freq, duration, amp, rate):
    t = np.linspace(0, duration, duration * rate)
    data = np.sin(2*np.pi*freq*t)*amp
    return data.astype(np.int16) # two byte integers

tone0 = note(0, duration, amp, rate) #silence
tone1 = note(261.63, duration, amp, rate) # C4
tone2 = note(329.63, duration, amp, rate) # E4
tone3 = note(392.00, duration, amp, rate) # G4


seq1 = np.concatenate((tone1,tone0,tone0,tone0, tone1),axis=1)
seq2 = np.concatenate((tone0,tone2,tone0,tone0, tone2),axis=1)
seq3 = np.concatenate((tone0,tone0,tone3,tone0, tone3),axis=1)

song = np.dstack((seq1,seq2,seq3))

write('song.wav', 44100, song)

我想播放song.wav文件,并依次听到C,E和G音符,然后静音,然后按C和弦(C,E,G音符同时播放).

I would like to play the song.wav file and hear the notes C, E and G one after the other then a silence and then the C chord (C,E,G notes play at the same time).

我得到的是写功能出错.没关系,因为写功能(据我所知它不能创建和弦的wav文件).

What I get, instead is an error by the write function. And that's ok because the write function (as far as I know it can´t create polyphonic wav files).

以防万一,错误是:

Traceback (most recent call last):
  File "music2.py", line 26, in <module>
    write('song.wav', 44100, song)
  File "/usr/lib/python2.7/dist-packages/scipy/io/wavfile.py", line 168, in write
    fid.write(struct.pack('<ihHIIHH', 16, 1, noc, rate, sbytes, ba, bits))
struct.error: 'I' format requires 0 <= number <= 4294967295

  1. 您知道支持复音的写函数的替代方法吗?
  2. 我愿意接受代码的完整替代方案.重要的一点是从一个numpy数组生成(或播放)和弦声音.我正在尝试执行以下操作: http://img44.imageshack.us/img44 /6182/music.swf ,但简单得多;不是GUI;不是交互式的,只是分配行和特定的声音/音符以及这些音符的播放时间的列代码,都是1和0数组的相同想法.
  1. Do you know an alternative to the write function that support polyphony?
  2. I am open to a complete alternative of my code. The important point is to generate (or play) polyphonic sounds from a numpy array. I am trying to do something like this: http://img44.imageshack.us/img44/6182/music.swf, but much simpler; not GUI; not interactive, just the same idea of a 1 and 0 array were the row have assigned and specific sound/note and the columns code for the time those notes are played.

推荐答案

您收到的错误是由于write只需要一维或二维数组.您正在传递一个3维数组(dstack的输出是3D).

The error you're getting is due to write wanting only a 1- or 2-dimensional array. You are passing it a 3-dimensional array (the output of dstack is 3D).

我不确定我所说的复音是什么意思,但是如果您只是想表示要在彼此之间叠加不同的音调,那么您要做的就是叠加波形:

I'm not sure I get what you mean by polyphonic, but if you simply mean that you want to have different tones overlaid on each-other then all you need to do is superimpose the waveforms:

song = seq1 + seq2 + seq3 # Assumes seqs are of same length

最后,您可能希望传递一维数组.如果要写入立体声,则使用二维阵列.

In the end you probably want to be passing a 1-D array. 2-D arrays are for if you want to write stereo sounds.

这篇关于如何将一个Numpy数组转换为和弦音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆