Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
MidiSounder - Sequenza.java

Sequenza.java

Caricato da: Netarrow
Scarica il programma completo

  1. import javax.swing.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * Classe che incapsula una sequenza di Note, implementa Noteable e Runnable per suonare in un campo differente
  6.  * @author netarrow
  7.  * @version 1.2.00
  8.  * @since 1.2.01
  9.  */
  10. public class Sequenza implements Noteable, Runnable {
  11. private Tastiera t;
  12. private volatile Vector note;
  13. private Thread thread;
  14.  
  15. /**
  16.  * inizializza il vettore per le note
  17.  * @param t tastiera da utilizzare
  18.  * @throws Exception
  19.  */
  20. Sequenza(Tastiera t) throws Exception {
  21. this.t = t;
  22. note = new Vector();
  23. }
  24.  
  25. /**
  26.  * Metodo che pulisce la sequenze, svuotando il vettore
  27.  */
  28. public synchronized void clear() {
  29. if(thread != null && thread.isAlive()) thread.stop();
  30. note.clear();
  31. }
  32.  
  33. /**
  34.  * Metodo sincronizzato che aggiunge una nota al vettore
  35.  * @see Nota
  36.  */
  37. public synchronized void addNota(Nota n) {
  38. note.add(n);
  39. }
  40.  
  41. /**
  42.  * Metodo che avvia il thread in cui si suona la sequenza
  43.  * @param isCanonic controlla se è attivato il canonico
  44.  * @throws Exception
  45.  */
  46. public void suonaSeq(boolean isCanonic) throws Exception {
  47. if(isCanonic) {
  48. thread = new Thread(this);
  49. thread.start();
  50. } else {
  51. if(thread != null && thread.isAlive()) {
  52. thread.stop();
  53. thread = null;
  54. }
  55. thread = new Thread(this);
  56. thread.start();
  57. }
  58. }
  59.  
  60. /**
  61.  * Thread che scorre le note della sequenze e le suona a seconda dell'altezza<br>
  62.  * a seconda della durata attente di suonare la nota seguente
  63.  */
  64. public void run() {
  65. try {
  66. for(int i = 0; i < note.size(); i++) {
  67. t.suonaNota(((Nota)note.get(i)).getNome().toUpperCase().replace('#', 'd'), true);
  68. Thread.sleep(((Nota)note.get(i)).getDurata());
  69. }
  70. } catch(Exception ex) {
  71. return;
  72. }
  73. }
  74. }