1

I have a binary data file (containing audio data) with the extension .w.

I opened a file connection using the following code in R;

file1 <- file("filename.w", "rb") # opening a file connection to .w binary file and specifying 'rb', read binary

file2 <- readBin(file1, double(), size = 3, endian = "big") # it's in double vector mode.

This gives me the error

Error in readBin(file1, double(), size = 3, endian = "big") : size 3 is unknown on this machine"

I chose size 3 as a I am interested in reading with a bit depth of 24. I used the conversion 1 byte = 8 bit.

The help files readBin says the variable takes only 1,2,4,8 bytes per element. Does anyone how I can specify 3 bytes? Or could someone help an alternate way to read this binary file into R (specifying the 24 bit).

I tried the file with a different vector mode of integer() instead of double() but am getting the same error.

My goal is to read this binary file, extract certain parts of it and then write it out as .wav file.

springsun
  • 11
  • 1

1 Answers1

3

One way to address this problem is to read two bytes first, then add on the third byte:

read3bytes <- function(file1) {
    first2 <- readBin(file1, "int", size = 2)
    third <- readBin(file1, "int", size = 1)

    # Shift the first two bytes one byte to the left, then add the third byte on
    bitwShiftL(first2, 8) + third
}
C. Braun
  • 5,061
  • 19
  • 47