view src/samer/audio/AudioSink.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	AudioSink.java
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.audio;
import samer.tools.*;

/**
	General interface for objects that accept a stream of
	samples.
*/

public interface AudioSink
{
	boolean isOpen();
	void open() throws Exception;
	void close();
	void dispose();

	/** Return a task which takes samples from the given buffer
	 *  The idea is that the audio sink can choose the right
	 *  kind of writer depending on the format of the audio stream,
	 *  and then handle any conversions automatically.
	 */
	Task writer(double buf[], int off, int len);
	Task writer(float buf[], int off, int len);

	public static class Util {
		public static void doubleToShort(double[] src, byte [] dst, int off, int n, double k) {
			k*=32768;
			for (int i=0, j=off; j<n+off; j++) {
		 		int y = (int)(k*src[j]);
				dst[i++] = (byte)(y&0xff);
				dst[i++] = (byte)(y>>8);
			}
		}

		public static void doubletoByte(double [] src, byte[] dst, int off, int n, double k) {
			k*=128;
			for (int i=0, j=off; j<n+off; j++) {
		 		int y = (int)(k*src[j]) + 128;
				dst[i++] = (byte)(y&0xff);
			}
		}

		public static void floatToShort(float [] src, byte [] dst, int off, int n, float k) {
			k*=32768;
			for (int i=0, j=off; j<n+off; j++) {
		 		int y = (int)(k*src[j]);
				dst[i++] = (byte)(y&0xff);
				dst[i++] = (byte)(y>>8);
			}
		}

		public static void floatToByte(float [] src, byte [] dst, int off, int n, float k) {
			k*=128;
			for (int i=0, j=off; j<n+off; j++) {
		 		int y = (int)(128.0*src[j]) + 128;
				dst[i++] = (byte)(y&0xff);
			}
		}

	}
}