2011-02-10 73 views
2

我在我的android应用程序中使用了麦克风录制,并且在播放数据时使用AudioPlayer类时播放效果非常好。我的问题是我想为此数据添加一个wav标题,以便可以在应用程序之外播放。我非常肯定在用其他音频文件在十六进制编辑器中播放后创建标题工作的方法,这会导致记录的pcm数据在wav文件中不能用作原始数据?AudioRecord PCM数据没用?

任何人都可以解释这一点吗?我可以将pcm/wav文件作为原始文件导入大胆文件,并且可以完美播放,但是当我尝试打开wav时,我只会听到噪音,再次暗示pcm数据有问题。

录音设置:

int frequency = 22050; 
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO; 
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 

头变量:

byte[] clipData = data; 
long myDataSize = clipData.length; 
long mySubChunk1Size = 16; 
int myBitsPerSample= 16; 
int myFormat = 1; 
long myChannels = 1; 
long mySampleRate = 22050; 
long myByteRate = mySampleRate * myChannels * myBitsPerSample/8; 
int myBlockAlign = (int) (myChannels * myBitsPerSample/8); 
long myChunk2Size = myDataSize * myChannels * myBitsPerSample/8; 
long myChunkSize = 36 + myChunk2Size; 

try 
     { 
      File audioDirectory = new File(Environment 
        .getExternalStorageDirectory().getAbsolutePath() 
        + "/Directory/"); 
      audioDirectory.mkdir(); 
      File file = new File(audioDirectory, "test.wav"); 
      if (file.exists()) 
       file.delete(); 

      // Create the new file. 
      try { 
       file.createNewFile(); 
      } catch (IOException e) { 
       throw new IllegalStateException("Failed to create " 
         + file.toString()); 
      } 
      OutputStream os = new FileOutputStream(file); 
      BufferedOutputStream bos = new BufferedOutputStream(os); 
      DataOutputStream outFile = new DataOutputStream(bos); 

      // write the wav file per the wav file format 
      outFile.writeBytes("RIFF");     // 00 - RIFF 
      outFile.write(intToByteArray((int)myChunkSize), 0, 4);  // 04 - how big is the rest of this file? 
      outFile.writeBytes("WAVE");     // 08 - WAVE 
      outFile.writeBytes("fmt ");     // 12 - fmt 
      outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4); // 16 - size of this chunk 
      outFile.write(shortToByteArray((short)myFormat), 0, 2);  // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation 
      outFile.write(shortToByteArray((short)myChannels), 0, 2); // 22 - mono or stereo? 1 or 2? (or 5 or ???) 
      outFile.write(intToByteArray((int)mySampleRate), 0, 4);  // 24 - samples per second (numbers per second) 
      outFile.write(intToByteArray((int)myByteRate), 0, 4);  // 28 - bytes per second 
      outFile.write(shortToByteArray((short)myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels 
      outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2); // 34 - how many bits in a sample(number)? usually 16 or 24 
      outFile.writeBytes("data");     // 36 - data 
      outFile.write(intToByteArray((int)myDataSize), 0, 4);  // 40 - how big is this data chunk 
      outFile.write(clipData);      // 44 - the actual data itself - just a long string of numbers 
     } 

转换器

public static int byteArrayToInt(byte[] b) 
    { 
     int start = 0; 
     int low = b[start] & 0xff; 
     int high = b[start+1] & 0xff; 
     return (int)(high << 8 | low); 
    } 


    // these two routines convert a byte array to an unsigned integer 
    public static long byteArrayToLong(byte[] b) 
    { 
     int start = 0; 
     int i = 0; 
     int len = 4; 
     int cnt = 0; 
     byte[] tmp = new byte[len]; 
     for (i = start; i < (start + len); i++) 
     { 
      tmp[cnt] = b[i]; 
      cnt++; 
     } 
     long accum = 0; 
     i = 0; 
     for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) 
     { 
      accum |= ((long)(tmp[i] & 0xff)) << shiftBy; 
      i++; 
     } 
     return accum; 
    } 


// =========================== 
// CONVERT JAVA TYPES TO BYTES 
// =========================== 
    // returns a byte array of length 4 
    private static byte[] intToByteArray(int i) 
    { 
     byte[] b = new byte[4]; 
     b[0] = (byte) (i & 0x00FF); 
     b[1] = (byte) ((i >> 8) & 0x000000FF); 
     b[2] = (byte) ((i >> 16) & 0x000000FF); 
     b[3] = (byte) ((i >> 24) & 0x000000FF); 
     return b; 
    } 

    // convert a short to a byte array 
    public static byte[] shortToByteArray(short data) 
    { 
     return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)}; 
    } 

回答

1

你的概率巧妙地设置标题属性错误。 WAV格式标题应该是44个字节,然后是原始音频数据。这里是WAV格式的描述:

http://www.sonicspot.com/guide/wavefiles.html

如果您已经创建了一个标题和附加的原始数据,并将得到的文件播放没有错误,但听起来像噪音,那么最有可能的罪魁祸首是原始音频使用每个样本2字节,但您将标头中的BitsPerSample属性设置为8.

您正在使用的方法(将WAV标头预先添加到原始音频)是完全有效的,应该可以正常工作。

更新:嘿,不应该转换方法是

// convert a short to a byte array 
    public static byte[] shortToByteArray(short data) 
    { 
     return new byte[]{(byte)(data & 0xff),(byte)((data >> 8) & 0xff)}; 
    } 

?我不确定什么>>>在变化的世界中意味着什么。

+0

我敢肯定,我设置了头吧,我已经编辑与我使用的记录和代码的问题然后创建标题。 – SamRowley 2011-02-10 17:11:36

0

看看在rehearshal助理代码编写的WAV头here