Java是否内置了用于音频_合成_的库?

2022-09-02 05:12:35

注意:我不想“foo.bar 读取音频文件并播放它”。

我想以编程方式动态生成音频文件并播放它们。

Java是否为此内置了库,或者这是否属于依赖于系统的库?

谢谢!


答案 1

使用安德鲁的方法,这里有一个玩平等温和量表的例子。

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class Tone {

    public static void main(String[] args) throws LineUnavailableException {
        final AudioFormat af =
            new AudioFormat(Note.SAMPLE_RATE, 8, 1, true, true);
        SourceDataLine line = AudioSystem.getSourceDataLine(af);
        line.open(af, Note.SAMPLE_RATE);
        line.start();
        for  (Note n : Note.values()) {
            play(line, n, 500);
            play(line, Note.REST, 10);
        }
        line.drain();
        line.close();
    }

    private static void play(SourceDataLine line, Note note, int ms) {
        ms = Math.min(ms, Note.SECONDS * 1000);
        int length = Note.SAMPLE_RATE * ms / 1000;
        int count = line.write(note.data(), 0, length);
    }
}

enum Note {

    REST, A4, A4$, B4, C4, C4$, D4, D4$, E4, F4, F4$, G4, G4$, A5;
    public static final int SAMPLE_RATE = 16 * 1024; // ~16KHz
    public static final int SECONDS = 2;
    private byte[] sin = new byte[SECONDS * SAMPLE_RATE];

    Note() {
        int n = this.ordinal();
        if (n > 0) {
            double exp = ((double) n - 1) / 12d;
            double f = 440d * Math.pow(2d, exp);
            for (int i = 0; i < sin.length; i++) {
                double period = (double)SAMPLE_RATE / f;
                double angle = 2.0 * Math.PI * i / period;
                sin[i] = (byte)(Math.sin(angle) * 127f);
            }
        }
    }

    public byte[] data() {
        return sin;
    }
}

这种低级方法可能适用于较旧的、功能较差的平台。还要考虑javax.sound.midi;这里显示了一个完整的示例,这里引用了合成声音教程。


答案 2

最简单的方法是在内置的MIDI库中使用java:

int channel = 0; // 0 is a piano, 9 is percussion, other channels are for other instruments

    int volume = 80; // between 0 et 127
    int duration = 200; // in milliseconds

    try {
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();
        MidiChannel[] channels = synth.getChannels();

        // --------------------------------------
        // Play a few notes.
        // The two arguments to the noteOn() method are:
        // "MIDI note number" (pitch of the note),
        // and "velocity" (i.e., volume, or intensity).
        // Each of these arguments is between 0 and 127.
        channels[channel].noteOn( 60, volume ); // C note
        Thread.sleep( duration );
        channels[channel].noteOff( 60 );
        channels[channel].noteOn( 62, volume ); // D note
        Thread.sleep( duration );
        channels[channel].noteOff( 62 );
        channels[channel].noteOn( 64, volume ); // E note
        Thread.sleep( duration );
        channels[channel].noteOff( 64 );

        Thread.sleep( 500 );

        // --------------------------------------
        // Play a C major chord.
        channels[channel].noteOn( 60, volume ); // C
        channels[channel].noteOn( 64, volume ); // E
        channels[channel].noteOn( 67, volume ); // G
        Thread.sleep( 3000 );
        channels[channel].allNotesOff();
        Thread.sleep( 500 );



        synth.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

推荐