java 在java中播放单个频率的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2991969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:50:58  来源:igfitidea点击:

Simple way to play a single frequency in java?

javaaudio

提问by nathas

I just want to play a very simple, straight forward note by giving my computer a certain frequency as an integer, and from there I can figure out how to make it play the note longer or shorter. It does not necessarily have to come out of the actual sound card - if it's generated and output by the internal speaker that's okay.

我只想通过给我的电脑一个整数频率来演奏一个非常简单、直接的音符,然后我可以弄清楚如何让它播放更长或更短的音符。它不一定必须来自实际的声卡 - 如果它是由内部扬声器生成和输出的,那没关系。

I looked at the midi libraries that java has included, and they are way more than what I want to do. This just needs to be very basic.

我查看了 java 包含的 midi 库,它们比我想做的要多得多。这只是需要非常基本的。

采纳答案by trashgod

This exampleconstructs and plays an equal tempered scale.

这个例子构建并演奏了一个均等的音阶

回答by Alex Feinman

Look into JFugue-- it's really easy to do some basic stuff, and the capabilities are there if you want to expand later.

看看JFugue—— 做一些基本的东西真的很容易,如果你以后想扩展,这些功能就在那里。

Player player = new Player();
player.play("A C# E");

回答by Durandal

As far as i know there is no way to do this without some boilerplate code in Java. The most simple API is probably provided by the Applet class (can be used by non-Applets as well) in the form of the static newAudioClip(URL url)-method. However this gives you just the ability to play predefined audio clips and you have very little control over the audio. If you just need to play audio from a small, predefined set of clips it might suffice (you could have a set of wav-files containing your notes and play them this way, AudioClip's can be looped if desired).

据我所知,如果没有 Java 中的样板代码,就无法做到这一点。最简单的 API 可能由 Applet 类(也可以由非 Applet 使用)以静态 newAudioClip(URL url) 方法的形式提供。但是,这使您只能播放预定义的音频剪辑,而您几乎无法控制音频。如果您只需要从一组预定义的小片段中播放音频就足够了(您可以拥有一组包含您的笔记的 wav 文件并以这种方式播放,如果需要,可以循环播放音频片段)。

Other than that, both midi and sampled audio API's are much more powerful, but the flexibility comes at a price: You need considerably more code to set them up.

除此之外,MIDI 和采样音频 API 都更强大,但灵活性是有代价的:您需要更多的代码来设置它们。