生成具有 15Khz 音调的声音文件

发布于 2024-12-21 20:31:22 字数 1435 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气尐潴 2024-12-28 20:31:22

您可以使用 Python 的 wave 模块创建一个波形文件,然后将其压缩为 MP3。要创建一秒 15khz 正弦波:

import math
import wave
import struct

nchannels = 1
sampwidth = 2
framerate = 44100
nframes = 44100
comptype = "NONE"
compname = "not compressed"
amplitude = 4000
frequency = 15000

wav_file = wave.open('15khz_sine.wav', 'w')
wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
for i in xrange(nframes):
    sample = math.sin(2*math.pi*frequency*(float(i)/framerate))*amplitude/2
    wav_file.writeframes(struct.pack('h', sample))
wav_file.close()

You could use Python's wave module to create a wave file which you could then compress to MP3. To create a one second 15khz sine wave:

import math
import wave
import struct

nchannels = 1
sampwidth = 2
framerate = 44100
nframes = 44100
comptype = "NONE"
compname = "not compressed"
amplitude = 4000
frequency = 15000

wav_file = wave.open('15khz_sine.wav', 'w')
wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
for i in xrange(nframes):
    sample = math.sin(2*math.pi*frequency*(float(i)/framerate))*amplitude/2
    wav_file.writeframes(struct.pack('h', sample))
wav_file.close()
月下伊人醉 2024-12-28 20:31:22

我会将其分为两部分:

  1. 使用 C++ 库创建一个波形文件(例如 libsndfile 库)
  2. 使用实用程序将波形文件转换为 mp3(例如 蹩脚)。这是一个命令行工具,也可以从 C 程序中调用。请参阅 -t 将 Wave 转换为 mp3。

需要注意的一件事是 15KHz 是人类听到的非常高的频率,我猜大多数扬声器都无法播放它,因为它超出了它们的截止频率。因此,如果您没有听到结果,请不要感到惊讶。

I would break this into 2 pieces:

  1. Create a wave file using a C++ library(like libsndfile library)
  2. Convert the wave file to mp3 using a utility (like lame). This is a command line tool which can be called from your C program as well. see -t for converting wave to mp3.

One thing to note is 15KHz is very high frequency to be heard by human and I guess most of speakers are not capable of playing it as it is beyond cutoff frequency of them. So don't be surprised if you don't hear the result.

日记撕了你也走了 2024-12-28 20:31:22

你有没有尝试过:

#include<dos.h>
#include<iostream.h>
#include<conio.h>
main()
{
    sound(500); // Frequency
    delay(1000); // Time
    nosound(); // Stop
}

Have you tried:

#include<dos.h>
#include<iostream.h>
#include<conio.h>
main()
{
    sound(500); // Frequency
    delay(1000); // Time
    nosound(); // Stop
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文