简体   繁体   English

如何将音符名称和频率存储为数据以在 java 中的其他类中使用

[英]How to store note names and frequencies as data to use in other classes in java

I want to build some sort of array that will store the note names eg A4 and it's frequency eg 440 I also want to know what sort of class I should use to do this.我想构建某种数组来存储音符名称,例如 A4,它的频率例如 440 我还想知道我应该使用什么样的类来做到这一点。

I want to be able to use a wave generator class to generate that note as a waveform like a Sine or square wave.我希望能够使用波形发生器类将该音符生成为像正弦波或方波这样的波形。 Example例子

public SineGenerator(double amplitude, Note note, int bitRate, double duration) {
SineGenerator sg = new SineGenerator(0.8, C4(or middle C, 261.63Hz), 16, 1);

The Note class will index C4 in some sort of array and then return Middle C's frequency, 261.63 to the SineGenerator and SineGenerator will generate the sine wave. Note 类将在某种数组中索引 C4,然后将 Middle C 的频率 261.63 返回给 SineGenerator,SineGenerator 将生成正弦波。

For storing the note names and their respective frequency, look up HashMaps, it sounds like what you're looking for.要存储音符名称及其各自的频率,请查找 HashMaps,这听起来像您要查找的内容。 Use the note names as Key and the frequencies as Value.使用音符名称作为键,使用频率作为值。

HashMap<String, Double> Notes = new HashMap<>();

If I understood correctly, you are asking for a rough guide of how to implement the Note class.如果我理解正确,您是在寻求有关如何实现Note类的粗略指南。

The Note class will probably contain 3 main fields: Note类可能包含 3 个主要字段:

private char letter;
private int octave;
private boolean sharp;

A note such as D♭ 4 will have a letter of C , octave of 4 and a sharp of true .的说明如d♭4将具有信Coctave4和一个sharptrue As you can see, I am representing D♭ and C# as the same note here, as they have the same frequency, and the only thing of importance here seems to be the frequency.如您所见,我在这里将 D♭ 和 C# 表示为相同的音符,因为它们具有相同的频率,而且这里唯一重要的似乎是频率。

Note will also have a HashMap<String, Double> or HashMap<String, Integer> to store the frequency of each note. Note还会有一个HashMap<String, Double>HashMap<String, Integer>来存储每个音符的频率。 Note that you don't need to store the semitones in this map, as you can just multiply the note below by 1.0595 (or Math.pow(2, 1.0 / 12) ) to get the semitone frequency.请注意,您不需要在此映射中存储半音,因为您只需将下面的音符乘以 1.0595(或Math.pow(2, 1.0 / 12) )即可获得半音频率。

And then you'd write a method called getFrequency , which accesses the map.然后您将编写一个名为getFrequency的方法,该方法访问地图。 If sharp is true, multiply it by 1.0595 and return.如果sharp为真,则乘以1.0595 并返回。

Here's a simple implementation that I have written:这是我编写的一个简单实现:

class Note {
    private char letter;
    private int octave;
    private boolean sharp;

    private static HashMap<String, Integer> map;

    static {
        map = new HashMap<>();
        map.put("C0", 16);
        map.put("D0", 18);
        map.put("E0", 21);
        map.put("F0", 22);
        map.put("G0", 25);
        map.put("A1", 28);
        // ...
    }

    public Note(char letter, int octave, boolean sharp) {
        this.letter = letter;
        this.octave = octave;
        this.sharp = sharp;
    }

    public char getLetter() {
        return letter;
    }

    public int getOctave() {
        return octave;
    }

    public boolean isSharp() {
        return sharp;
    }

    public double getFrequency() {
        String key = Character.toString(letter) + octave;
        double frequency = map.get(key);
        if (sharp) {
            frequency *= Math.pow(2, 1.0 / 12);
        }
        return frequency;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM