1

In my project I have implemented an audio record option. For reading real-time voice I used TargetDataLine.I want to record audio with high volume. How can I do that?

        TargetDataLine line;
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        File file = new File("RecordedVoice.raw");
        if(file.exists())
            file.delete();
        file.createNewFile();
        fos = new FileOutputStream(file);
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Line not supported: " + format.toString());
            System.exit(0);
        } else {
            try {
                line = (TargetDataLine) AudioSystem.getLine(info);
                line.open(format);
                out = new ByteArrayOutputStream();
                int numBytesRead;
                byte[] data = new byte[line.getBufferSize()/5];
                line.start();
                while (!isCancelled()) {
                    numBytesRead = line.read(data, 0, data.length);
                    out.write(data, 0, numBytesRead);
                    fos.write(data, 0, data.length);
                }
                out.close();
            } catch (Exception excp) {
                System.out.println("Error! Could not open Audio System line!");
                excp.printStackTrace();
            }
        }
  • as you know loudness in English is equal to volume so this question has been answered here https://stackoverflow.com/questions/26618446/is-it-possible-to-change-the-volume-of-sound-by-manipulating-the-bytestream-that – gpasch Sep 20 '21 at 02:08

1 Answers1

0

In the while loop, these steps:

  1. convert the bytes to PCM values (depends on format, e.g., 16 bit is two bytes, 24 bit is three bytes, also pay attention whether it is big-endian or little-endian as this gives you the order of the bytes

  2. multiply the PCM values by a volume factor (e.g., 1.1 to raise the volume a bit). You might want to also include a Math.min() to prevent the values from exceeding the range of the bits used to encode the values.

  3. convert the PCM values back to bytes, according to your audio format specs

There may be a way to do this instead via Controls. The plan I wrote about is basically how to do it manually, as written about in the linked tutorial in the last section "Manipulating the Audio Data Directly". Benefits: this is all within Java, instead of being dependent upon the given PC and OS (audio Controls are not guaranteed to exist). Also you have frame-level granularity. I think the Controls in the article tend to only enact changes at buffer boundaries.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41