annotate src/samer/audio/StreamSource.java.old @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
rev   line source
samer@1 1 /*
samer@1 2 * FileSource.java
samer@1 3 *
samer@1 4 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@1 5 * All rights reserved.
samer@1 6 *
samer@1 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@1 8 * without even the implied warranty of MERCHANTABILITY or
samer@1 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@1 10 */
samer@1 11
samer@1 12 package samer.audio;
samer@1 13
samer@1 14 import samer.core.*;
samer@1 15 import samer.tools.*;
samer@1 16 import javax.sound.sampled.*;
samer@1 17 import java.io.*;
samer@1 18
samer@1 19 /**
samer@1 20 A lightweight AudioSource that reads from a single Stream.
samer@1 21 Any format for which the appropriate JavaSound plug-in is installed on
samer@1 22 your system, eg WAV, AU, MP3, OGG. The implementation of
samer@1 23 */
samer@1 24
samer@1 25 public class StreamSource implements AudioSource
samer@1 26 {
samer@1 27 InputStream in=null;
samer@1 28 AudioFormat format=null;
samer@1 29
samer@1 30 /**
samer@1 31 * Construct a FileSource initialised with current file and loop initialised
samer@1 32 * from the current Environment. No exception is thrown if the current file
samer@1 33 * cannot be opened, however, you must be sure to set it to a valid file
samer@1 34 * (or to set a playlist) before trying to read any samples.
samer@1 35 */
samer@1 36 public StreamSource(InputStream in) throws Exception { open(in); }
samer@1 37 public StreamSource(InputStream in, AudioFormat f) throws Exception { format=f; open(in); }
samer@1 38 public StreamSource(AudioFormat f) { format=f; }
samer@1 39
samer@1 40 public void dispose() { close(); }
samer@1 41 public void setTargetFormat(AudioFormat f) { format=f; }
samer@1 42
samer@1 43 /** Returns number of bytes available in current file */
samer@1 44 public int available() throws Exception { return in.available(); }
samer@1 45
samer@1 46
samer@1 47 /** Allows reading from any given InputStream, but does not affect
samer@1 48 * current file or playlist. */
samer@1 49 public synchronized void open(InputStream s) throws Exception {
samer@1 50 open(AudioSystem.getAudioInputStream(s));
samer@1 51 }
samer@1 52
samer@1 53 public synchronized void open(AudioInputStream s) throws Exception
samer@1 54 {
samer@1 55 Shell.trace("stream format: "+s.getFormat());
samer@1 56 Shell.trace("file format: "+s.getFormat());
samer@1 57
samer@1 58 // in = new BufferedInputStream(s,16*1024);
samer@1 59 // convert to target format if required
samer@1 60 in=s;
samer@1 61 if (format!=null) {
samer@1 62 Shell.trace("Converting to "+format);
samer@1 63 in=AudioSystem.getAudioInputStream(format,s);
samer@1 64 }
samer@1 65 }
samer@1 66
samer@1 67 /** Closes current input stream */
samer@1 68 public synchronized void close() {
samer@1 69 try { if (in!=null) in.close(); in=null; }
samer@1 70 catch (IOException ex) {}
samer@1 71 }
samer@1 72
samer@1 73 /** Returns a Task which copies samples as doubles into the given
samer@1 74 * buffer between the given positions. */
samer@1 75 public Task reader(final double [] dbuf, final int off, final int len) {
samer@1 76 return new AnonymousTask() {
samer@1 77 int blen=2*len;
samer@1 78 byte [] bbuf = new byte[blen];
samer@1 79
samer@1 80 public void run() throws Exception {
samer@1 81 int n = 0;
samer@1 82
samer@1 83 while (n < blen) {
samer@1 84 int count = in.read(bbuf, n, blen - n);
samer@1 85 if (count > 0) n+=count;
samer@1 86 else throw new EOFException();
samer@1 87 }
samer@1 88 Util.shortToDouble(bbuf,dbuf,off,len);
samer@1 89 }
samer@1 90 };
samer@1 91 }
samer@1 92
samer@1 93 /** Returns a Task which copies samples as floats into the given
samer@1 94 * buffer between the given positions. */
samer@1 95 public Task reader(final float [] dbuf, final int off, final int len) {
samer@1 96 return new AnonymousTask() {
samer@1 97 int blen=2*len;
samer@1 98 byte [] bbuf = new byte[blen];
samer@1 99
samer@1 100 public synchronized void run() throws Exception {
samer@1 101 int n = 0;
samer@1 102
samer@1 103 while (n < blen) {
samer@1 104 int count = in.read(bbuf, n, blen - n);
samer@1 105 if (count > 0) n+=count;
samer@1 106 else throw new EOFException();
samer@1 107 }
samer@1 108 Util.shortToFloat(bbuf,dbuf,off,len);
samer@1 109 }
samer@1 110 };
samer@1 111 }
samer@1 112 }
samer@1 113