0
public static ArrayList<Chord> createRisingChordList(double duration, double[] frequencies, int count){
        ArrayList<Chord> chordList = new ArrayList<Chord>();
        double multiplier = 1.224;


        for(int i = 0; i < count; i++){

            Chord chord = new Chord(duration, frequencies);
            chordList.add(chord);

            frequencies[0] *= multiplier;
            frequencies[1] *= multiplier;
            frequencies[2] *= multiplier;

        }


        for(int x = 0; x < count; x++){

            chordList.get(x).play();

        }

        return chordList;
    }

///

double[] frequencies = {110.0, 110.0*1.224, 110.0*1.224*1.224};
        ArrayList<Chord> risingChords = Final.createRisingChordList(0.2, frequencies, 10);
        for (Chord risingChord: risingChords) {
            StdOut.println("Playing: " + risingChord);
            risingChord.play();
        }

Why would my output be this:

Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]

I can confirm that it has the right objects to add it just replaces them for some reason. Here is the output of the array after i add each object.

The updated List after: 0 iterations


[0.2:  134.64 164.79935999999998 201.71441663999997]
The updated List after: 1 iterations


[0.2:  164.79935999999998 201.71441663999997 246.89844596735995]
[0.2:  164.79935999999998 201.71441663999997 246.89844596735995]
The updated List after: 2 iterations


[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
The updated List after: 3 iterations


[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]

Etc
etc
etc

I dont have static fields in my chords class from what I understand

chord Class:

public class Chord {
     double duration, frequencies[];

    public Chord(double duration, double[] frequencies) {

        this.duration = duration;
        this.frequencies = frequencies;

    } 

    public void play(){

        playChord(duration, frequencies);

    }

    public String toString(){
        String freq = "";

        for(int i = 0; i < frequencies.length; i++){

            freq = freq + " " + frequencies[i];
        }

        return "[" + duration + ": " + freq + "]";
    }

    private void playChord(double duration, double[] frequencies) {
        final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
        final double[] slices = new double[sliceCount+1];
        for (int i = 0; i <= sliceCount; i++) {
            double chord = 0.0;
            for (double frequency: frequencies) {
                chord += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);
            }
            slices[i] = chord/frequencies.length;
        }
        StdAudio.play(slices);
    }

}
Danstett14
  • 31
  • 1
  • 6
  • 2
    I'm going to guess you have static fields in your Chord class, and you're simply overwriting them each time you make a new object. static == only one instance, ever. – Rogue Jun 08 '17 at 01:07
  • I added my chord class I don't seem to understand sorry – Danstett14 Jun 08 '17 at 01:11
  • In `createRisingChordList(double duration, double[] frequencies, int count)` you create `Chord` in a way `new Chord(duration, frequencies)`, so you are passing to *all* `Chord` instances ***same*** `frequencies` array which you modify in loop, but since all `Chord` are using same array they all see its latest version. – Pshemo Jun 08 '17 at 01:16
  • So what would be the solution putting new chord(duration, frequencies) above the loop and change the frequencies of that specific chord? – Danstett14 Jun 08 '17 at 01:26
  • Solution depends on what you want to achieve. I am guessing that in each iteration you could create copy of current state of `frequencies` array like `double[] frequenciesCopy = Arrays.copyOf(frequencies, frequencies.length);` then pass that copy instead of original like `Chord chord = new Chord(duration, frequenciesCopy);`. This way you can modify original array for next iteration. – Pshemo Jun 08 '17 at 01:33
  • thanks that worked – Danstett14 Jun 08 '17 at 01:39

0 Answers0