栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

使用(Andrew)的方法,下面是一个示例,该示例具有相同的调节范围。

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
; 一个完整的例子显示在这里和声音合成教程引用在这里。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号