为了账号安全,请及时绑定邮箱和手机立即绑定

Java是否为音频_synthesis_内置了库?

Java是否为音频_synthesis_内置了库?

温温酱 2019-10-21 09:28:35
注意:我不想“读取音频文件foo.bar并播放它”。我想以编程方式即时生成音频文件并进行播放。Java是否为此内置了库,还是属于系统依赖的库?谢谢!
查看完整描述

3 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

此Sun论坛帖子中包含一些有趣的代码,用于生成单音。同样,鉴于WAV文件格式不是太复杂,您可以创建一个代表所需波形的表,然后将其写入文件。周围有一些示例,例如原始音频转换器以及如何编写wav文件。


查看完整回答
反对 回复 2019-10-21
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

最简单的方法是使用Java的内置MIDI库:


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();

    }


查看完整回答
反对 回复 2019-10-21
  • 3 回答
  • 0 关注
  • 479 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信