FileDocCategorySizeDatePackage
DirectMIDIControl.javaAPI DocphoneME MR2 API (J2ME)12111Wed May 02 16:47:10 BST 2007com.sun.mmedia

DirectMIDIControl

public class DirectMIDIControl extends Object implements MIDIControl, RateControl, PitchControl, DirectControls, TempoControl
Implement all of MIDI related controls like : MIDIControl, RateControl, PitchControl, TempoControl

Fields Summary
private com.sun.mmedia.DirectPlayer
_player
private int
_cachedRate
Constructors Summary
DirectMIDIControl(com.sun.mmedia.DirectPlayer p)

		_player = p;
	
Methods Summary
private voidcheckBank(int bank)

        if (bank < 0 || bank > 16383) {
            throw new IllegalArgumentException("Bank is out of range");
        }
    
private voidcheckChannel(int channel)

        if (channel < 0 || channel > 15) {
            throw new IllegalArgumentException("Channel is out of range");
        }
    
private voidcheckData(int data)

        if (data < 0 || data > 127) {
            throw new IllegalArgumentException("Data is out of range");
        }
    
private voidcheckLongMidiEvent(byte[] data, int offset, int length)

        if (data == null) {
            throw new IllegalArgumentException("Data is null for long MIDI event ");
        } else if ((offset >= data.length) || (offset < 0)) {
            if (data.length != 0) {
                throw new IllegalArgumentException("Invalid offset for long MIDI event");
            }
        } else if ((length > data.length) || (length < 0)) {
            throw new IllegalArgumentException("Length is out of range for long MIDI event");
        }
    
private voidcheckProgram(int program)

        if (program < 0 || program > 127) {
            throw new IllegalArgumentException("Program is out of range");
        }
    
private voidcheckState()

        if (_player.state < Player.PREFETCHED) {
            throw new IllegalStateException("Not prefetched");
        }
    
private voidcheckType(int type)

        if (type < 0x80 || type > 0xFF || type == 0xF0 || type == 0xF7) {
            throw new IllegalArgumentException("Type is out of range");
        }
    
private voidcheckVolume(int volume)

        if (volume < 0 || volume > 127) {
            throw new IllegalArgumentException("Volume is out of range");    
        }
    
public synchronized int[]getBankList(boolean custom)


        checkState();
        
        int[] bl1 = new int[20];
        
        int len = nGetBankList(_player.hNative, custom, bl1);
        if(len < 0) throw new MediaException("BankList failure");
        
        int[] bl2 = new int[len];
        
        for(int i=0; i<len; i++)
            bl2[i] = bl1[i];
        
        return bl2;
    
public synchronized intgetChannelVolume(int channel)

        checkState();
        checkChannel(channel);

        if (_player != null && _player.hNative != 0) {
            return nGetChannelVolume(_player.hNative, channel);
        } else {
            return -1;
        }
    
public synchronized java.lang.StringgetKeyName(int bank, int prog, int key)

 
        checkState();
        checkBank(bank);
        checkProgram(prog);
        
        if((key < 0) || (key > 127))
            throw new IllegalArgumentException("key out of range");
        
        byte[] str = new byte[64];
        
        int len = nGetKeyName(_player.hNative, bank, prog, key, str);
        if(len < 0) throw new MediaException("KeyName failure");
        
        return new String(str, 0, len);
    
public synchronized intgetMaxPitch()

		if (_player != null && _player.hNative != 0) {
			return nGetMaxPitch(_player.hNative);
		} else {
			return 0;
		}
	
public synchronized intgetMaxRate()

		if (_player != null && _player.hNative != 0) {
			return nGetMaxRate(_player.hNative);
		} else {
			return 0;
		}
    
public synchronized intgetMinPitch()

		if (_player != null && _player.hNative != 0) {
			return nGetMinPitch(_player.hNative);
		} else {
			return 0;
		}
	
public synchronized intgetMinRate()

		if (_player != null && _player.hNative != 0) {
			return nGetMinRate(_player.hNative);
		} else {
			return 0;
		}
	
public synchronized intgetPitch()

		if (_player != null && _player.hNative != 0) {
			return nGetPitch(_player.hNative);
		} else {
			return 0;
		}
	
public synchronized int[]getProgram(int channel)


        checkState();
        checkChannel(channel);
        
        int[] p = new int[2];
        
        int len = nGetProgram(_player.hNative, channel, p);
        if(len < 0) throw new MediaException("GetProgram failure");
        
        return p;        
    
public synchronized int[]getProgramList(int bank)


        checkState();
        checkBank(bank);
                
        int[] pl1 = new int[200];
        
        int len = nGetProgramList(_player.hNative, bank, pl1);        
        if(len < 0) throw new MediaException("ProgramList failure");
        
        int[] pl2 = new int[len];
        
        for(int i=0; i<len; i++)
            pl2[i] = pl1[i];
        
        return pl2;
    
