How to create a simple but well structured score table (score) in Java?

I'm using very basic sound synthesis to create audio and effects in my game Basically, I have some ways to make a frequency and frequency sound Amplitude and duration of amplitude

For phrases and melodies, I want to propose a basic symbol so that I can easily rewrite or add new melodies to the code (finally, maybe I can read them from the file, but this may be overkill)

I don't know how to achieve this

I first created an enumeration equaltemperamenttuning, which contains all 88 basic piano notes with midi# fields and frequency fields This at least means that I can handle note names instead of frequencies

public enum EqualTemperamentTuning {
    A_0         (1,27.5),A_SHARP_0   (2,29.1352),...
    C_8         (88,4186.01);

    private int num;
    private double frequency;

    EqualTemperamentTuning(int num,double frequency){

        this.num = num;
        this.frequency = frequency;
    }

    public double getFrequency(){
        return frequency;
    }

    public double getNum(){
        return num;
    }
}

Then I started to create more objects. The first is a note, which contains equaltemperamenttuning, an amplitude and a length:

public class Note {

    /** Note frequency */
    private double frequency;
    /** Note Amplitude */
    private double amplitude;
    /** Note length */
    private int length;     

    public Note(EqualTemperamentTuning tuning,double amplitude,int length){

        this.frequency = tuning.getFrequency();
        this.amplitude = amplitude;
        this.length = length;   
    }

    public double getFrequency(){
        return frequency;
    }

    public double getAmplitude(){
        return amplitude;
    }

    public int getLength(){
        return length;  
    }
}

Finally, in order to define the melody I want to play, I created a notephrase class:

public class NotePhrase {

    /** The arrayList of notes*/
    private Note[] notes;

    public NotePhrase(Note[] notes){

        this.notes = notes;
    }

    public double getFrequency(int counter){

        // for each note in the array
        for (int i = 0; i< notes.length; i++){

            // for each unit of length per note
            for (int j=0; j<notes[i].getLength(); j++){

                counter--;

                // return the frequency at this point
                if (counter <= 0) return notes[i].getFrequency();

            }
        }
        return -1;  
    }
}

Then in my audio generation class, I have a loop (with counter) that generates samples from the waveform generator Every time I need to play a new sample, it will be based on the notephase The getfrequency (int counter) method sets the frequency of the wave This should be (I haven't really tested it yet!) Just play notephrase melody according to frequency and amplitude (to be added)

The problem is that it doesn't look elegant. More specifically, it's very difficult to "write" the melody in any clear way I have to write a lot of new note objects and use their arrays to construct a notephrase object... It's not obvious to me how to hard code these melodies and then easily switch them later

I really want to create a melody or something like that. I can easily hard code human readable configurations for each different melody, and then when I want to use them, I just need to pass the enumeration type to the audio player

The best I get is:

private static enum Melody {
    NOKIA_ringtone ( new Note(EqualTemperamentTuning.E_5,0.5,1),new Note(EqualTemperamentTuning.D_5,1))
    ;

    private Note[] notes = new Note[2];

    Melody (Note note1,Note note2){
        this.notes[0] = note1;
        this.notes[1] = note2;
    }
}

Then I will load it into the notephase object at run time This is not very good because I have to create a new constructor for melodies with different note quantities If I do it in the opposite way and construct an enumeration with a series of notes, then I just "write" the melody elsewhere, and these two parts look more confusing

So I insist on how to build it correctly? That is, what classes should be created and what information should be retained... I want this "correct" because I may want to expand the symbols containing effects (echo, etc.) in the future. I have found very little experience. The correct classes, structures and relationships (even names) can make my program very easy or difficult to understand

Sorry for this article, this may not be a very good (cough) problem, but as a Java & OOP beginner, any tips are very welcome!

Edit**

Thank you for your answer & suggestion, which is very helpful Considering the answer given in this case, let me rethink my general audio implementation, which is now very unstable I don't know who I should mark as correct, because I really just put all my suggestions on the boat and try to start there

Solution

Do not use enumerations for melody, because melody does not represent a real constant, but a collection of note data I recommend not using arrays, but using ArrayList < < note >, which is more flexible

In melody, I add note to measure and beat, and assign melody class to addnote (note, int measure, int beatfraction), which is also used for remove note

Consider enumerating 12 core comments and then using them plus an octave int to create a note object

... there are many ways to play this exercise The key is to continue the experiment

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>