- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有一种简单的方法可以记录几秒钟的声音并将其转换为频率?我有一个 USB 麦克风和一个树莓派 2 B。
在发布的文件 (convert2note.py) 中,我想知道如何使 f 等于从麦克风获得的频率。 This is what the program looks like so far
#d=69+12*log(2)*(f/440)
#d is midi, f is frequency
import math
f=raw_input("Type the frequency to be converted to midi: ")
d=69+(12*math.log(float(f)/440))/(math.log(2))
d=round(int(d))
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
print notes[d % len(notes)]
提前致谢:D
最佳答案
要捕获音频,例如,您可以使用 sox
程序。有关详细信息,请参阅链接的文档,但它可以像这样简单:
rec input.wav
但是下面是用来让文件匹配下面代码期望的格式;
rec −c 2 −b 16 −e signed-integer -r 44100 input.wav
(从技术上讲,只有 -c
、-b
和 -e
选项才能匹配下面的代码。您可以减少示例rate -r
以加快处理速度)
对于用 Python 处理音频,最好将其保存在 wav
文件中,因为 Python 有一个模块可以读取标准库中的音频。
为了将音频转换为频率,我们将使用 discrete Fourier transform以 Numpy 的形式 fast Fourier transform for real input .请参阅下面的代码片段,其中我还使用 matplotlib 来绘制图表。
下面的代码假定一个 2 声道(立体声)16 位 WAV 文件。
from __future__ import print_function, division
import wave
import numpy as np
import matplotlib.pyplot as plt
wr = wave.open('input.wav', 'r')
sz = wr.getframerate()
q = 5 # time window to analyze in seconds
c = 12 # number of time windows to process
sf = 1.5 # signal scale factor
for num in range(c):
print('Processing from {} to {} s'.format(num*q, (num+1)*q))
avgf = np.zeros(int(sz/2+1))
snd = np.array([])
# The sound signal for q seconds is concatenated. The fft over that
# period is averaged to average out noise.
for j in range(q):
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
left, right = da[0::2]*sf, da[1::2]*sf
lf, rf = abs(np.fft.rfft(left)), abs(np.fft.rfft(right))
snd = np.concatenate((snd, (left+right)/2))
avgf += (lf+rf)/2
avgf /= q
# Plot both the signal and frequencies.
plt.figure(1)
a = plt.subplot(211) # signal
r = 2**16/2
a.set_ylim([-r, r])
a.set_xlabel('time [s]')
a.set_ylabel('signal [-]')
x = np.arange(44100*q)/44100
plt.plot(x, snd)
b = plt.subplot(212) # frequencies
b.set_xscale('log')
b.set_xlabel('frequency [Hz]')
b.set_ylabel('|amplitude|')
plt.plot(abs(avgf))
plt.savefig('simple{:02d}.png'.format(num))
plt.clf()
avgf
数组现在保存左右频率的平均值。情节看起来像这样;
如您所见,声音信号通常包含许多频率。
关于python - 从 Raspberry Pi 上的麦克风读取频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250228/
我有一个运行 Raspbian 的 Raspberry Pi 1。我尝试在 Raspberry Pi 3 上运行 SD 卡,但它没有启动。 我已经阅读了有关升级 Raspberry Pi 2 安装以在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我目前正在尝试RadiusNetworks发布的Raspberry Pi iBeacon教程,网址为 http://developer.radiusnetworks.com/2013/10/09/ho
我的公司使用 Raspberry Pi 3 作为产品中的嵌入式 Controller 。用户不会优雅地关闭它,他们只是扳动一个开关。为避免损坏,/boot 和/root 文件系统是只读的。这似乎是防弹
如何使用 Raspberry Pi 作为 b/w USB Tethered 手机和路由器的桥接器,使用“以太网电缆 b/w Raspberry Pi 和路由器”和“USB 电缆 b/w 手机和 Ras
我关注了一个名为Creating an Electron Application for the Raspberry Pi的博客,内容涉及使用Buster OS在Raspberry Pi中启动Elec
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有一个树莓派,并且已经从 raspbmc.com 加载了最新的独立版本。在使用 XBMC 时,我看到 CPU 使用率始终在 90% 以上。查看 XBMC wiki 和常见问题解答后,脏区域似乎是减少
我现在正在做一个小项目。我希望 python 脚本在登录到 GUI 后自动运行。 我按照这里的步骤操作:https://www.raspberrypi.org/forums/view ... 91&t
我正在使用 Android Things 在 Raspberry Pi 上构建应用程序并且我有 7 inch touch screen ,但屏幕永远不会关闭。 是否可以像 Android 手机一样设置
我正在执行一组事件以确保 Redis 在一组嵌入式系统(包括 Raspberry PI)中运行良好。为了修复执行未对齐内存访问的 Redis 的某些代码路径(由于 Redis 3.2 中引入的更改),
我正在尝试使用 Tanuki Java Service Wrapper。 我使用的硬件是带有 Raspbian wheezy 发行版的 Raspberry Pi。 (见 http://www.rasp
我希望构建一个以全屏模式在 Raspberry Pi 上运行的应用程序。我已经尝试过 JavaFX 和基于 Swing 的应用程序,但性能非常糟糕。 在我开始使用 SDL( http://www.li
我的项目在/home/pi/app中 以npm start开头 启动操作系统后如何启动应用程序? ****西类牙文 Mi proyecto esta zh/home/pi/app Arranca la
我正在尝试安装 Kappelt gBridge在 Raspberry Pi 3 B 型上,使用本指南:https://doc.gbridge.io/selfHosted/hostItYourself.
我正在使用我的 Pi 作为文件服务器,最近当我登录时,我看到一条错误消息,指出 libarmmem.so(无法打开共享对象文件),尽管有一些建议运行 apt-get update + 升级它并没有带来
我正在尝试使用 Raspberry# 库通过 Raspberry PI 上的 GPIO 引脚(打开和关闭)执行基本任务。根据 github 上的示例:https://github.com/raspbe
如标题所述,我在将一些用户空间中断代码从另一个 armv7 嵌入式 linux 平台移植到 Raspberry Pi 2 Model B 时遇到问题。 我知道 wiringPi 库(并让它以这种方式工
我正在尝试为 Raspberry Pi B+ 交叉编译 Tensorflow-Lite。为此,我正在关注 these instructions来自官方网站,它们是: git clone https:/
我正在尝试使用 PulseAudio RTP 将音频从 Linux Mint 桌面流式传输到运行 LibreELEC (Kodi) 的 RaspberryPi 3B。我可以使用 RTP 多播成功地流式
我是一名优秀的程序员,十分优秀!