|
| 1 | +/** |
| 2 | + * Copyright 2017 Kailash Dabhi (Kingbull Technology) |
| 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 com.kailashdabhi.omrecorder; |
| 17 | + |
| 18 | +import android.media.AudioFormat; |
| 19 | +import android.media.AudioRecord; |
| 20 | +import android.os.Build; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.OutputStream; |
| 24 | + |
| 25 | +/** |
| 26 | + * This class represents a bus between {@link PullableSource} and {@link OutputStream}. |
| 27 | + * Basically it just pulls the data from {@link PullableSource} and transport it to |
| 28 | + * {@link OutputStream} |
| 29 | + * |
| 30 | + * @author Kailash Dabhi |
| 31 | + * @date 06-07-2016 |
| 32 | + */ |
| 33 | +public interface PullTransport { |
| 34 | + /** |
| 35 | + * It starts to pull the {@link PullableSource} and transport it to |
| 36 | + * {@link OutputStream} |
| 37 | + * |
| 38 | + * @param outputStream the OutputStream where we want to transport the pulled audio data. |
| 39 | + * @throws IOException if there is any problem arise in pulling and transporting |
| 40 | + */ |
| 41 | + void start(OutputStream outputStream) throws IOException; |
| 42 | + |
| 43 | + //It immediately stop pulling PullableSource |
| 44 | + void stop(); |
| 45 | + |
| 46 | + //Returns the pullableSource which is used for pulling |
| 47 | + PullableSource pullableSource(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Interface definition for a callback to be invoked when a chunk of audio is pulled from |
| 51 | + * {@link PullableSource}. |
| 52 | + */ |
| 53 | + interface OnAudioChunkPulledListener { |
| 54 | + /** |
| 55 | + * Called when {@link PullableSource} is pulled and returned{@link AudioChunk}. |
| 56 | + */ |
| 57 | + void onAudioChunkPulled(AudioChunk audioChunk); |
| 58 | + } |
| 59 | + |
| 60 | + abstract class AbstractPullTransport implements PullTransport { |
| 61 | + final PullableSource pullableSource; |
| 62 | + final OnAudioChunkPulledListener onAudioChunkPulledListener; |
| 63 | + private final UiThread uiThread = new UiThread(); |
| 64 | + |
| 65 | + AbstractPullTransport(PullableSource pullableSource, |
| 66 | + OnAudioChunkPulledListener onAudioChunkPulledListener) { |
| 67 | + this.pullableSource = pullableSource; |
| 68 | + this.onAudioChunkPulledListener = onAudioChunkPulledListener; |
| 69 | + } |
| 70 | + |
| 71 | + @Override public void start(OutputStream outputStream) throws IOException { |
| 72 | + startPoolingAndWriting(pullableSource.preparedToBePulled(), pullableSource.pullSizeInBytes(), |
| 73 | + outputStream); |
| 74 | + } |
| 75 | + |
| 76 | + void startPoolingAndWriting(AudioRecord audioRecord, int pullSizeInBytes, |
| 77 | + OutputStream outputStream) throws IOException { |
| 78 | + } |
| 79 | + |
| 80 | + @Override public void stop() { |
| 81 | + pullableSource.isEnableToBePulled(false); |
| 82 | + pullableSource.audioRecord().stop(); |
| 83 | + pullableSource.audioRecord().release(); |
| 84 | + } |
| 85 | + |
| 86 | + public PullableSource pullableSource() { |
| 87 | + return pullableSource; |
| 88 | + } |
| 89 | + |
| 90 | + void postSilenceEvent(final Recorder.OnSilenceListener onSilenceListener, |
| 91 | + final long silenceTime) { |
| 92 | + uiThread.execute(new Runnable() { |
| 93 | + @Override public void run() { |
| 94 | + onSilenceListener.onSilence(silenceTime); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + void postPullEvent(final AudioChunk audioChunk) { |
| 100 | + uiThread.execute(new Runnable() { |
| 101 | + @Override public void run() { |
| 102 | + onAudioChunkPulledListener.onAudioChunkPulled(audioChunk); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + final class Default extends AbstractPullTransport { |
| 109 | + private final WriteAction writeAction; |
| 110 | + |
| 111 | + public Default(PullableSource pullableSource, WriteAction writeAction) { |
| 112 | + this(pullableSource, null, writeAction); |
| 113 | + } |
| 114 | + |
| 115 | + public Default(PullableSource pullableSource, |
| 116 | + OnAudioChunkPulledListener onAudioChunkPulledListener, WriteAction writeAction) { |
| 117 | + super(pullableSource, onAudioChunkPulledListener); |
| 118 | + this.writeAction = writeAction; |
| 119 | + } |
| 120 | + |
| 121 | + public Default(PullableSource pullableSource, |
| 122 | + OnAudioChunkPulledListener onAudioChunkPulledListener) { |
| 123 | + this(pullableSource, onAudioChunkPulledListener, new WriteAction.Default()); |
| 124 | + } |
| 125 | + |
| 126 | + public Default(PullableSource audioRecordSource) { |
| 127 | + this(audioRecordSource, null, new WriteAction.Default()); |
| 128 | + } |
| 129 | + |
| 130 | + @Override void startPoolingAndWriting(AudioRecord audioRecord, int pullSizeInBytes, |
| 131 | + OutputStream outputStream) throws IOException { |
| 132 | + int audioFormat = audioRecord.getAudioFormat(); |
| 133 | + if (audioFormat == AudioFormat.ENCODING_PCM_FLOAT && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 134 | + AudioChunk.Floats audioChunk = new AudioChunk.Floats(new float[pullSizeInBytes]); |
| 135 | + while (pullableSource.isEnableToBePulled()) { |
| 136 | + |
| 137 | + audioChunk.readCount(audioRecord.read(audioChunk.toFloats(), 0, pullSizeInBytes, AudioRecord.READ_BLOCKING)); |
| 138 | + int readCount = audioChunk.readCount(); |
| 139 | + if (AudioRecord.ERROR_INVALID_OPERATION != readCount |
| 140 | + && AudioRecord.ERROR_BAD_VALUE != readCount) { |
| 141 | + if (onAudioChunkPulledListener != null) { |
| 142 | + postPullEvent(audioChunk); |
| 143 | + } |
| 144 | + writeAction.execute(audioChunk, outputStream); |
| 145 | + } |
| 146 | + } |
| 147 | + } else { |
| 148 | + AudioChunk audioChunk = new AudioChunk.Bytes(new byte[pullSizeInBytes]); |
| 149 | + while (pullableSource.isEnableToBePulled()) { |
| 150 | + audioChunk.readCount(audioRecord.read(audioChunk.toBytes(), 0, pullSizeInBytes)); |
| 151 | + int readCount = audioChunk.readCount(); |
| 152 | + if (AudioRecord.ERROR_INVALID_OPERATION != readCount |
| 153 | + && AudioRecord.ERROR_BAD_VALUE != readCount) { |
| 154 | + if (onAudioChunkPulledListener != null) { |
| 155 | + postPullEvent(audioChunk); |
| 156 | + } |
| 157 | + writeAction.execute(audioChunk, outputStream); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + final class Noise extends AbstractPullTransport { |
| 166 | + private final long silenceTimeThreshold; |
| 167 | + private final Recorder.OnSilenceListener silenceListener; |
| 168 | + private final WriteAction writeAction; |
| 169 | + private long firstSilenceMoment = 0; |
| 170 | + private int noiseRecordedAfterFirstSilenceThreshold = 0; |
| 171 | + |
| 172 | + public Noise(PullableSource pullableSource, |
| 173 | + OnAudioChunkPulledListener onAudioChunkPulledListener, |
| 174 | + Recorder.OnSilenceListener silenceListener, long silenceTimeThreshold) { |
| 175 | + this(pullableSource, onAudioChunkPulledListener, new WriteAction.Default(), |
| 176 | + silenceListener, silenceTimeThreshold); |
| 177 | + } |
| 178 | + |
| 179 | + public Noise(PullableSource pullableSource, |
| 180 | + OnAudioChunkPulledListener onAudioChunkPulledListener, WriteAction writeAction, |
| 181 | + Recorder.OnSilenceListener silenceListener, long silenceTimeThreshold) { |
| 182 | + super(pullableSource, onAudioChunkPulledListener); |
| 183 | + this.writeAction = writeAction; |
| 184 | + this.silenceListener = silenceListener; |
| 185 | + this.silenceTimeThreshold = silenceTimeThreshold; |
| 186 | + } |
| 187 | + |
| 188 | + public Noise(PullableSource pullableSource, WriteAction writeAction, |
| 189 | + Recorder.OnSilenceListener silenceListener, long silenceTimeThreshold) { |
| 190 | + this(pullableSource, null, writeAction, silenceListener, silenceTimeThreshold); |
| 191 | + } |
| 192 | + |
| 193 | + public Noise(PullableSource pullableSource, Recorder.OnSilenceListener silenceListener, |
| 194 | + long silenceTimeThreshold) { |
| 195 | + this(pullableSource, null, new WriteAction.Default(), silenceListener, |
| 196 | + silenceTimeThreshold); |
| 197 | + } |
| 198 | + |
| 199 | + public Noise(PullableSource pullableSource, Recorder.OnSilenceListener silenceListener) { |
| 200 | + this(pullableSource, null, new WriteAction.Default(), silenceListener, 200); |
| 201 | + } |
| 202 | + |
| 203 | + public Noise(PullableSource pullableSource) { |
| 204 | + this(pullableSource, null, new WriteAction.Default(), null, 200); |
| 205 | + } |
| 206 | + |
| 207 | + @Override void startPoolingAndWriting(AudioRecord audioRecord, int pullSizeInBytes, |
| 208 | + OutputStream outputStream) throws IOException { |
| 209 | + final AudioChunk.Shorts audioChunk = new AudioChunk.Shorts(new short[pullSizeInBytes]); |
| 210 | + while (pullableSource.isEnableToBePulled()) { |
| 211 | + short[] shorts = audioChunk.toShorts(); |
| 212 | + audioChunk.readCount(audioRecord.read(shorts, 0, shorts.length)); |
| 213 | + if (AudioRecord.ERROR_INVALID_OPERATION != audioChunk.readCount() |
| 214 | + && AudioRecord.ERROR_BAD_VALUE != audioChunk.readCount()) { |
| 215 | + if (onAudioChunkPulledListener != null) { |
| 216 | + postPullEvent(audioChunk); |
| 217 | + } |
| 218 | + if (audioChunk.peakIndex() > -1) { |
| 219 | + writeAction.execute(audioChunk, outputStream); |
| 220 | + firstSilenceMoment = 0; |
| 221 | + noiseRecordedAfterFirstSilenceThreshold++; |
| 222 | + } else { |
| 223 | + if (firstSilenceMoment == 0) { |
| 224 | + firstSilenceMoment = System.currentTimeMillis(); |
| 225 | + } |
| 226 | + final long silenceTime = System.currentTimeMillis() - firstSilenceMoment; |
| 227 | + if (firstSilenceMoment != 0 && silenceTime > this.silenceTimeThreshold) { |
| 228 | + if (silenceTime > 1000) { |
| 229 | + if (noiseRecordedAfterFirstSilenceThreshold >= 3) { |
| 230 | + noiseRecordedAfterFirstSilenceThreshold = 0; |
| 231 | + if (silenceListener != null) { |
| 232 | + postSilenceEvent(silenceListener, silenceTime); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } else { |
| 237 | + writeAction.execute(audioChunk, outputStream); |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments