0

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?

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • 2
    It seems that saving what should be a binary PCM audio file as a text file may be the wrong thing to do! – phuzi Jul 02 '22 at 20:59
  • Is `download.txt` file a plain/text file with floats delimited by whitespace? – zer00ne Jul 03 '22 at 01:04
  • @phuzi How do I save the binary code as a PCM file? – coder_noob Jul 04 '22 at 22:52
  • Take a look at this https://stackoverflow.com/questions/71439207/download-pdf-from-javascript-blob-without-replacing-the-open-page which should be enough to trigger an automated download from the sample data. – phuzi Jul 05 '22 at 08:50

0 Answers0