[英]IPython: Audio: concating list to add chords results in one single chord not several played one-by-one: `+=` vs `append`
I am completetly new to IPython hence I am sorry in case this is totally obvious:我是 IPython 的新手,如果这很明显,我很抱歉:
import IPython.display as ipd
import scipy
from scipy import signal as sp
import math
import numpy as np
I defined two functions which should help me generate chords:我定义了两个函数来帮助我生成和弦:
def f_k(f_0, k):
return f_0*2**((k%12)/12)
def h_k(k, f0, t):
return math.sin(2*math.pi*f_k(f0, k)*t)
F = 44000
T = 2
f0 = 440
N = F*T
I define H0
:我定义
H0
:
H0 = []
for k in [0,4,7]:
H0.append([h_k(k, f0, t) for t in np.linspace(0, T, N)])
ipd.Audio(H0, rate=F)
and it plays 2 seconds as expected because of the discretization through linspace.由于通过 linspace 离散化,它按预期播放了 2 秒。
I defined a few several chords and I wanted to concat the lists to get several chords (I expected the sound to be 8 secs long)我定义了几个和弦,我想连接列表以获得几个和弦(我希望声音长 8 秒)
H5 = []
for k in [5,9,12]:
H5.append([h_k(k, f0, t) for t in np.linspace(0, T, N)])
H7 = []
for k in [7, 11, 14]:
H7.append([h_k(k, f0, t) for t in np.linspace(0, T, N)])
H9 = []
for k in [5,9,16]:
H9.append([h_k(k, f0, t) for t in np.linspace(0, T, N)])
added_sample = []
for h in [H0, H7, H9, H5]:
added_sample += h
ipd.Audio(added_sample, rate=F)
Yet the sound is somehow 2secs long.然而声音不知何故有 2 秒长。 Could someone explain how to add chords insted of 'layering' them?
有人可以解释如何添加和弦而不是“分层”它们吗? Any hint would be greatly appreciated!
任何提示将不胜感激!
Maybe this small example will help you see the different between append
and +=
:也许这个小例子可以帮助您了解
append
和+=
之间的区别:
For each we run:对于我们运行的每一个:
In [54]: alist = [1,2,3]; blist = [4,5]
append
adds one item to the list: append
向列表中添加一项:
In [55]: alist.append(blist); alist
Out[55]: [1, 2, 3, [4, 5]]
+= is a list join: += 是一个列表连接:
In [57]: alist += blist; alist
Out[57]: [1, 2, 3, 4, 5]
and is the equivalent of extend
:并且相当于
extend
:
In [59]: alist.extend(blist); alist
Out[59]: [1, 2, 3, 4, 5]
The non-in-place version of extend:扩展的非就地版本:
In [61]: alist + blist
Out[61]: [1, 2, 3, 4, 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.