4

当我录制手机的麦克风时,我Mediarecorder给了我PCM File一个输出。现在,当尝试收听File它时,我听到的所有声音都是静态的,我想,如果我理解正确,我会从Mediarecorder非 AAC 获得一个 PCM 文件,我需要在其中添加ADTS标题PCM才能收听它。

我见过自定义线程,Encoders但我似乎无法弄清楚我需要在哪里以及需要对它们做什么。

output File像这样从麦克风录制:

private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int SAMPLE_RATE = 44100; //44.1kHz
private static final int BUFFER_SIZE = 2048;

public Status status = Status.IDLE;
private AudioRecordingHandler arh;
private File outputFile;
private Context context;


/**
 * Starts script for running. Needs output file to work!
 */
public void start() {
    if (outputFile == null) { return; }
    System.out.println("Start reading stream...");
    aacEncoder = new AACEncoder(SAMPLE_RATE, micOutputPCM);
    new Thread(new Runnable() {
        @Override
        public void run() {
            record.startRecording();
            byte[] data = new byte[BUFFER_SIZE];
            float[] audioFloatBuffer = new float[BUFFER_SIZE/2];
            Yin y = new Yin(SAMPLE_RATE, BUFFER_SIZE/2);

            while(status == Status.RECORDING) {
                record.read(data, 0, BUFFER_SIZE);
                audioFloatBuffer = ConverterUtil.toFloatArray(data, 0, audioFloatBuffer,
                        0, audioFloatBuffer.length);
                PitchDetectionResult pdr = y.getPitch(audioFloatBuffer);
                aacEncoder.writeIntoOutputfile(data);
                arh.handlePitch(pdr.getPitch());
            }
            aacEncoder.stopEncoding();
        }
    }).start();
}

/**
 * Stops script
 */
public void stop() {
    status = Status.IDLE;
    record.stop();
    arh.finishedRecording(micOutputPCM);
}

以下是我如何从中获取byte[]以及File我尝试将 ADTS 标头编码到它们的位置。

 public static File addHeaderToAac(File micOutputPCM, File output) throws IOException {

    byte[] pcmFile = fullyReadFileToBytes(micOutputPCM);
    int bufferSize = 2048;

    //addADTSHeader to byte[] and return a File object


    return fileWithADTSHeader;
}


public static byte[] fullyReadFileToBytes(File f) throws IOException {
    int size = (int) f.length();
    byte bytes[] = new byte[size];
    byte tmpBuff[] = new byte[size];
    FileInputStream fis= new FileInputStream(f);;
    try {

        int read = fis.read(bytes, 0, size);
        if (read < size) {
            int remain = size - read;
            while (remain > 0) {
                read = fis.read(tmpBuff, 0, remain);
                System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                remain -= read;
            }
        }
    }  catch (IOException e){
        throw e;
    } finally {
        fis.close();
    }

    return bytes;
}

我的问题是,有没有人Encoder可以接受 aFile or byte[] or ByteStream作为输入并返回 a File

因为最终我想制作一个mp4parser AACTrackImpl,可以在这里找到: https ://github.com/sannies/mp4parser

AACTrackImpl aacTrack2 = new MP3TrackImpl(new FileDataSourceImpl(micOutputPCM));

此外,如果我遗漏了一些关于如何转换以及我应该怎么做才能播放它的重要细节,那么这些信息也会很有用。

如果我需要提供更多信息来回答这个问题,那么我很乐意这样做。

编辑:

我一直在尝试制作一个可以满足我需要的编码器,但到目前为止我还没有成功。

    public static File addHeaderToAac(File pcmFile1, File output, Context context) throws IOException {

    byte[] pcmFile = fullyReadFileToBytes(pcmFile1);
    int bufferSize = 2048;


    AACEncoder encoder = new AACEncoder(44100, output);
    encoder.encodeAudioFrameToAAC(pcmFile);

    return output;
}

我正在尝试PCM to AAC使用此编码器进行编码,但这encoder会将输出文件写入内存,但我需要一个对象。当我给它时,byte[]它也会给我一个错误:

W/System.err:     at java.nio.ByteBuffer.put(ByteBuffer.java:642)

错误来自这一行:

inputBuf.put(frameData);

最后,我的编码器:

public class AACEncoder {
    final String TAG = "UEncoder Processor";

    final int sampleRate;
    File outputFile;

    FileOutputStream fos;

    final int TIMEOUT_USEC = 10000 ;

    MediaCodec encoder;
    boolean isEncoderRunning = false;
    boolean outputDone = false;

