如何格式化原始音频文件以导入大胆?

发布于 2025-02-12 10:01:24 字数 2381 浏览 0 评论 0 原文

我试图在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>

我的代码中有错误吗?数据格式不正确?错误的文件格式?大胆的设置错误?

I am trying to generate PCM (pulse-code modulation) code in JavaScript to be imported into Audacity as raw data. After running my code, I copied and pasted the displayed text into https://www.oflox.com/blog/txt-file-generator-online to save it as a .txt file (if there is a way to create the file automatically in my code I would appreciate it). I tried testing this with a standard 440 Hz sine wave, but when I imported the file into Audacity with these settings, I just heard ringing (much higher than 440 Hz) and noise.

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>

Is there an error in my code? Data formatted incorrectly? Wrong file format? Wrong settings in Audacity?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文