1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ex.variablespeed;
18
19import android.media.AudioManager;
20
21import javax.annotation.concurrent.Immutable;
22import javax.annotation.concurrent.NotThreadSafe;
23
24/**
25 * Encapsulates the parameters required to configure the audio engine.
26 * <p>
27 * You should not need to use this class directly, it exists for the benefit of
28 * this package and the classes contained therein.
29 */
30@Immutable
31/*package*/ final class EngineParameters {
32    private final int mTargetFrames;
33    private final int mMaxPlayBufferCount;
34    private final float mWindowDuration;
35    private final float mWindowOverlapDuration;
36    private final float mInitialRate;
37    private final int mDecodeBufferInitialSize;
38    private final int mDecodeBufferMaxSize;
39    private final int mStartPositionMillis;
40    private final int mAudioStreamType;
41
42    public int getTargetFrames() {
43        return mTargetFrames;
44    }
45
46    public int getMaxPlayBufferCount() {
47        return mMaxPlayBufferCount;
48    }
49
50    public float getWindowDuration() {
51        return mWindowDuration;
52    }
53
54    public float getWindowOverlapDuration() {
55        return mWindowOverlapDuration;
56    }
57
58    public float getInitialRate() {
59        return mInitialRate;
60    }
61
62    public int getDecodeBufferInitialSize() {
63        return mDecodeBufferInitialSize;
64    }
65
66    public int getDecodeBufferMaxSize() {
67        return mDecodeBufferMaxSize;
68    }
69
70    public int getStartPositionMillis() {
71        return mStartPositionMillis;
72    }
73
74    public int getAudioStreamType() {
75        return mAudioStreamType;
76    }
77
78    private EngineParameters(int targetFrames, int maxPlayBufferCount, float windowDuration,
79            float windowOverlapDuration, float initialRate, int decodeBufferInitialSize,
80            int decodeBufferMaxSize, int startPositionMillis, int audioStreamType) {
81        mTargetFrames = targetFrames;
82        mMaxPlayBufferCount = maxPlayBufferCount;
83        mWindowDuration = windowDuration;
84        mWindowOverlapDuration = windowOverlapDuration;
85        mInitialRate = initialRate;
86        mDecodeBufferInitialSize = decodeBufferInitialSize;
87        mDecodeBufferMaxSize = decodeBufferMaxSize;
88        mStartPositionMillis = startPositionMillis;
89        mAudioStreamType = audioStreamType;
90    }
91
92    /**
93     * We use the builder pattern to construct an {@link EngineParameters}
94     * object.
95     * <p>
96     * This class is not thread safe, you should confine its use to one thread
97     * or provide your own synchronization.
98     */
99    @NotThreadSafe
100    public static class Builder {
101        private int mTargetFrames = 1000;
102        private int mMaxPlayBufferCount = 2;
103        private float mWindowDuration = 0.08f;
104        private float mWindowOverlapDuration = 0.008f;
105        private float mInitialRate = 1.0f;
106        private int mDecodeBufferInitialSize = 5 * 1024;
107        private int mDecodeBufferMaxSize = 20 * 1024;
108        private int mStartPositionMillis = 0;
109        private int mAudioStreamType = AudioManager.STREAM_MUSIC;
110
111        public EngineParameters build() {
112            return new EngineParameters(mTargetFrames, mMaxPlayBufferCount,
113                    mWindowDuration, mWindowOverlapDuration, mInitialRate,
114                    mDecodeBufferInitialSize, mDecodeBufferMaxSize, mStartPositionMillis,
115                    mAudioStreamType);
116        }
117
118        public Builder maxPlayBufferCount(int maxPlayBufferCount) {
119            mMaxPlayBufferCount = maxPlayBufferCount;
120            return this;
121        }
122
123        public Builder windowDuration(int windowDuration) {
124            mWindowDuration = windowDuration;
125            return this;
126        }
127
128        public Builder windowOverlapDuration(int windowOverlapDuration) {
129            mWindowOverlapDuration = windowOverlapDuration;
130            return this;
131        }
132
133        public Builder initialRate(float initialRate) {
134            mInitialRate = initialRate;
135            return this;
136        }
137
138        public Builder decodeBufferInitialSize(int decodeBufferInitialSize) {
139            mDecodeBufferInitialSize = decodeBufferInitialSize;
140            return this;
141        }
142
143        public Builder decodeBufferMaxSize(int decodeBufferMaxSize) {
144            mDecodeBufferMaxSize = decodeBufferMaxSize;
145            return this;
146        }
147
148        public Builder startPositionMillis(int startPositionMillis) {
149            mStartPositionMillis = startPositionMillis;
150            return this;
151        }
152
153        public Builder audioStreamType(int audioStreamType) {
154            mAudioStreamType = audioStreamType;
155            return this;
156        }
157    }
158}
159