如何翻译一个numpy的阵列和弦音乐 [英] how to translate a numpy array to polyphonic music

查看:558
本文介绍了如何翻译一个numpy的阵列和弦音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个数组来播放音乐/声音。输出音乐/声音必须和弦。

我试试这个。

 从scipy.io.wavfile进口写
导入numpy的是NP持续时间= 0.24
功放= 1E4
率= 44100高清说明(频率,持续时间,安培,率):
    T = np.linspace(0,持续时间,持续时间*率)
    数据= np.sin(2 * np.pi *频率* T)*放大器
    返回data.astype(np.int16)#两个字节的整数tone0 =注(0,持续时间,安培,率)#silence
TONE1 =注(261.63,持续时间,安培,率)#4
tone2 =注(329.63,持续时间,安培,率)E4#
tone3 =注(392.00,持续时间,安培,率)#G4
SEQ1 = np.concatenate((TONE1,tone0,tone0,tone0,TONE1),轴= 1)
SEQ2 = np.concatenate((tone0,tone2,tone0,tone0,tone2),轴= 1)
SEQ3 = np.concatenate((tone0,tone0,tone3,tone0,tone3),轴= 1)曲= np.dstack((SEQ1,SEQ2 SEQ3))写(song.wav',44100,歌)

我想打song.wav文件和听到音符C,E和G逐个然后一片寂静,然后C和弦(C,E,G音符在同一时间播放)。<后/ p>

我得到什么,而不是由写入功能的错误。并且确定因为写函数(据我所知它不容创建和弦WAV文件)。万一误差

 回溯(最后最近一次调用):
  文件music2.py26行,上述&lt;&模块GT;
    写(song.wav',44100,歌)
  文件/usr/lib/python2.7/dist-packages/scipy/io/wavfile.py,168线,在写
    fid.write(struct.pack('&LT; ihHIIHH',16,1,NOC,速度,sbytes,BA,位))
struct.error:'I'格式要求0℃=号&LT; = 4294967295

1)你知道到支持复音写功能的方法吗?
2)我打开我的code的完整的备选方案。最重要的一点是要产生(或播放)和弦从numpy的阵列声音。我试图做这样的事情 http://img44.imageshack.us/img44/6182 /music.swf ,但更简单,没有GUI,没有互动,只是1和0阵列同样的想法是该行已指定和特定的声音/笔记,列code的时间这些笔记被演奏。


解决方案

你得到的错误是由于想只有1或2维数组。你是传递一个3维数组( dstack 的输出3D)。

我不知道我让你通过和弦的意思,但如果你只是意味着你想拥有覆盖每个 - 其他不同的音调,那么所有你需要做的是叠加的波形:

 歌曲= SEQ1 SEQ2 + + SEQ3#假设seqs是相同的长度

在最后,你可能要被路过的一维数组。 2-D阵列是,如果你想写的立体声声音。

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

I try this.

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)

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).

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

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) 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 more 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.

解决方案

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天全站免登陆