Beispiel #1
0
 /*
  * Writes WAV header into new file.
  */
 private void writeWavHeader() {
   try {
     randomAccessWriter = new RandomAccessFile(filePath, "rw");
     randomAccessWriter.setLength(0);
     randomAccessWriter.writeBytes("RIFF");
     randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
     randomAccessWriter.writeBytes("WAVE");
     randomAccessWriter.writeBytes("fmt ");
     randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
     randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
     randomAccessWriter.writeShort(
         Short.reverseBytes((short) 1)); // Number of channels, 1 for mono, 2 for stereo
     randomAccessWriter.writeInt(Integer.reverseBytes(RECORDER_SAMPLE_RATE)); // Sample rate
     randomAccessWriter.writeInt(
         Integer.reverseBytes(
             RECORDER_SAMPLE_RATE
                 * 16
                 * 1
                 / 8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
     randomAccessWriter.writeShort(
         Short.reverseBytes(
             (short) (1 * 16 / 8))); // Block align, NumberOfChannels*BitsPerSample/8
     randomAccessWriter.writeShort(Short.reverseBytes((short) 16)); // Bits per sample
     randomAccessWriter.writeBytes("data");
     randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0
   } catch (Exception e) {
     Log.e("Microphone", "Error writing WAV header.");
   }
 }
  /**
   * Prepares the recorder for recording, in case the recorder is not in the INITIALIZING state and
   * the file path was not set the recorder is set to the ERROR state, which makes a reconstruction
   * necessary. In case uncompressed recording is toggled, the header of the wave file is written.
   * In case of an exception, the state is changed to ERROR
   */
  public void prepare() {
    try {
      if (state == State.INITIALIZING) {
        if (rUncompressed) {
          if ((audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) & (filePath != null)) {
            // write file header

            randomAccessWriter = new RandomAccessFile(filePath, "rw");

            randomAccessWriter.setLength(
                0); // Set file length to 0, to prevent unexpected behavior in case the file already
                    // existed
            randomAccessWriter.writeBytes("RIFF");
            randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
            randomAccessWriter.writeBytes("WAVE");
            randomAccessWriter.writeBytes("fmt ");
            randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
            randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
            randomAccessWriter.writeShort(
                Short.reverseBytes(nChannels)); // Number of channels, 1 for mono, 2 for stereo
            randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
            randomAccessWriter.writeInt(
                Integer.reverseBytes(
                    sRate * bSamples * nChannels
                        / 8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(
                Short.reverseBytes(
                    (short)
                        (nChannels * bSamples
                            / 8))); // Block align, NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(Short.reverseBytes(bSamples)); // Bits per sample
            randomAccessWriter.writeBytes("data");
            randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0

            buffer = new byte[framePeriod * bSamples / 8 * nChannels];
            state = State.READY;
          } else {
            Log.e(
                ExtAudioRecorder.class.getName(),
                "prepare() method called on uninitialized recorder");
            state = State.ERROR;
          }
        } else {
          mediaRecorder.prepare();
          state = State.READY;
        }
      } else {
        Log.e(ExtAudioRecorder.class.getName(), "prepare() method called on illegal state");
        release();
        state = State.ERROR;
      }
    } catch (Exception e) {
      if (e.getMessage() != null) {
        Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
      } else {
        Log.e(ExtAudioRecorder.class.getName(), "Unknown error occured in prepare()");
      }
      state = State.ERROR;
    }
  }
  /**
   * Writes the Item UM to the Items Random Access File
   *
   * @param file - the Items Random Access File
   */
  private void writeUMToFile(RandomAccessFile file) {
    try {
      file.writeShort(getUm().getCode());
    } catch (Exception e) {

    }
  }
  /**
   * Writes the Item's creation date to the Items Random Access File
   *
   * @param file - the Item's Random Access File
   */
  private void writeCreationDate(RandomAccessFile file) {
    try {
      file.writeShort(getCreationDate().get(Calendar.YEAR));
      file.writeByte(getCreationDate().get(Calendar.MONTH));
      file.writeByte(getCreationDate().get(Calendar.DAY_OF_MONTH));
    } catch (Exception e) {

    }
  }
Beispiel #5
0
 /** Write NumBytes data. */
 public int Write(short Data, int NumBytes) {
   short theData = (short) (((Data >>> 8) & 0x00FF) | ((Data << 8) & 0xFF00));
   if (fmode != RFM_WRITE) {
     return DDC_INVALID_CALL;
   }
   try {
     file.writeShort(theData);
     fmode = RFM_WRITE;
   } catch (IOException ioe) {
     return DDC_FILE_ERROR;
   }
   riff_header.ckSize += NumBytes;
   return DDC_SUCCESS;
 }
Beispiel #6
0
 /**
  * This writes the binary short to the file. Later, use readBinaryShort to read the value from the
  * file.
  */
 public void writeBinaryShort(int col, int row, short s) throws IOException {
   raf.seek(row * nBytesPerRow + columnStartAt[col]);
   raf.writeShort(s);
 }
 /**
  * Prepares the recorder for recording, in case the recorder is not in the INITIALIZING state and
  * the file path was not set the recorder is set to the ERROR state, which makes a reconstruction
  * necessary. In case uncompressed recording is toggled, the header of the wave file is written.
  * In case of an exception, the state is changed to ERROR
  */
 public void prepare() {
   try {
     if (state == State.INITIALIZING) {
       if ((audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) & (filePath != null)) {
         // write file header
         randomAccessWriter = new RandomAccessFile(filePath, "rw");
         randomAccessWriter.setLength(0); // Set file length to 0, to
         // prevent unexpected
         // behavior in case the
         // file already existed
         randomAccessWriter.writeBytes("RIFF");
         randomAccessWriter.writeInt(0); // Final file size not known
         // yet, write 0
         randomAccessWriter.writeBytes("WAVE");
         randomAccessWriter.writeBytes("fmt ");
         randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk
         // size,
         // 16
         // for
         // PCM
         randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat,
         // 1 for
         // PCM
         randomAccessWriter.writeShort(Short.reverseBytes(nChannels)); // Number
         // of
         // channels,
         // 1 for
         // mono,
         // 2 for
         // stereo
         randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample
         // rate
         randomAccessWriter.writeInt(
             Integer.reverseBytes(sRate * nChannels * mBitsPersample / 8)); // Byte rate,
         // SampleRate*NumberOfChannels*mBitsPersample/8
         randomAccessWriter.writeShort(
             Short.reverseBytes((short) (nChannels * mBitsPersample / 8))); // Block align,
         // NumberOfChannels*mBitsPersample/8
         randomAccessWriter.writeShort(Short.reverseBytes(mBitsPersample)); // Bits per sample
         randomAccessWriter.writeBytes("data");
         randomAccessWriter.writeInt(0); // Data chunk size not known
         // yet, write 0
         buffer = new byte[mPeriodInFrames * mBitsPersample / 8 * nChannels];
         state = State.READY;
       } else {
         Log.e(
             WavAudioRecorder.class.getName(),
             "prepare() method called on uninitialized recorder");
         state = State.ERROR;
       }
     } else {
       Log.e(WavAudioRecorder.class.getName(), "prepare() method called on illegal state");
       release();
       state = State.ERROR;
     }
   } catch (Exception e) {
     if (e.getMessage() != null) {
       Log.e(WavAudioRecorder.class.getName(), e.getMessage());
     } else {
       Log.e(WavAudioRecorder.class.getName(), "Unknown error occured in prepare()");
     }
     state = State.ERROR;
   }
 }
 /* @see java.io.DataOutput.writeShort(int) */
 public void writeShort(int v) throws IOException {
   raf.writeShort(v);
 }