如何格式化原始音频文件以导入大胆?
我试图在JavaScript中生成PCM(脉冲代码调制)代码,以将其导入Audacity作为原始数据。运行代码后,我将显示的文本复制并粘贴到将其保存为.txt文件文件自动在我的代码中,我会感谢它)。我尝试使用标准的440 Hz正弦波测试,但是当我用,我刚刚听到铃声(大于440 Hz)和噪音。
let list = [] // list of plot points
let t = 0 // theta
let sampleRate = 48000 // sampling rate in Hz
let bitDepth = 24 // bit depth in bits
let length = 1 // length of sound in seconds
let binary = "" // plot points converted to binary in this string
let binaryList = [] // list of plot points in binary
let byteList = [] // list of binary values separated into bytes
for (i = 0; i < sampleRate * length; i++) {
t = t + 2 * Math.PI / sampleRate
list.push(Math.sin(440 * t))
}
// generates plot points (440 Hz sine wave in this case)
for (i = 0; i < list.length; i++) {
list.splice(i, 1, Math.round(2 ** (bitDepth - 1) * list[i]) + 2 ** (bitDepth - 1))
}
let j = bitDepth
for (i = 0; i < list.length; i++) {
j = bitDepth
binary = ""
while (list[i] > 0) {
while (2 ** j > list[i]) {
j = j - 1
binary = "0" + binary
}
list.splice(i, 1, list[i] - 2 ** j)
binary = "1" + binary.substr(1, binary.length - 1)
}
binaryList.push(binary)
}
// converts plot points to binary
for (i = 0; i < binaryList.length; i++) {
while (binaryList[i].length < bitDepth) {
binaryList.splice(i, 1, "0" + binaryList[i])
}
}
// puts 0's in front of each binary value to define all place values (e.g. 101 in 8 bit would become 00000101)
for (i = 0; i < binaryList.length; i++) {
for (j = 0; j < bitDepth / 8; j++) {
byteList.push(binaryList[i].substr(8 * j, 8))
}
}
// separates binary values into bytes
let text1 = String(byteList)
let text2 = ""
for (i = 0; i < text1.length; i++) {
if (text1.substr(i, 1) == ",") {
text2 = text2 + " "
} else {
text2 = text2 + text1.substr(i, 1)
}
}
// replaces commas list with spaces in list
document.getElementById("demo").innerHTML = text2
<p id="demo"></p>
我的代码中有错误吗?数据格式不正确?错误的文件格式?大胆的设置错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论