1 /* 2 * Copyright (C) 2015 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 17 package org.drrickorang.loopback; 18 19 20 /** 21 * This class is a pipe that allows one writer and one reader. 22 */ 23 24 public abstract class Pipe { 25 public static final int OVERRUN = -2; // when there's an overrun, return this value 26 27 protected int mSamplesOverrun; 28 protected int mOverruns; 29 protected final int mMaxValues; // always a power of two 30 31 /** maxSamples must be >= 2. */ Pipe(int maxSamples)32 public Pipe(int maxSamples) { 33 mMaxValues = Utilities.roundup(maxSamples); // round up to the nearest power of 2 34 } 35 36 /** 37 * Read at most "count" number of samples into array "buffer", starting from index "offset". 38 * If the available samples to read is smaller than count, just read as much as it can and 39 * return the amount of samples read (non-blocking). offset + count must be <= buffer.length. 40 */ read(short[] buffer, int offset, int count)41 public abstract int read(short[] buffer, int offset, int count); 42 43 /** Return the amount of samples available to read. */ availableToRead()44 public abstract int availableToRead(); 45 46 /** Clear the pipe. */ flush()47 public abstract void flush(); 48 49 } 50