无法在android中生成声音信号,持续时间小于100毫秒

时间:2017-11-08 09:21:08

标签: android audio

我试图生成一个10000 Hz的简单正弦波,持续时间为50 ms。我在Android(手机1)中使用AudioTrack播放声音,并在另一部手机(手机20)中使用AudioRecord记录生成的波形。我无法看到持续时间小于100毫秒的任何波形。如果持续时间设置为100毫秒,我可以在手机2中看到录制的波形。我无法理解原因,如果有人可以帮助我,我将感激不尽。以下是我用于传输的代码。

private void playSound(double frequency, int duration) {
    // AudioTrack definition
    final File file = new File(path, fileName);
    try {
        file.createNewFile();

        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        duration = duration * 44100 / 1000; // 44100 = 1 second
        Log.v(TAG, "Bufff: " + duration);
        int mBufferSize = AudioTrack.getMinBufferSize(44100,
                CHANNEL_OUT_MONO,
                ENCODING_PCM_16BIT);

        AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
                AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
                mBufferSize, AudioTrack.MODE_STREAM);

        // Sine wave
        double[] mSound = new double[duration];
        short[] mBuffer = new short[duration];
        int i=0;
        for (i = 0; i < duration; i++) {
            mSound[i] = Math.sin((2.0 * Math.PI * i / (44100 / frequency)));
            mBuffer[i] = (short) (mSound[i] * Short.MAX_VALUE);
        }
        mAudioTrack.play();

        int writeSize = mAudioTrack.write(mBuffer, 0, mBuffer.length);
        Log.v(TAG, "Written: "+writeSize);
        mAudioTrack.stop();
        mAudioTrack.release();
        myOutWriter.close();
        fOut.flush();
        fOut.close();
    } catch(Exception e) {
        Log.e(TAG,e.getMessage(),e);
    }
    Log.d(TAG, "Audio played");
}

录音:

private void recordSound(File path, String fileName) {
    // Write the output audio in byte

    final File file = new File(path, fileName);

    // Save your stream, don't forget to flush() it before closing it.
    Log.v(TAG, file.getAbsolutePath());
    short sData[] = new short[BufferElements2Rec];

    FileOutputStream os = null;
    try {
        file.createNewFile();

        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        int bufferSize = AudioRecord.getMinBufferSize(getValidSampleRates(),
                RECORDER_CHANNELS, ENCODING_PCM_16BIT);
        Log.v(TAG, "Buffer size:"+bufferSize);
        AudioRecord audioRecord = findAudioRecord();

        short[] buffer = new short[bufferSize];
        audioRecord.startRecording();
        int r = 0;
        float totalAbsValue = 0.0f;
        short sample = 0;

        while (isRecording) {
            int bufferReadResult = audioRecord.read(buffer, 0,
                    bufferSize);
            for (int i = 0; i < bufferReadResult; i++) {
                myOutWriter.write(String.valueOf(buffer[i])+"\n");
            }
        }
        audioRecord.stop();
        myOutWriter.close();
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
    Log.v(TAG, "Record finished");
}

0 个答案:

没有答案