1 /*
2  * Copyright 2020 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 package org.hyphonate.megaaudio.recorder;
17 
18 import org.hyphonate.megaaudio.common.BuilderBase;
19 
20 /**
21  * Class to construct contrete Recorder objects.
22  */
23 public class RecorderBuilder extends BuilderBase {
24     @SuppressWarnings("unused")
25     private static final String TAG = RecorderBuilder.class.getSimpleName();
26     @SuppressWarnings("unused")
27     private static final boolean LOG = false;
28 
29     /**
30      * Consumes recorded audio.
31      */
32     private AudioSinkProvider mSinkProvider;
33 
34     /**
35      * Specified the input preset for the constructed stream.
36      */
37     private int mInputPreset = Recorder.INPUT_PRESET_NONE;
38 
RecorderBuilder()39     public RecorderBuilder() {
40     }
41 
42     //
43     // Recorder-Specific Attributes
44     //
45     /**
46      * Specifies the recorder type
47      * @param type Composed from API Types & API subtypes (defined in BuilderBase)
48      * @return this RecorderBuilder (for cascaded calls)
49      */
setRecorderType(int type)50     public RecorderBuilder setRecorderType(int type) {
51         mType = type;
52         return this;
53     }
54 
55     /**
56      * Specifies the AudioSinkProvider which will allocate an AudioSink subclass object
57      * to consume audio data for this stream.
58      * @param sinkProvider Allocates the AudioSink to receive data from the created stream.
59      * @return this RecorderBuilder (for cascaded calls)
60      */
setAudioSinkProvider(AudioSinkProvider sinkProvider)61     public RecorderBuilder setAudioSinkProvider(AudioSinkProvider sinkProvider) {
62         mSinkProvider = sinkProvider;
63         return this;
64     }
65 
66     /**
67      *
68      * @param inputPreset The input preset for the created stream. See Recorder.INPUT_PRESET_
69      * constants.
70      * @return this RecorderBuilder (for cascaded calls)
71      */
setInputPreset(int inputPreset)72     public RecorderBuilder setInputPreset(int inputPreset) {
73         mInputPreset = inputPreset;
74         return this;
75     }
76 
77     /**
78      * @return the input preset ID for the created Recorder.
79      */
getInputPreset()80     public int getInputPreset() {
81         return mInputPreset;
82     }
83 
build()84     public Recorder build() throws BadStateException {
85         if (mSinkProvider == null) {
86             throw new BadStateException();
87         }
88 
89         Recorder recorder = null;
90         int playerType = mType & TYPE_MASK;
91         switch (playerType) {
92             case TYPE_NONE:
93                 // NOP
94                 break;
95 
96             case TYPE_JAVA:
97                 recorder = new JavaRecorder(this, mSinkProvider);
98                 break;
99 
100             case TYPE_OBOE: {
101                 int recorderSubType = mType & SUB_TYPE_MASK;
102                 recorder = new OboeRecorder(this, mSinkProvider, recorderSubType);
103             }
104             break;
105 
106             default:
107                 throw new BadStateException();
108         }
109 
110         return recorder;
111     }
112 
113     /**
114      * Exception class used to signal a failure to allocate an API-specific stream in the build()
115      * method.
116      */
117     public class BadStateException extends Throwable {
118     }
119 }
120