0

I'm trying to make a system that starts listening everything on a mic without recording, that stores it in a ByteArrayOutputStream object and when it reaches a certain level it starts recording, when it drops below the selected level that stops recording again but keeps listening and so on...

This is the code I am using:

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class tests {

    protected static boolean running;
    private static ByteArrayOutputStream output;
    final static float MAX_16_BITS_SIGNED = Short.MAX_VALUE;
    final static float MAX_16_BITS_UNSIGNED = 0xffff;
    private static float level;

    private static AudioFormat getFormat() {
        return new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
    }

    private static void stopAudio() {
        OutputStream wavOutput = null;
        try {
            System.out.println("Saving...");
            rinning = false;
            wavOutput = new FileOutputStream(System.getProperty("user.home") + "/Desktop/test1.wav");
            output.writeTo(wavOutput);
            System.out.println("WAV file saved. Goodbye!");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                wavOutput.close();
            } catch (IOException ex) {
                Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private static void recordingAudio() {
        try {
            final AudioFormat format = getFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            final TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(info);
            mic.open(format);
            mic.start();
            Thread tRecording = new Thread(new Runnable() {
                int bufferLength = (int) format.getSampleRate() * format.getFrameSize();
                byte buffer[] = new byte[bufferLength];

                public void run() {
                    output = new ByteArrayOutputStream();
                    running = true;
                    while (running) {
                        int count = mic.read(buffer, 0, buffer.length);
                        calculateLevel(buffer, 0, 0);
                        int levelPercent = (int) (level * 100);
                        int gain = 50;
                        if (levelPercent > gain) {
                            output.write(buffer, 0, count);
                            System.out.println("Recording");
                        }
                    }
                    mic.stop();
                }
            });
            tRecording.start();
        } catch (LineUnavailableException e) {
            System.err.println("Line unavailable: " + e);
            System.exit(-2);
        }
    }

    private static void calculateLevel(byte[] buffer, int readPoint, int leftOver) {
        int max = 0;
        boolean signed = (getFormat().getEncoding() == AudioFormat.Encoding.PCM_SIGNED);
        boolean bigEndian = (getFormat().isBigEndian());
        for (int i = readPoint; i < buffer.length - leftOver; i += 2) {
            int value = 0;
            int hiByte = (bigEndian ? buffer[i] : buffer[i + 1]);
            int loByte = (bigEndian ? buffer[i + 1] : buffer[i]);
            if (signed) {
                short shortVal = (short) hiByte;
                shortVal = (short) ((shortVal << 8) | (byte) loByte);
                value = shortVal;
            } else {
                value = (hiByte << 8) | loByte;
            }
            max = Math.max(max, value);
        }
        if (signed) {
            level = (float) max / MAX_16_BITS_SIGNED;
        } else {
            level = (float) max / MAX_16_BITS_UNSIGNED;
        }
    }

    public static void main(String args[]) {
        recordingAudio();
        System.out.println("Stop recording press '1' + Enter");
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        try {
            if (br.readLine().equals("1")) {
                stopAudio();
            }
        } catch (IOException ex) {
            Logger.getLogger(pruebas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

I can detect audio level, just need to save it... In the stopAudio() method is where ByteArrayOutputStream should be converted to WAV file, and it works, but I can't plaly it

carope9
  • 31
  • 5

1 Answers1

0

It's not a WAV format file you're saving, it's the raw audio data. You'd need to write that data as a WAV file.

Although this question is a bit outdated, there are some references and libraries that might still be usable (or just find a more recent library from somewhere).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • doesnt the audio system have a write method??!! – gpasch Oct 31 '19 at 04:46
  • @gpasch does it? I haven't touched Java sound in over a decade, but I remember it being cumbersome to use, and the linked question doesn't seem to indicate otherwise. – Kayaman Oct 31 '19 at 06:20