public synchronized java.lang.StringgetProgramName(int bank, int prog)


        checkState();
        checkBank(bank);
        checkProgram(prog);
        
        byte[] str = new byte[64];
        
        int len = nGetProgramName(_player.hNative, bank, prog, str);
        if(len < 0) throw new MediaException("ProgramName failure");
        
        return new String(str, 0, len);    
    
public synchronized intgetRate()

		if (_player != null && _player.hNative != 0) {
			return _cachedRate == -1 ? nGetRate(_player.hNative) : _cachedRate;
		} else {
			return 0;
		}
	
public synchronized intgetTempo()

    	if (_player != null &&  _player.hNative != 0) {
			return nGetTempo(_player.hNative);
		} else {
			return 0;
		}
	
public synchronized booleanisBankQuerySupported()

	return nIsBankQuerySupported(_player.hNative);        
    
public synchronized intlongMidiEvent(byte[] data, int offset, int length)

        checkState();
        checkLongMidiEvent(data, offset, length);

        if (_player != null && _player.hNative != 0) {
            return nLongMidiEvent(_player.hNative, data, offset, length);
        } else {
            return -1;
        }
    
private native intnGetBankList(int handle, boolean custom, int[] list)

private native intnGetChannelVolume(int handle, int channel)

private native intnGetKeyName(int handle, int bank, int prog, int key, byte[] keyname)

private native intnGetMaxPitch(int handle)

private native intnGetMaxRate(int handle)

private native intnGetMinPitch(int handle)

private native intnGetMinRate(int handle)

private native intnGetPitch(int handle)

private native intnGetProgram(int handle, int channel, int[] program)

private native intnGetProgramList(int handle, int bank, int[] proglist)

private native intnGetProgramName(int handle, int bank, int prog, byte[] progname)

private native intnGetRate(int handle)

private native intnGetTempo(int handle)

private native booleannIsBankQuerySupported(int handle)

private native intnLongMidiEvent(int handle, byte[] data, int offset, int length)

private native voidnSetChannelVolume(int handle, int channel, int volume)

private native intnSetPitch(int handle, int pitch)

private native voidnSetProgram(int handle, int channel, int bank, int program)

private native intnSetRate(int handle, int rate)

private native intnSetTempo(int handle, int tempo)

private native voidnShortMidiEvent(int handle, int type, int data1, int data2)

public synchronized voidplayerClosed()

        _player = null;
	
public synchronized voidplayerStarted()

		if (_cachedRate != -1) {
			// NOTE - MMAPI SPEC is not clear about this
			// set cached rate at enter playing state
			nSetRate(_player.hNative, _cachedRate);
			_cachedRate = -1;
		}
	
public synchronized voidplayerStopped()

    
public synchronized voidsetChannelVolume(int channel, int volume)

		checkState();
                checkChannel(channel);
                checkVolume(volume);
		
		if (_player != null && _player.hNative != 0) {
			nSetChannelVolume(_player.hNative, channel, volume);
		}
	
public synchronized intsetPitch(int millisemitones)

		int max = getMaxPitch();
		int min = getMinPitch();
		
		if (millisemitones > max) {
			millisemitones = max;
		} else if (millisemitones < min) {
			millisemitones = min;
		}

		if (_player != null && _player.hNative != 0) {
			return nSetPitch(_player.hNative, millisemitones);
		} else {
			return 0;
		}
	
public synchronized voidsetProgram(int channel, int bank, int program)

        checkState();
        checkChannel(channel);
        
        if(bank != -1) checkBank(bank);
        checkProgram(program);

        if (_player != null && _player.hNative != 0) {
            nSetProgram(_player.hNative, channel, bank, program);
        }
    
public synchronized intsetRate(int millirate)

        if (_player == null) 
            return 0;

        int max = getMaxRate();
        int min = getMinRate();

        if (millirate > max) {
                millirate = max;
        } else if (millirate < min) {
                millirate = min;
        }

        if (_player.state >= Player.STARTED) {
                _cachedRate = -1;
                return nSetRate(_player.hNative, millirate);
        } else {
                _cachedRate = millirate;
                return millirate;
        }
	
public synchronized intsetTempo(int millitempo)

		if (millitempo < 0) {
			millitempo = 0;
		}

		if (_player != null && _player.hNative != 0) {
			return nSetTempo(_player.hNative, millitempo);
		} else {
			return 0;
		}
	
public synchronized voidshortMidiEvent(int type, int data1, int data2)

	    checkState();
        checkType(type);
        checkData(data1);
        checkData(data2);
    
        if (_player != null && _player.hNative != 0) {
            nShortMidiEvent(_player.hNative, type, data1, data2);
        }