3

I am really new in sound processing and till date I have been able to understand (with a lot of help and criticism :P ) how to (1) take 2 frequencies and then generate audio out of it, alternatively. Then, (2) write that audio as a .wav file that can be played by media players. Then, (3) instead of time I took input from the user in the form of bits(8 bytes max) and when there is '0' in the given input I took the 1st frequency and in case of '1' the 2nd frequency. I am attaching the above mentioned code, the '(3)' one, just for the sake of helping someone who needs it. If you want to see my previous code, click here

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class AudioBits {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        final double SAMPLING_RATE = 44100;             // Audio sampling rate
        float timeInterval = in.nextFloat();            //Time specified by user in milliseconds for each bit to be played
        int frequency1 = in.nextInt();                  //Frequency1 specified by the user in hz
        int frequency2 = in.nextInt();                  //Frequency2 specified by the user in hz

        //To check if the user enters the value in the form of 0-1 or not, as that what is required
        //And also the bits entered should not be greater than 8
        while (!in.hasNext("[0-1]{1,8}")) {
            System.out.println("Wrong input.");
            in.next();
        }
        //Value in zero-one form. Where there is '0' it means one frequency and incase of '1' it means the other frequency
        String binary = in.next();

        //Converting the String value of one-zero form into its equivalent integer
        int value = Integer.parseInt(binary, 2);

        int binVal = 0b10000000;                        //Used to perform '&' operation with 'value'

        //Size of buffer[], which in case of 2 sec is 88.2
        float buffer[] = new float[((int) (timeInterval * SAMPLING_RATE)) / 1000];

        //Size of buffer1[], which in case of 2 sec is 88.2
        float buffer1[] = new float[((int) (timeInterval * SAMPLING_RATE)) / 1000];

        for (int sample = 0; sample < buffer.length; sample++) {    //range from zero to buffer.length
            double cycle = sample / SAMPLING_RATE;                  //Fraction of cycle between samples
            buffer[sample] = (float) (Math.sin(2 * Math.PI * frequency1 * cycle));  //value at every point of the cycle
        }
        for (int sample = 0; sample < buffer1.length; sample++) {
            double cycle = sample / SAMPLING_RATE;                  //Fraction of cycle between samples
            buffer1[sample] = (float) (Math.sin(2 * Math.PI * frequency2 * cycle));
        }

        byte byteBuffer[] = new byte[buffer.length * 2];            //Size of byteBuffer
        byte byteBuffer1[] = new byte[buffer1.length * 2];          //Size of byteBuffer1

        int count = 0;

        //Analog to digital
        for (int i = 0; i < byteBuffer.length; i++) {
            int x = (int) (buffer[count++] * Short.MAX_VALUE);
            byteBuffer[i++] = (byte) x;
            byteBuffer[i] = (byte) (x / 256);
        }

        count = 0;
        for (int i = 0; i < byteBuffer1.length; i++) {
            int x = (int) (buffer1[count++] * Short.MAX_VALUE);
            byteBuffer1[i++] = (byte) x;
            byteBuffer1[i] = (byte) (x / 256);
        }

        byte[] merge = new byte[8 * byteBuffer.length];    //Merged Array's length

        //Merging the two frequencies into one. Where there is '0' adding 1st frequency and in case of '1' adding 2nd
        for (int i = 0; i < 8; i++) {               //Loop for 8 Bits
            int c = value & binVal;                 //'&' operation to check whether 'c' contains zero or not in every iteration
            if (c == 0) {
                System.arraycopy(byteBuffer, 0, merge, i * (byteBuffer.length), byteBuffer.length);   //Adds 1st frequency
            } else {
                System.arraycopy(byteBuffer1, 0, merge, i * (byteBuffer.length), byteBuffer1.length); //Adds 2nd frequency
            }
            binVal = binVal >> 1;                           //Right Shifting the value of 'binVal' to be used for 'c'
        }

        File out = new File("E:/RecordAudio30.wav"); //The path where user want the file data to be written

        //Construct an audio format, using 44100hz sampling rate, 16 bit samples, mono, and big 
        // endian byte ordering
        AudioFormat format = new AudioFormat((float) SAMPLING_RATE, 16, 1, true, false);

        // It uses 'merge' as its buffer array that contains bytes that may be read from the stream.
        ByteArrayInputStream bais = new ByteArrayInputStream(merge);

        //Constructs an audio input stream that has the requested format and length in sample frames, using audio data 
        //from the specified input stream.
        AudioInputStream audioInputStream = new AudioInputStream(bais, format, (long) (8 * (byteBuffer.length / 2)));

        //Writes a stream of bytes representing an audio file of the specified file type to the external file provided.
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, out);

        audioInputStream.close();       //Closes this audio input stream
    }
}
Vibhav Chaddha
  • 431
  • 7
  • 15
  • I'm unclear what is being asked. – Phil Freihofner Dec 29 '16 at 01:55
  • Hi, @PhilFreihofner. I actually asked a question about 3 weeks back, but there was no response and I somewhat figured it on my own. Then I edited my question and wrote whatever I have done to help others. But if I'll get stuck somewhere in the future I will definitely be asking more questions. Thanks. – Vibhav Chaddha Dec 29 '16 at 11:24

0 Answers0