Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Procedural music  (Read 2033 times)
0 Members and 1 Guest are viewing this topic.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Posted 2009-11-02 09:21:50 »

Hi  Grin

The Java4k seems to getting start and so I. This year, I really want to try to put some music in my game.
Midi synthetizer seems to be a good choise since there is everything that I need and you only need a few line of code :

1  
2  
3  
4  
5  
6  
7  
8  
     synth = MidiSystem.getSynthesizer();
     synth.open();
     synth.getChannels()[0].programChange(XX);

     ...

    synth.getChannels()[0].allNotesOff();
    synth.getChannels()[0].noteOn( XX,YY);


Now my problem is the music data. I don't think I can store raw data like that (it will take too much space). So I want to use an algorithm to produce procedural music. But I'm not use to those sort of things (and I'm no good at music  Tongue).

While googling, I found some interresting site :
Fractal melody
Fractal Music
Tune Toys (I have play a lot with the exemples  Grin)

Even so I didn't manage to do anything good (in a small size). Does anyone have some expercience in this field ?
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2009-11-16 09:59:33 »

Not much luck on this suject...

I have another problem related to this. I use the Synthetizer class to deal with the MIDI ouput. But not all device have one. I have found that a Synthetizer can be faked with a Receiver :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
import java.util.List;
import javax.sound.midi.Instrument;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiDevice.Info;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Patch;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Soundbank;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.Transmitter;
import javax.sound.midi.VoiceStatus;

/**
 *
 * @author
 */

public class GenericSynthetizer implements Synthesizer
{
  protected MidiDevice device;
  protected GenericMidiChannel channels[];

  public GenericSynthetizer(MidiDevice device) throws MidiUnavailableException
  {
    this.device = device;

    device.open();

    try
    {
      ShortMessage message = new ShortMessage();
      message.setMessage(ShortMessage.START);
      device.getReceiver().send(message,0);
    }
    catch (InvalidMidiDataException ex)
    {
      ex.printStackTrace();
    }

    channels = new GenericMidiChannel[16];
    for(int i=0;i<16;i++)
    {
      channels[i] = new GenericMidiChannel(i,device,device.getReceiver());
    }
  }

  public int getMaxPolyphony()
  {
    return 16;
  }

  public long getLatency()
  {
    return 0;
  }

  public MidiChannel[] getChannels()
  {
    return channels;
  }

  public VoiceStatus[] getVoiceStatus()
  {
    return null;
  }

  public boolean isSoundbankSupported(Soundbank soundbank)
  {
    return false;
  }

  public boolean loadInstrument(Instrument instrument)
  {
    return true;
  }

  public void unloadInstrument(Instrument instrument)
  {
   
  }

  public boolean remapInstrument(Instrument from, Instrument to)
  {
    return true;
  }

  public Soundbank getDefaultSoundbank()
  {
    return null;
  }

  public Instrument[] getAvailableInstruments()
  {
    return null;
  }

  public Instrument[] getLoadedInstruments()
  {
    return null;
  }

  public boolean loadAllInstruments(Soundbank soundbank)
  {
    return true;
  }

  public void unloadAllInstruments(Soundbank soundbank)
  {
     
  }

  public boolean loadInstruments(Soundbank soundbank, Patch[] patchList)
  {
    return true;
  }

  public void unloadInstruments(Soundbank soundbank, Patch[] patchList)
  {

  }

  public Info getDeviceInfo()
  {
    return device.getDeviceInfo();
  }

  public void open() throws MidiUnavailableException
  {
    device.open();
  }

  public void close()
  {
    device.close();
  }

  public boolean isOpen()
  {
    return device.isOpen();
  }

  public long getMicrosecondPosition()
  {
    return device.getMicrosecondPosition();
  }

  public int getMaxReceivers()
  {
    return device.getMaxReceivers();
  }

  public int getMaxTransmitters()
  {
    return device.getMaxTransmitters();
  }

  public Receiver getReceiver() throws MidiUnavailableException
  {
    return device.getReceiver();
  }

  public List<Receiver> getReceivers()
  {
    return device.getReceivers();
  }

  public Transmitter getTransmitter() throws MidiUnavailableException
  {
    return device.getTransmitter();
  }

  public List<Transmitter> getTransmitters()
  {
    return device.getTransmitters();
  }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;

/**
 *
 * @author
 */

public class GenericMidiChannel implements MidiChannel
{
  protected MidiDevice device;
  protected Receiver receiver;
  protected int nb;
  protected ShortMessage message;

  public GenericMidiChannel(int nb,MidiDevice device,Receiver receiver)
  {
    this.nb = nb;
    this.device = device;
    this.receiver = receiver;

    message = new ShortMessage();
  }

  public void noteOn(int noteNumber, int velocity)
  {
    try
    {
      message.setMessage(ShortMessage.NOTE_ON, nb, noteNumber, velocity);
      receiver.send(message, 0);
    }
    catch (InvalidMidiDataException ex)
    {
      ex.printStackTrace();
    }
  }

  public void noteOff(int noteNumber, int velocity)
  {
    try
    {
      message.setMessage(ShortMessage.NOTE_OFF, nb, noteNumber, velocity);
      receiver.send(message, 0);
    }
    catch (InvalidMidiDataException ex)
    {
      ex.printStackTrace();
    }
  }

  public void noteOff(int noteNumber)
  {

  }

  public void setPolyPressure(int noteNumber, int pressure)
  {
       throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getPolyPressure(int noteNumber)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setChannelPressure(int pressure)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getChannelPressure()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void controlChange(int controller, int value)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getController(int controller)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void programChange(int program)
  {
    try
    {
      message.setMessage(ShortMessage.PROGRAM_CHANGE, nb, program, 0);
      receiver.send(message,0);
    }
    catch (InvalidMidiDataException ex)
    {
      ex.printStackTrace();
    }
  }

  public void programChange(int bank, int program)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getProgram()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setPitchBend(int bend)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getPitchBend()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void resetAllControllers()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void allNotesOff()
  {
    try
    {
      message.setMessage(ShortMessage.CONTROL_CHANGE, nb, 123, 0);
      receiver.send(message, 0);
    }
    catch (InvalidMidiDataException ex)
    {
      ex.printStackTrace();
    }
  }

  public void allSoundOff()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean localControl(boolean on)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setMono(boolean on)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getMono()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setOmni(boolean on)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getOmni()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setMute(boolean mute)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getMute()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setSolo(boolean soloState)
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getSolo()
  {
        throw new UnsupportedOperationException("Not supported yet.");
  }
}


Until now, I only use noteOn, noteOff, programChange and allNoteOff. It's work with the Java Sound Synthetizer and with the Microsoft wave table but it doesn't with the Timidity driver (whish is the one that I want to use ofcourse  Undecided).
I don't get any error, there is just no sound.

Any idea ?  Huh
Offline pjt33
« Reply #2 - Posted 2009-11-16 11:21:08 »

Hi  Grin

The Java4k seems to getting start and so I. This year, I really want to try to put some music in my game.
Midi synthetizer seems to be a good choise since there is everything that I need and you only need a few line of code :
Unless the rules have changed since last year:

Quote
No soundbanks may be used because they are not a default part of the "public" JRE. You will have to create your sounds at runtime rather than use MIDIs.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #3 - Posted 2009-11-18 07:46:51 »

Unless the rules have changed since last year:


Arf... thanks Sad. Even so, I still work on it (It became a full "project" by itself). For the 4k, I will see with basic sin sound but I don't know if I will have enough bytes left.

It really annoy me to not be able to use Timidity driver  Cry.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (38 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!