0

I am trying to play a list of music strings using a for loop.

While the first music string is played correctly along with the base, the second and the subsequent do not play simultaneously.

Am i missing something here?

/**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {
        List<String> musicStringList = new ArrayList<>();

        musicStringList.add("G5i Ri G5i Ri G5i Ri G5i Ri G5i Ri G5i Ri G5i Ri G5i Ri");
        musicStringList.add("D5i Ri D5i Ri D5i Ri D5i Ri D5i Ri D5i Ri D5i Ri D5i Ri");
        musicStringList.add("E5i Ri E5i Ri E5i Ri E5i Ri E5i Ri E5i Ri E5i Ri E5i Ri");

        Player player = new Player();
        String base = "Cmajw Gmajw";
        for (int i = 0; i < musicStringList.size(); i++) {
            List<Pattern> patterns = new ArrayList<>();
            String voice = musicStringList.get(i);
            Pattern p0 = new Pattern(voice);
            p0.setInstrument("Flute");
            Pattern p1 = new Pattern(base);
            p1.setInstrument("Piano");
            patterns.add(p0);
            patterns.add(p1);
            for (int j = 0; j < patterns.size(); j++) {
                patterns.get(j).setVoice(j);
            }
            Pattern[] patternsArr = patterns.toArray(new Pattern[0]);
            player.play(patternsArr);
        }
    }
BabaNew
  • 884
  • 1
  • 13
  • 27
  • You can use less code by using the V and I tokens in your strings rather than setting the instrument and voice on patterns. For example: String base = "V0 I[Piano] Cmajw Gmajw"; and musicStringList.add("V1 I[Flute] G5i Ri G5i Ri G5i Ri"); I'd also suggest printing the strings that you're generating to make sure they look right to you. For example, you can check for missing spaces between voices. – David Koelle Jan 20 '23 at 20:35

0 Answers0