Post One: Tracking And Recording Sound Input

     In this blog we will be exploring audio programming. The end goal is to be able to take input from an instrument through and audio interface and to manipulate it. Personally, I am running a guitar through a Focusrite Scarlett 2i2 (third gen) audio interface. It has two inputs that can be used to either plug in an instrument through a quarter-inch jack or a microphone through an XLR cable. If you decide to follow along and attempt to recreate the things I am experimenting with here you will need to ensure you have your default audio device set to whatever audio interface you choose so that you can plug in an instrument or microphone. Otherwise you should be able to use the code I post and just use your built-in microphone to capture audio. This will allow you to play around and find your own way to apply this knowledge.

    For the setup phase of this project I have referenced a video that explains how to set up a project with the Java Sound API that will read and record audio input from your device. The video can be found here: https://www.youtube.com/watch?v=PTs01qr9RlY&ab_channel=GangapatnamAnil. I will walk through the important elements of the code and share some information about what is going on. 

     First, we need to discuss AudioFormat. The constructor I used takes seven arguments. The first parameter is an AudioFormat.encoding. Right now it is alright to default to using PCM_SIGNED. There are other options here as well which I may end up diving into in the future. The second parameter is the sample rate. When creating a digital representation of a sound there is no way to completely accurately recreate the recorded sound. In order to get an accurate representation of the sound you need to take enough “samples” of the signal to closely represent the original. Sample rate is the number of samples of the signal taken per second. The number recommended in the video linked above will work, but you can try experimenting with this number and see what happens to the resulting audio file. The next parameter is the sample size in bits, which can just be left as is. Next is the number of channels. For me this will be two because my audio interface has two channels. You will also probably need to choose two channels if you are testing with your standard audio. The rest of the arguments should be left as is for now.

After this, it is a good idea to check that your audio system is line supported. You can see this in the code example. The code example also has an instance of DataLine.Info. A DataLine.Info object will store a type of line DataLine tp be used later. In this case we are using TargetDataLine as this the type of DataLine from which audio can be read and recorded. Once this TargetDataLine is instantiated it will need to be opened. 

The example program from the video has a dialog box that appears and is used to start a thread that will record incoming audio. This kind of application needs to be threaded so that audio can be sampled continuously regardless of what other operations are being done (GUI for example). The run method of the thread will need to be overridden and in it there are a few things that need to be done. First, a file location needs to be specified for where to write the recorded audio file. Second, an AudioInputStream must be instantiated with the TargetDataLine passed into it as an argument. AudioInputStream allows us to read and operate on the bytes of the audio input. Finally, we need to call the read method on our AudioInputStream so that we can listen for incoming audio. AudioSystem.write will write this audio to the specified file location. All of this needs to be surrounded in a try/catch block so we can catch an IOException if it occurs since we are taking input and writing it to a new file.

At the end of this process, if you follow the code from the video, you will be able to record yourself speaking or playing an instrument. From here we will start to explore manipulating the audio data to come up with interesting sounds. In my case I want to create effects for my guitar to make it sound more interesting, but anyone will be able to use this information to manipulate any audio input they want to.


Comments

  1. Sounds interesting. I've done a little bit with sound in Java before, but I was using Clips rather than an AudioInputStream.

    Btw, something went weird with your formatting for the last four paragraphs.

    ReplyDelete
  2. The video you provided was pretty useful, and I didn't expect it to be that easy to record with Java. I'm excited to see in what ways audio can be played with.

    ReplyDelete

Post a Comment

Popular posts from this blog

Detour: A Simple White Noise Generator

Introduction To Juce DSP Module: Building An Oscillator

Conclusion