| /* |
| * Copyright 2020 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.hyphonate.megaaudio.player; |
| |
| import android.media.AudioFormat; |
| import android.media.AudioTrack; |
| |
| import org.hyphonate.megaaudio.common.StreamBase; |
| |
| /** |
| * An abstract class defining the common operations and attributes for all |
| * player (concrete) sub-classes. |
| */ |
| public abstract class Player extends StreamBase { |
| public Player(AudioSourceProvider sourceProvider) { |
| mSourceProvider = sourceProvider; |
| } |
| |
| /* |
| * Audio Source |
| */ |
| protected AudioSourceProvider mSourceProvider; |
| |
| // |
| // Attributes |
| // |
| // This needs to be static because it is called before creating the Recorder subclass |
| public static int calcMinBufferFrames(int channelCount, int sampleRate) { |
| int channelMask = Player.channelCountToChannelMask(channelCount); |
| int bufferSizeInBytes = |
| AudioTrack.getMinBufferSize (sampleRate, |
| channelMask, |
| AudioFormat.ENCODING_PCM_FLOAT); |
| return bufferSizeInBytes / sampleSizeInBytes(AudioFormat.ENCODING_PCM_FLOAT); |
| } |
| |
| // |
| // Status |
| // |
| public abstract boolean isPlaying(); |
| |
| /* |
| * Channel utils |
| */ |
| // TODO Consider moving these to a "Utility" library. |
| /** |
| * @param channelCount The number of channels for which to generate an output position mask. |
| * @return An output channel-position mask corresponding to the supplied number of channels. |
| */ |
| public static int channelCountToChannelMask(int channelCount) { |
| switch (channelCount) { |
| case 1: |
| return AudioFormat.CHANNEL_OUT_MONO; |
| |
| case 2: |
| return AudioFormat.CHANNEL_OUT_STEREO; |
| |
| case 3: |
| return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER; |
| |
| case 4: |
| return AudioFormat.CHANNEL_OUT_QUAD; |
| |
| case 5: // 5.0 |
| return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER; |
| |
| case 6: // 5.1 |
| return AudioFormat.CHANNEL_OUT_5POINT1; |
| |
| case 7: // 6.1 |
| return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER; |
| |
| case 8: |
| return AudioFormat.CHANNEL_OUT_7POINT1; |
| |
| |
| default: |
| return AudioTrack.ERROR_BAD_VALUE; |
| } |
| } |
| |
| } |