~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/player/impl/MidiMessageUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.herac.tuxguitar.player.impl;
2
 
 
3
 
import javax.sound.midi.InvalidMidiDataException;
4
 
import javax.sound.midi.MetaMessage;
5
 
import javax.sound.midi.MidiMessage;
6
 
import javax.sound.midi.ShortMessage;
7
 
 
8
 
import org.herac.tuxguitar.song.models.TimeSignature;
9
 
 
10
 
public class MidiMessageUtils {
11
 
        
12
 
        public static final byte TICK_MOVE = 0x01;
13
 
        
14
 
        private static int getValue(int value){
15
 
                value = (value > 127)?127:value;
16
 
                value = (value < 0)?0:value;            
17
 
                return value;
18
 
        }
19
 
        
20
 
        private static int getChannel(int channel){
21
 
                channel = (channel > 15)?15:channel;
22
 
                channel = (channel < 0)?0:channel;              
23
 
                return channel;
24
 
        }
25
 
        
26
 
        public static MidiMessage noteOn(int channel,int note,int velocity){                            
27
 
                try {
28
 
                        ShortMessage message = new ShortMessage();
29
 
                        message.setMessage(ShortMessage.NOTE_ON, getChannel(channel), getValue(note), getValue(velocity));
30
 
                        return message;
31
 
                } catch (InvalidMidiDataException e) {
32
 
                        e.printStackTrace();
33
 
                }
34
 
                return null;
35
 
        }
36
 
        
37
 
        public static MidiMessage noteOff(int channel,int note,int velocity){                           
38
 
                try {                   
39
 
                        ShortMessage message = new ShortMessage();
40
 
                        message.setMessage(ShortMessage.NOTE_OFF, getChannel(channel), getValue(note), getValue(velocity));
41
 
                        return message;
42
 
                } catch (InvalidMidiDataException e) {
43
 
                        e.printStackTrace();
44
 
                }
45
 
                return null;
46
 
        }       
47
 
        
48
 
        public static MidiMessage controlChange(int channel,int controller,int value){                          
49
 
                try {
50
 
                        ShortMessage message = new ShortMessage();
51
 
                        message.setMessage(ShortMessage.CONTROL_CHANGE,getChannel(channel),getValue(controller), getValue(value));
52
 
                        return message;
53
 
                } catch (InvalidMidiDataException e) {
54
 
                        e.printStackTrace();
55
 
                }
56
 
                return null;
57
 
        }
58
 
        
59
 
        public static MidiMessage programChange(int channel,int instrument){                            
60
 
                try {
61
 
                        ShortMessage message = new ShortMessage();
62
 
                        message.setMessage(ShortMessage.PROGRAM_CHANGE, getChannel(channel), getValue(instrument), 0);
63
 
                        return message;
64
 
                } catch (InvalidMidiDataException e) {
65
 
                        e.printStackTrace();
66
 
                }
67
 
                return null;
68
 
        }
69
 
        
70
 
        public static MidiMessage pitchBend(int channel,int value){                             
71
 
                try {
72
 
                        ShortMessage message = new ShortMessage();
73
 
                        message.setMessage(ShortMessage.PITCH_BEND, getChannel(channel), 0, getValue(value));
74
 
                        return message;
75
 
                } catch (InvalidMidiDataException e) {
76
 
                        e.printStackTrace();
77
 
                }
78
 
                return null;
79
 
        }
80
 
                
81
 
        public static MidiMessage systemReset(){                                
82
 
                try {
83
 
                        ShortMessage message = new ShortMessage();
84
 
                        message.setMessage(ShortMessage.SYSTEM_RESET);
85
 
                        return message;
86
 
                } catch (InvalidMidiDataException e) {
87
 
                        e.printStackTrace();
88
 
                }
89
 
                return null;
90
 
        }       
91
 
        
92
 
        public static MidiMessage tempoInUSQ(int usq){                          
93
 
                try {
94
 
                        MetaMessage message = new MetaMessage();
95
 
                message.setMessage(0x51, new byte[]{ (byte)((usq >> 16) & 0x00FF),(byte)((usq >> 8) & 0x00FF),(byte)((usq) & 0x00FF) }, 3);             
96
 
                        return message;
97
 
                } catch (InvalidMidiDataException e) {
98
 
                        e.printStackTrace();
99
 
                }
100
 
                return null;            
101
 
        }
102
 
        
103
 
        public static MidiMessage timeSignature(TimeSignature ts){                              
104
 
                try {                   
105
 
                        MetaMessage message = new MetaMessage();
106
 
                message.setMessage(0x58, new byte[]{  (byte)ts.getNumerator(),(byte)ts.getDenominator().log2(),(byte)(96 / ts.getDenominator().getValue()),8 }, 4);             
107
 
                        return message;
108
 
                } catch (InvalidMidiDataException e) {
109
 
                        e.printStackTrace();
110
 
                }
111
 
                return null;            
112
 
        }               
113
 
        
114
 
        public static MidiMessage tickMove(long move){                          
115
 
                try {                                           
116
 
                        byte[] data = new byte[]{ (byte) (move & 0x00FF),
117
 
                                                                          (byte) ((move >> 8) & 0x000000FF),
118
 
                                                                          (byte) ((move >> 16) & 0x000000FF),
119
 
                                                                          (byte) ((move >> 24) & 0x000000FF),
120
 
                                                                          (byte) ((move >> 32) & 0x000000FF),
121
 
                                                                          (byte) ((move >> 40) & 0x000000FF),
122
 
                                                                          (byte) ((move >> 48) & 0x000000FF),
123
 
                                                                          (byte) ((move >> 56) & 0x000000FF) 
124
 
                        };                      
125
 
                        MetaMessage message = new MetaMessage();                
126
 
                        message.setMessage(TICK_MOVE, data,data.length);                                
127
 
                        return message;
128
 
                } catch (InvalidMidiDataException e) {
129
 
                        e.printStackTrace();
130
 
                }
131
 
                return null;            
132
 
        }       
133
 
}
 
 
b'\\ No newline at end of file'