我想将mp3和wav音频文件作为浮点数或双精度数组加载,类似于scipy中的io.wavfile.read函数.我可以通过将音频流写入缓冲区来使用麦克风数据或播放音频.但是,我不确定如何一次加载所有音频文件的数据.
- 更新
对于将来使用音频信号数据的任何人来说,这是一个可以解决问题的功能.它基于Rhythmic Fistman的回答.
func loadAudioSignal(audioURL: NSURL) -> (signal: [Float], rate: Double, frameCount: Int) {
let file = try! AVAudioFile(forReading: audioURL)
let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length))
try! file.readIntoBuffer(buf) // You probably want better error handling
let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))
return (signal: floatArray, rate: file.fileFormat.sampleRate, frameCount: Int(file.length))
}
Run Code Online (Sandbox Code Playgroud) 我想使用Cython将C函数导入到IPython笔记本中.目前,我正在尝试在Cython文档中复制该示例,但是我收到了编译错误.
我的Python代码(来自iPython笔记本):
import cython
%load_ext Cython
Run Code Online (Sandbox Code Playgroud)
----------------------------------新细胞
%%cython
cdef extern from "spam.c":
void order_spam(int tons)
Run Code Online (Sandbox Code Playgroud)
我的C代码:
// spam.c
#include <stdio.h>
static void order_spam(int tons)
{
printf("Ordered %i tons of spam!\n", tons);
}
Run Code Online (Sandbox Code Playgroud)
运行此代码,我得到以下回溯和错误消息:
CompileError Traceback (most recent call last)
<ipython-input-13-8bb733557977> in <module>()
----> 1 get_ipython().run_cell_magic(u'cython', u'', u'\ncdef extern from "spam.c":\n void order_spam(int tons)')
/Users/danielacker/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2118 magic_arg_s = self.var_expand(line, stack_depth)
2119 with self.builtin_trap:
-> 2120 result = fn(magic_arg_s, cell)
2121 return result
2122
<decorator-gen-126> …Run Code Online (Sandbox Code Playgroud)