Pereiti prie turinio

Java - Threads ir Midi garsai veikia per lėtai


Rekomenduojami pranešimai

Sveiki,

 

Taigi turiu programą, kuri veikia lyg sound machine - eina rodyklė per 16 stulpelių ir groja garsus kur paspausti mygtukai. Tačiau veikia per lėtai, vienu metu gali groti iki 256 garsų, todėl kuriu po naują giją (thread) kiekvienam garsui, tačiau iškart girdisi atsilikimas ir nestabilumas.

 

Ar būtų patarimų, kaip padaryt šitą greičiau? Pridedu kodą kaip kuriami garsai

 

public void playSound(int note, int velocity, int layerIndex) {
	int instrument = SimoriOn.getInstance().getLayer(layerIndex)
			.getCurrentInstrument();

	Sound sound;
	// Check if instrument is percussion
	if (instrument > 127) {
		// If instrument number is higher than 127, that means it's a
		// percussion instrument
		// Change instrument into a note and change which channel to use
		note = instrument - 127;
		sound = new Sound(this.synth,
				this.midiChannels[PERCUSSION_CHANNEL], instrument, note,
				velocity);
	} else {
		sound = new Sound(this.synth, this.midiChannels[OTHER_CHANNEL],
				instrument, note, velocity);
	}

	// Create new thread to play that sound
	(new Thread(sound)).start();
}

public class Sound implements Runnable {

	private int note = 50;
	private int velocity = 64;
	private Synthesizer synth;
	private MidiChannel midiChannel;
	private int instrument;

	/**
	 * constructor that is used to play unique sounds.
	 * 
	 * @param synth
	 * @param midiChannel
	 * @param instrument
	 * @param note
	 * @param velocity
	 */
	public Sound(Synthesizer synth, MidiChannel midiChannel,
			int instrument, int note, int velocity) {
		this.note = note;
		this.velocity = velocity;
		this.synth = synth;
		this.midiChannel = midiChannel;
		this.instrument = instrument;
	}

	/**
	 * method to run in its own thread
	 */
	public void run() {
		Instrument[] instruments = synth.getDefaultSoundbank()
				.getInstruments();

		synth.loadInstrument(instruments[instrument]);
		midiChannel.programChange(instrument);

		midiChannel.noteOn(note, velocity);
		delay(10 * velocity);
		midiChannel.noteOff(note, velocity);
	}

	/**
	 * create a delay to prevent threads inter-locking and causing issues
	 * 
	 * @param ms
	 */
	public void delay(int ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException ex) {
			return;
		}
	}
}

Nuoroda į pranešimą
Dalintis kituose puslapiuose

Prisijunkite prie diskusijos

Jūs galite rašyti dabar, o registruotis vėliau. Jeigu turite paskyrą, prisijunkite dabar, kad rašytumėte iš savo paskyros.

Svečias
Parašykite atsakymą...

×   Įdėta kaip raiškusis tekstas.   Atkurti formatavimą

  Only 75 emoji are allowed.

×   Nuorodos turinys įdėtas automatiškai.   Rodyti kaip įprastą nuorodą

×   Jūsų anksčiau įrašytas turinys buvo atkurtas.   Išvalyti redaktorių

×   You cannot paste images directly. Upload or insert images from URL.

Įkraunama...
  • Dabar naršo   0 narių

    Nei vienas registruotas narys šiuo metu nežiūri šio puslapio.

×
×
  • Pasirinkite naujai kuriamo turinio tipą...