Reading music from the sound card

I'm creating MIDI music using a synthesizer, sequencer, and sequence. I'm trying to capture the music (not the MIDI messages) as it's being played so then I have all the data and I can do some Fourier analysis on it. The documentation revolves itself around having music coming in from the microphone, or other ports so what I'm trying to figure out is how to listen in on one of the ports so I can capture the data as it's playing. It all seems confusing as if you can't listen to the music that you're playing? The way I'm playing the music is just using sequencer.start() and then having the thread sleep for a couple of minutes. I understand that method isn't going to work and I'm going to have to multi-thread this, but right now I don't understand how to read in the data while it's being played.

Re: Reading music from the sound card

I'm still not certain what you are trying to do. I know for a fact that you can play a MIDI file and see the MIDI commands as they are processed. But since you said you don't want the MIDI messages, exactly what do you want?

Re: Reading music from the sound card

The MIDI messages have to be going somewhere that translates a noteOn command into play this instrument for this long with this intensity. That translation then goes somewhere and eventually into sound. I want to grab the data that becomes the "sound" after the MIDI message gets processed. I want to read in all the data then perform some Digital Signal Processing (DSP) particularly a DFT or FFT. Helloooo spectral analysis!

I also don't want to read in a MIDI file because I've found the music I play in real time (aka. calling sequencer.play()) is more accurate to what the music should sound like compared to its MIDI file format. Also I need to do trial runs and having to generate and handle MIDI files is going to be more cumbersome and I don't want to create that many MIDI files.

So it comes down to how do I grab/listen/read the actual music data that gets created from a MIDI message and gets played off of the sound card? As long as it's the actual music data it doesn't have to come off the sound card. I'm just assuming I'm going to have to listen to the port that's playing the music and I don't know how to set that up because all of the examples I've found are about doing this with MIDI files or using some input port for a keyboard or microphone.

Re: Reading music from the sound card

I am not familiar with accessing the actual "data" this is translated to sound. I assume it would be some n bit signal that is generated. That level of information may be within the native support for the MIDI classes. You might look into the Java Media Framework to see if it offers any additional help. If you just want to capture what is coming over the speakers, I have been using TotalRecorder for years. They have a great deal on lifetime updates for the level of version you purchase. And it is relatively inexpensive. It is made by High Criteria. It will store the music in a variety of formats. If you know the format you could use Java to read in the file and do your analysis.

Re: Reading music from the sound card

Again I don't want to have to deal with any files of any type. I only want to deal with files if I'm going to listen to it which I do so people can listen to what I'm creating.

I did found something worthwhile. Pretty much my music starts like this Trail: Sound (The Java™ Tutorials) . Look for the picture with the sequencer and synthesizer. The music gets sent to "sampled audio system" and that's where anything MIDI related ends. The rest of the process uses AudioSystem and right now I'm using this Wired for sound - Java tutorial - developer Fusion to figure out the logical flow of data. If you're still following along since I want to gather MIDI music generated by the PC I'm using Figure 4 to figure out what I need set up in order to capture the data and HOPEFULLY that will do it.

Then comes setting up a thread to listen and gather the music as it's being played while another thread is playing the music along with then notifying, closing, ending and stopping threads and other resources I release once the music is done playing. This part I'll figure out.

Re: Reading music from the sound card

I think (and this is not something I've done, only done the MIDI side of things) that you use the bits in the sampled package to listen in on audio coming out of the hardware. In your case this should be the MIDI. So you'd have one thread generating the MIDI which is fed into the PC hardware, and another thread listening in on the audio being output from the hardware.

So my first step would be to create something that simply listens in on audio being played (the javax.sound.sampled package), even if it's just a wav.

Re: Reading music from the sound card

ANSWER
I found on grepcode.com that there was an AudioSynthesizer interface that allows one to render output audio straight into an AudioInputStream or SourceDataLine, but couldn't figure out how to use it correctly. I later found through a friend that JFugue allows you to save MIDI music as a .wav file. On further inspection it implements the AudioSynthesizer interface and allowed me to convert my MIDI music straight into audio data using an AudioInputStream. I then had to read in from the buffer 4 bytes at a time because that's all the buffer supported and now I had my data. Sort of. Turns out since my audio is 16 bit I needed to take 2 bytes at a time and do some logical operations and byte shifting to get the amplitudes of the data. When I graphed this data it now looks like the waveform of the music. The good thing is when I format the audio data the result is stored into a double and now I just need to store this into a double [] and I'll be set to perform my FFT.