    MediaCodec.BufferInfo info;

    public AACEncoder(final int sampleRate, File outputFile) {
        this.sampleRate = sampleRate;
        this.info = new MediaCodec.BufferInfo();
        this.outputFile = outputFile;

        openFileStream();
        initEncoder();
    }

    /**
     * Initializes CrappyEncoder for AAC-LC (Low complexity)
     * @throws Exception
     */
    public void initEncoder() {
        try {
            encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
            MediaFormat format = new MediaFormat();
            format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
            format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
            format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
            format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
            format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
            encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        } catch (IOException ex) {
            Log.e(TAG, "Failed to create CrappyEncoder");
            ex.printStackTrace();
        }
    }

    int generateIndex = 0;

    public void encodeAudioFrameToAAC(byte[] frameData) {

        if (encoder == null) return;

        if (!isEncoderRunning) {
            encoder.start();
            isEncoderRunning = true;
        }

        ByteBuffer[] encoderInputBuffers = encoder.getInputBuffers();
        if (fos != null) {
            int inputBufIndex = encoder.dequeueInputBuffer(TIMEOUT_USEC);

            if (inputBufIndex >= 0) {
                long ptsUsec = (System.currentTimeMillis() * 1000) / 10000;
                if (outputDone) {
                    encoder.queueInputBuffer(inputBufIndex, 0, 0, ptsUsec,
                            MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                } else {
                    ByteBuffer inputBuf = encoderInputBuffers[inputBufIndex];
                    inputBuf.clear();
                    inputBuf.put(frameData);
                    encoder.queueInputBuffer(inputBufIndex, 0, frameData.length, ptsUsec, 0);
                }
                generateIndex++;
            }
            tryEncodeOutputBuffer();
        }
        checkIfOutputDone();
    }

    /**
     * Gets data from output buffer and encodes it to
     * AAC-LC encoding with ADTS header attached before every frame
     */
    private void tryEncodeOutputBuffer() {

        ByteBuffer[] encoderOutputBuffers = encoder.getOutputBuffers();
        //If >= 0 then valid response
        int encoderStatus = encoder.dequeueOutputBuffer(info, TIMEOUT_USEC);
        if (encoderStatus >= 0) {
            ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];

            encodedData.position(info.offset);
            encodedData.limit(info.offset + info.size + 7);

            byte[] data = new byte[info.size + 7];
            addADTStoPacket(data, info.size + 7);
            encodedData.get(data, 7, info.size);

            encodedData.position(info.offset);
            writeIntoOutputfile(data);
            encoder.releaseOutputBuffer(encoderStatus, false);
        }
    }

    private void checkIfOutputDone() {
        if (outputDone) {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ioe) {
                    Log.w(TAG, "failed closing debug file");
                    throw new RuntimeException(ioe);
                }
                fos = null;
            }
        }
    }

    /**
     *  Add ADTS header at the beginning of each and every AAC packet.
     *  This is needed as MediaCodec CrappyEncoder generates a packet of raw
     *  AAC data.
     *
     *  Note the packetLen must count in the ADTS header itself.
     **/
    private void addADTStoPacket(byte[] packet, int packetLen) {
        int profile = 2;  //AAC LC
        //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
        int freqIdx = 4;  //44.1KHz
        int chanCfg = 2;  //CPE

        // fill in ADTS data
        packet[0] = (byte)0xFF;
        packet[1] = (byte)0xF9;
        packet[2] = (byte)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
        packet[3] = (byte)(((chanCfg&3)<<6) + (packetLen>>11));
        packet[4] = (byte)((packetLen&0x7FF) >> 3);
        packet[5] = (byte)(((packetLen&7)<<5) + 0x1F);
        packet[6] = (byte)0xFC;
    }

    private void openFileStream() {
        fos = null;
        try {
            fos = new FileOutputStream(outputFile, false);
        } catch (FileNotFoundException e) {
            Log.e("AudioRecorder", e.getMessage());
        }
    }

    /**
     * Writes data into file
     * @param data
     */
    public void writeIntoOutputfile(byte[] data) {
        try {
            fos.write(data);
        } catch (IOException ioe) {
            Log.w(TAG, "failed writing debug data to file");
            throw new RuntimeException(ioe);
        }
    }

    public void stopEncoding() {
        isEncoderRunning = false;
        encoder.stop();
        closeStream();
    }

    private void closeStream() {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            Log.e("AudioRecorder", e.getMessage());
        }
    }
}
4

0 回答 0