1 package com.neuronrobotics.bowlerstudio;
4 import java.io.IOException;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
8 import javax.sound.sampled.AudioFormat;
9 import javax.sound.sampled.AudioInputStream;
10 import javax.sound.sampled.AudioSystem;
11 import javax.sound.sampled.DataLine;
12 import javax.sound.sampled.FloatControl;
13 import javax.sound.sampled.LineListener;
14 import javax.sound.sampled.SourceDataLine;
15 import javax.sound.sampled.UnsupportedAudioFileException;
17 import marytts.util.data.audio.MonoAudioInputStream;
18 import marytts.util.data.audio.StereoAudioInputStream;
28 public static final int MONO = 0;
32 private AudioInputStream
ais;
35 private SourceDataLine
line;
50 int xfadeDistance = 16;
51 double[] samples =
new double[xfadeDistance];
56 public AudioStatus update(
AudioStatus currentStatus,
double amplitudeUnitVector,
double currentRollingAverage,
57 double currentDerivitiveTerm,
double percent) {
60 for (
int i = 0; i < xfadeDistance; i++) {
61 samples[i] = currentRollingAverage;
64 double index = samples[xfadeIndex];
65 samples[xfadeIndex] = currentRollingAverage;
67 if (xfadeIndex == xfadeDistance) {
70 double val = (currentRollingAverage + index) / 2 * currentDerivitiveTerm;
71 switch (currentStatus) {
99 public AudioInputStream startProcessing(AudioInputStream
ais, String
TTSString) {
136 public AudioPlayer(File audioFile)
throws IOException, UnsupportedAudioFileException {
137 this.ais = AudioSystem.getAudioInputStream(audioFile);
154 this.ais = AudioSystem.getAudioInputStream(audioFile);
175 throws IOException, UnsupportedAudioFileException {
176 this.ais = AudioSystem.getAudioInputStream(audioFile);
206 throws IOException, UnsupportedAudioFileException {
207 this.ais = AudioSystem.getAudioInputStream(audioFile);
236 throw new IllegalStateException(
"Cannot set audio while playing");
281 if (
getLine() !=
null &&
getLine().isControlSupported(FloatControl.Type.MASTER_GAIN))
282 ((FloatControl)
getLine().getControl(FloatControl.Type.MASTER_GAIN))
283 .setValue((
float) (20 * Math.log10(fGain <= 0.0 ? 0.0000 : fGain)));
296 AudioFormat audioFormat =
ais.getFormat();
297 if (audioFormat.getChannels() == 1) {
300 audioFormat =
ais.getFormat();
303 assert audioFormat.getChannels() == 2 :
"Unexpected number of channels: " + audioFormat.getChannels();
305 ais =
new MonoAudioInputStream(
ais);
313 DataLine.Info info =
new DataLine.Info(SourceDataLine.class, audioFormat);
317 boolean bIsSupportedDirectly = AudioSystem.isLineSupported(info);
318 if (!bIsSupportedDirectly) {
319 AudioFormat sourceFormat = audioFormat;
320 AudioFormat targetFormat =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
321 sourceFormat.getSampleRate(), sourceFormat.getSampleSizeInBits(),
322 sourceFormat.getChannels(),
323 sourceFormat.getChannels() * (sourceFormat.getSampleSizeInBits() / 8),
324 sourceFormat.getSampleRate(), sourceFormat.isBigEndian());
326 ais = AudioSystem.getAudioInputStream(targetFormat,
ais);
327 audioFormat =
ais.getFormat();
329 info =
new DataLine.Info(SourceDataLine.class, audioFormat);
330 setLine((SourceDataLine) AudioSystem.getLine(info));
336 }
catch (Exception ex) {
337 Logger.getLogger(getClass().getName()).log(Level.WARNING,
null, ex);
345 byte[] abData =
new byte[6553];
348 int integralIndex = 0;
349 double integralTotal = 0;
350 double[] buffer =
null;
351 Double previousValue =
null;
353 while ((nRead != -1) && (!
exitRequested) && (!Thread.interrupted())) {
360 nRead =
ais.read(abData, 0, abData.length);
361 }
catch (IOException ex) {
362 Logger.getLogger(getClass().getName()).log(Level.WARNING,
null, ex);
365 int amountToRead = nRead;
371 for (
int i = 0; i < nRead - 1; i += 2) {
372 if (Thread.interrupted()) {
376 int upperByteOfAmplitude = abData[i];
377 if (upperByteOfAmplitude < 0)
378 upperByteOfAmplitude += 256;
379 int lowerByteOfAmplitude = abData[i + 1];
380 if (lowerByteOfAmplitude < 0)
381 lowerByteOfAmplitude += 256;
382 double amplitude16Bit = (upperByteOfAmplitude << 8) + (lowerByteOfAmplitude);
383 double amplitudeUnitVector = amplitude16Bit / 65535.0;
385 if (previousValue ==
null) {
387 previousValue = amplitudeUnitVector;
389 if (buffer ==
null) {
394 buffer[j] = amplitudeUnitVector;
398 integralTotal = integralTotal + amplitudeUnitVector - buffer[integralIndex];
400 buffer[integralIndex] = amplitudeUnitVector;
407 double currentDerivitiveTerm = (amplitudeUnitVector - previousValue) *
getDerivitiveGain();
408 previousValue = amplitudeUnitVector;
409 double tmpAmtToRead = i - lastIndex;
410 double tmpTotal = total + tmpAmtToRead;
411 double len = (
ais.getFrameLength() * 2);
412 double percentTmp = tmpTotal / len * 100.0;
415 currentDerivitiveTerm, percentTmp);
416 boolean change = newStat !=
status;
419 if (i == (nRead - 2)) {
423 amountToRead = i - lastIndex;
424 total += amountToRead;
426 if (total >= (len - 2)) {
430 double percent = now / len * 100.0;
432 getLine().write(abData, lastIndex, amountToRead);
438 double len = (
ais.getFrameLength() * 2);
440 double percent = now / len * 100.0;
443 getLine().write(abData, lastIndex, amountToRead);
449 }
catch (Throwable t) {
455 }
catch (Throwable t) {
AudioPlayer(AudioInputStream ais, SourceDataLine line, LineListener lineListener, int outputMode)
void setAudio(AudioInputStream audio)
static IAudioProcessingLambda getLambda()
AudioPlayer(AudioInputStream ais, SourceDataLine line, LineListener lineListener)
static void setIntegralDepth(int integralDepth)
static void setLowerThreshhold(double lt)
static final int RIGHT_ONLY
static double getThreshhold()
static double getDerivitiveGain()
ISpeakingProgress speakProgress
AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener)
ISpeakingProgress getSpeakProgress()
static void setDerivitiveGain(double derivitiveGain)
static IAudioProcessingLambda lambda
LineListener lineListener
static void setLambda(IAudioProcessingLambda lambda)
double[] getIntegralBuffer()
static final int LEFT_ONLY
static double integralGain
void setSpeakProgress(ISpeakingProgress speakProgress)
AudioPlayer(File audioFile)
AudioPlayer(AudioInputStream ais)
AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener, int outputMode)
static double derivitiveGain
void setGain(float fGain)
static double lowerThreshhold
static double getLowerThreshhold()
void setLine(SourceDataLine line)
static int getIntegralDepth()
static void setThreshhold(double t)
AudioPlayer(AudioInputStream ais, LineListener lineListener)
static void setIntegralGain(double integralGain)
AudioPlayer(File audioFile, LineListener lineListener)
static double getIntegralGain()
AudioInputStream startProcessing(AudioInputStream ais, String ttsString)
AudioStatus update(AudioStatus current, double amplitudeUnitVector, double currentRollingAverage, double currentDerivitiveTerm, double percent)
void update(double percentage, AudioStatus status)