2011-02-10 5 views
2

내 안드로이드 응용 프로그램에서 Mic를 사용하여 녹음했습니다. 데이터를 스트리밍하는 AudioPlayer 클래스를 사용할 때 완벽하게 잘 재생됩니다. 내 문제는이 데이터에 wav 헤더를 추가하여 응용 프로그램 외부에서 재생할 수 있도록하려는 것입니다. . 다른 오디오 파일과 함께 16 진수 편집기에서 재생 한 후 헤더 작업을 만드는 방법이 확실합니다. 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

당신은 PROB있어 헤더 속성을 잘못 설정하는 것만으로도 충분합니다. WAV 형식 헤더는 44 바이트이어야하며 그 다음에 원시 오디오 데이터가옵니다.

당신이 헤더를 생성하고 원시 데이터를 추가하고, 결과 파일이 오류없이 재생되지만 잡음 같은 소리 한 경우

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

후 가장 가능성이 범인이다 : 여기에 WAV 형식의 설명입니다 원시 오디오는 샘플 당 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

나는 내가 헤더 권리를 설정하고있어 확신 해요 WAV 헤더를 작성하기위한 rehearshal 보조 코드를 살펴, 나는 내가 기록하고 사용하고 코드로 질문을 편집 한 그런 다음 헤더를 만듭니다. – SamRowley

+0

@Sam : 헤더에 'long'유형이 없어야합니다. WAV 헤더의 속성은 모두 4 바이트 정수 또는 2 바이트 정수입니다. 더 많은 코드를 포함시키고 결과 파일에 실제로 헤더와 데이터를 쓰는 방법을 알려주십시오. – MusiGenesis

+0

@MusiGenesis - 머리글을 만드는 데 사용하는 코드를 모두 추가했습니다. 예를 들어 긴 데이터 형식에 대해 이해하고 있습니다. 그 이유는 여기에 Convertor 메서드가있는 이유입니다. 파일은 재생시 정적 및 잡음으로 가득 찬 .wav 파일로 인식됩니다. – SamRowley

관련 문제