2011-02-10 24 views
2

私のアンドロイドアプリでマイクを使用して録音しましたが、これはデータをストリーミングしたAudioPlayerクラスを使用すると完全にうまくいきます。私の問題はこのデータにwavヘッダーを追加してアプリケーション外で再生できるようにすることです。私は確かに、wcmファイルの生データとして有用ではないと記録されたpcmデータにつながる、他のオーディオファイルとの16進エディタで再生した後にヘッダーを作成する方法を確かめていますか?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

あなたはPROBですヘッダーのプロパティを正しく設定しないでください。 WAV形式のヘッダーは44バイト、生のオーディオデータが続きます。ここではWAV形式の説明である:

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

あなたはヘッダを作成し、生データを付加し、その結果ファイルがエラーなしで演じているが、ノイズのように聞こえてきた場合は、最も可能性の高い犯人ということです生のオーディオは1サンプルにつき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

を書き込むためrehearshalアシスタントのコードを見てみましょう、私は記録するために使用しているコードとの質問を編集していると、ヘッダーを作成します。 – SamRowley

+0

@ Sam:ヘッダーに「long」というタイプのものは絶対に置いてはいけません。 WAVヘッダーのプロパティは、すべて4バイト整数または2バイト整数のいずれかです。より多くのコードを含めると、実際にヘッダーとデータを結果ファイルに書き込む方法がわかります。 – MusiGenesis

+0

@MusiGenesis - ヘッダーの作成に使用しているコードをすべて追加しました。長いデータ型について理解しています。そのため、そこにConvertorメソッドがあります。このファイルは、再生時に静的でノイズの多い.wavファイルとして認識されます。 – SamRowley

0

私は右のヘッダーを設定していかなり確信しているwavファイルのヘッダhere

関連する問題