this.bufferSize = bufferSize; audioTimestampPoller = new AudioTimestampPoller(audioTrack); outputSampleRate = audioTrack.getSampleRate(); needsPassthroughWorkarounds = needsPassthroughWorkarounds(outputEncoding); isOutputPcm = Util.isEncodingLinearPcm(outputEncoding);
/** * Reconfigures the audio track utility helper to use the specified {@code audioTrack}. * * @param audioTrack The audio track to wrap. * @param isPassthrough Whether the audio track is used for passthrough (e.g. AC-3) playback. */ public void reconfigure(android.media.AudioTrack audioTrack, boolean isPassthrough) { this.audioTrack = audioTrack; this.isPassthrough = isPassthrough; lastRawPlaybackHeadPosition = 0; rawPlaybackHeadWrapCount = 0; passthroughWorkaroundPauseOffset = 0; if (audioTrack != null) { sampleRate = audioTrack.getSampleRate(); } }
/** * This method is called by SDL using JNI. */ public static int audioOpen(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) { int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO; int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT; int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1); Log.v(TAG, "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer"); // Let the user pick a larger buffer if they really want -- but ye // gods they probably shouldn't, the minimums are horrifyingly high // latency already desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize); if (mAudioTrack == null) { mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM); // Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid // Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java // Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState() if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) { Log.e(TAG, "Failed during initialization of Audio Track"); mAudioTrack = null; return -1; } mAudioTrack.play(); } Log.v(TAG, "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer"); return 0; }
private void logMainParameters() { Logging.d(TAG, "AudioTrack: " + "session ID: " + audioTrack.getAudioSessionId() + ", " + "channels: " + audioTrack.getChannelCount() + ", " + "sample rate: " + audioTrack.getSampleRate() + ", " // Gain (>=1.0) expressed as linear multiplier on sample values. + "max gain: " + audioTrack.getMaxVolume()); }
/** * Create an audio track then call play(). */ public void start(int framesPerBlock) { stop(); mAudioTrack = createAudioTrack(); mLatencyTuner = new AudioLatencyTuner(mAudioTrack, framesPerBlock); // Use frame rate chosen by the AudioTrack so that we can get a // low latency fast mixer track. mFrameRate = mAudioTrack.getSampleRate(); // AudioTrack will wait until it has enough data before starting. mAudioTrack.play(); previousBeginTime = 0; filteredCpuInterval = 0; filteredTotalInterval = 0; }
private boolean areParametersValid(int sampleRate, int channels) { final int streamType = audioTrack.getStreamType(); return (audioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT && audioTrack.getChannelConfiguration() == AudioFormat.CHANNEL_OUT_MONO && streamType == AudioManager.STREAM_VOICE_CALL && audioTrack.getSampleRate() == sampleRate && sampleRate == audioTrack.getNativeOutputSampleRate(streamType) && audioTrack.getChannelCount() == channels); }
.setBufferSizeInBytes(4) // one 16bit 2ch frame .build(); sr = at.getSampleRate(); bs = at.getBufferSizeInFrames(); at.release(); .build(); sr_ll = at.getSampleRate(); bs_ll = at.getBufferSizeInFrames(); at.release(); .setPerformanceMode(AudioTrack.PERFORMANCE_MODE_POWER_SAVING) .build(); sr_ps = at.getSampleRate(); bs_ps = at.getBufferSizeInFrames(); at.release();