Jaca
Jaca

Reputation: 197

How to generate audio in java

This question has to do with a game console I'm working on, and right now I am trying to write a Java program to simulate the console's DSP. That way, I know exactly what to do when I port it to actual hardware. But I am having trouble finding the right sound library. What I need is basically this: I have my own sound format, and I feed it into the DSP. It then decodes the data, processes post decode effects (echo, amplify, etc.), and splits the results into two sound waves for stereo output. I have everything planned out except a way to get my sound waves to my computer's sound card. So basically a more advanced version of a sine wave generator. And a little code sample to get me started would help. If I need to clarify anything, than let me know.

EDIT: Okay, so just to be clear the sound wave data will be stored in chunks in a byte array. So I need a way of playing sounds from there. And I don't want to dump the audio to a file and then play the file, that would take too long.

Upvotes: 1

Views: 3050

Answers (2)

d_air
d_air

Reputation: 641

Here is one way to generate a sound using java.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.midi.Instrument;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Synthesizer;
import javax.swing.*;

public class SimpleSound extends JFrame{
    Synthesizer syn;
    MidiChannel[] midChannel;
    Instrument[] instrument;

    public SimpleSound() {
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JButton button1 = new JButton("Try");
        this.add(panel);
        panel.add(button1);
        this.pack();

        try {
            syn = MidiSystem.getSynthesizer();
            syn.open();
            midChannel = syn.getChannels();

            instrument = syn.getDefaultSoundbank().getInstruments();
            syn.loadInstrument(instrument[90]);

            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    makeASound();
                }
            });

        } catch (MidiUnavailableException ex) {
            ex.printStackTrace();
        }

    }

    void makeASound() {
        this.midChannel[5].noteOn(55,550);
//       this.midChannel[5].noteOn(70,700);
//       this.midChannel[5].noteOn(30,400);
    }

    public static void main(String[] args) {
        new SimpleSound().setVisible(true);
    }
}

You can experiment on the values in the code this.midChannel[5].noteOn(55,550);

You can find more explanations here: http://patater.com/gbaguy/javamidi.htm and here: https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html

Update:

I found another source from here http://www.automatic-pilot.com/midifile.html. It is a simple program demonstrating the creation of MIDI sound and then save it to a file. I made a modification to the last part so that the sound will be written to a byte array instead.

     // write to byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        MidiSystem.write(s,1,baos);

        byte[] bytes = baos.toByteArray();

But I just think that the byte array contents may not have the same format as you what you already have in mind. May I know what is the format of the sound data that is usable to you?

Upvotes: 1

Related Questions