public static void RestartMetronome() { m_stop_metronome_required = false; if (m_vsq != null) { double now = PortUtil.getCurrentTime(); double elapsed = ((now - m_started) + 0.25) * m_speed; int clock = (int)m_vsq.getClockFromSec(m_started_sec + elapsed); ByRef <Integer> bar = new ByRef <Integer>(); Timesig timesig = m_vsq.getTimesigAt(clock, bar); int clock_at_bartop = m_vsq.getClockFromBarCount(bar.value); int clock_step = 480 * 4 / timesig.denominator; int next_clock = clock_at_bartop + ((clock - clock_at_bartop) / clock_step + 1) * clock_step; MidiQueue mq = new MidiQueue(); mq.Track = 0; mq.Clock = next_clock; mq.Channel = 14; mq.Program = ProgramNormal; mq.Note = NoteNormal; mq.Velocity = 0x40; mq.Done += new MidiQueueDoneEventHandler(ReGenerateMidiQueue); s_queue.add(mq); if ((next_clock - clock_at_bartop) % (timesig.numerator * clock_step) == 0) { MidiQueue mq_bell = new MidiQueue(); mq_bell.Track = 0; mq_bell.Clock = next_clock; mq_bell.Channel = 15; mq_bell.Program = ProgramBell; mq_bell.Note = NoteBell; mq_bell.Velocity = 0x40; s_queue.add(mq_bell); } Collections.sort(s_queue); } }
/// <summary> /// テキストファイルからデータ点を読込み、現在のリストに追加します /// </summary> /// <param name="reader"></param> /// <returns></returns> public string appendFromText(TextStream reader) { #if DEBUG sout.println("VsqBPList#appendFromText; start"); double started = PortUtil.getCurrentTime(); int count = 0; #endif int clock = 0; int value = 0; int minus = 1; int mode = 0; // 0: clockを読んでいる, 1: valueを読んでいる while (reader.ready()) { char ch = reader.get(); if (ch == '\n') { if (mode == 1) { addWithoutSort(clock, value * minus); mode = 0; clock = 0; value = 0; minus = 1; } continue; } if (ch == '[') { if (mode == 1) { addWithoutSort(clock, value * minus); mode = 0; clock = 0; value = 0; minus = 1; } reader.setPointer(reader.getPointer() - 1); break; } if (ch == '=') { mode = 1; continue; } if (ch == '-') { minus = -1; continue; } if (Char.IsNumber(ch)) { int num = 0; if (ch == '1') { num = 1; } else if (ch == '2') { num = 2; } else if (ch == '3') { num = 3; } else if (ch == '4') { num = 4; } else if (ch == '5') { num = 5; } else if (ch == '6') { num = 6; } else if (ch == '7') { num = 7; } else if (ch == '8') { num = 8; } else if (ch == '9') { num = 9; } if (mode == 0) { clock = clock * 10 + num; } else { value = value * 10 + num; } } } return(reader.readLine()); }
private static void ThreadProc() { const int TOLERANCE_MILLISEC = 10; while (!m_stop_required) { if (s_queue.size() == 0) { Thread.Sleep(100); continue; } int clock = s_queue.get(0).Clock; double tick_sec = m_vsq.getSecFromClock(clock); double next_tick = m_started + ((tick_sec - m_started_sec) / m_speed); double now = PortUtil.getCurrentTime(); double time_span = next_tick - now; if (time_span <= 0) { Vector <MidiQueue> add = new Vector <MidiQueue>(); while (s_queue.size() > 0 && s_queue.get(0).Clock == clock) { if (s_queue.get(0).Done != null) { add.addAll(s_queue.get(0).Done(s_queue.get(0))); } MidiQueue item = s_queue.get(0); if (item.Track == 0 || s_metronome_device == s_general_device) { s_device0.Play(item.Channel, item.Program, item.Note, item.Velocity); } else { s_device1.Play(item.Channel, item.Program, item.Note, item.Velocity); } s_queue.removeElementAt(0); } s_queue.addAll(add); Collections.sort(s_queue); continue; } int wait_millisec = (int)((next_tick - PortUtil.getCurrentTime()) * 1000.0) - PreUtterance; int thiswait = (wait_millisec > TOLERANCE_MILLISEC * 2) ? TOLERANCE_MILLISEC * 2 : wait_millisec; #if DEBUG AppManager.debugWriteLine("MidiPlayer#ThreadProc; wait_millisec=" + wait_millisec); #endif while (thiswait > TOLERANCE_MILLISEC) { Thread.Sleep(thiswait); wait_millisec = (int)((next_tick - PortUtil.getCurrentTime()) * 1000.0) - PreUtterance; if (wait_millisec < TOLERANCE_MILLISEC || m_stop_required) { break; } thiswait = wait_millisec; } if (m_stop_required) { break; } if (m_temp_exit) { m_temp_exit = false; Vector <MidiQueue> add = new Vector <MidiQueue>(); while (s_queue.size() > 0 && s_queue.get(0).Clock == clock) { if (s_queue.get(0).Done != null) { add.addAll(s_queue.get(0).Done(s_queue.get(0))); } s_queue.removeElementAt(0); } s_queue.addAll(add); Collections.sort(s_queue); continue; } Vector <MidiQueue> adding = new Vector <MidiQueue>(); while (s_queue.size() > 0 && s_queue.get(0).Clock == clock) { if (s_queue.get(0).Track == 0 || s_metronome_device == s_general_device) { if (s_queue.get(0).Track != 0 || (s_queue.get(0).Track == 0 && !m_stop_metronome_required)) { s_device0.Play(s_queue.get(0).Channel, s_queue.get(0).Program, s_queue.get(0).Note, s_queue.get(0).Velocity); } } else { s_device1.Play(s_queue.get(0).Channel, s_queue.get(0).Program, s_queue.get(0).Note, s_queue.get(0).Velocity); } if (s_queue.get(0).Done != null) { if (s_queue.get(0).Track != 0 || (s_queue.get(0).Track == 0 && !m_stop_metronome_required)) { adding.addAll(s_queue.get(0).Done(s_queue.get(0))); } } s_queue.removeElementAt(0); } s_queue.addAll(adding); Collections.sort(s_queue); } }
/* * public static int getNumDevs() { * try { * int i = (int)win32.midiInGetNumDevs(); * return i; * } catch ( Exception ex ) { * debug.push_log( "MidiInDevice.GetNumDevs" ); * debug.push_log( " ex=" + ex ); * } * return 0; * } */ /*public static MIDIINCAPS[] GetMidiInDevices() { * List<MIDIINCAPS> ret = new List<MIDIINCAPS>(); * uint num = 0; * try { * num = win32.midiInGetNumDevs(); * } catch { * num = 0; * } * for ( uint i = 0; i < num; i++ ) { * MIDIINCAPS m = new MIDIINCAPS(); * uint r = win32.midiInGetDevCaps( i, ref m, (uint)Marshal.SizeOf( m ) ); #if DEBUG * sout.println( "MidiInDevice#GetMidiDevices; #" + i + "; r=" + r + "; m=" + m ); #endif * ret.Add( m ); * } * return ret.ToArray(); * }*/ public void MidiInProc(uint hMidiIn, uint wMsg, int dwInstance, int dwParam1, int dwParam2) { try { switch (wMsg) { case win32.MM_MIM_OPEN: { return; } case win32.MM_MIM_CLOSE: { return; } case win32.MM_MIM_DATA: { int receive = dwParam1; double now = PortUtil.getCurrentTime(); switch (receive & 0xF0) { case 0x80: case 0x90: case 0xa0: case 0xb0: case 0xe0: { if (MidiReceived != null) { javax.sound.midi.MidiMessage msg = new cadencii.javax.sound.midi.MidiMessage( new byte[] { (byte)(receive & 0xff), (byte)((receive & 0xffff) >> 8), (byte)((receive & ((2 << 24) - 1)) >> 16) }); MidiReceived.Invoke(this, msg); } break; } case 0xc0: case 0xd0: { if (MidiReceived != null) { javax.sound.midi.MidiMessage msg = new cadencii.javax.sound.midi.MidiMessage( new byte[] { (byte)(receive & 0xff), (byte)((receive & 0xffff) >> 8) }); MidiReceived.Invoke(this, msg); } break; } case 0xf0: { if (mReceiveSystemCommonMessage) { byte b0 = (byte)(receive & 0xff); byte b1 = (byte)((receive >> 8) & 0xff); byte b2 = (byte)((receive >> 16) & 0xff); byte b3 = (byte)((receive >> 24) & 0xff); if (b0 == 0xf1) { // MTC quater frame message if (MidiReceived != null) { javax.sound.midi.MidiMessage msg = new cadencii.javax.sound.midi.MidiMessage(new byte[] { b0, b1, b2 }); MidiReceived.Invoke(this, msg); } } else if (b0 == 0xf2) { // song position pointer #if DEBUG sout.println("MidiInDevice#MidiInProc; 0xf2; b0=" + PortUtil.toHexString(b0, 2) + "; b1=" + PortUtil.toHexString(b1, 2) + "; b2=" + PortUtil.toHexString(b2, 2)); #endif } } if (mReceiveSystemRealtimeMessage && MidiReceived != null) { byte b0 = (byte)(receive & 0xff); byte b1 = (byte)((receive >> 8) & 0xff); byte b2 = (byte)((receive >> 16) & 0xff); byte b3 = (byte)((receive >> 24) & 0xff); if (b0 == 0xfa) { MidiReceived.Invoke(this, new javax.sound.midi.MidiMessage(new byte[] { b0 })); } else if (b0 == 0xfc) { MidiReceived.Invoke(this, new javax.sound.midi.MidiMessage(new byte[] { b0 })); } } break; } } return; } case win32.MM_MIM_LONGDATA: { return; } case win32.MM_MIM_ERROR: { return; } case win32.MM_MIM_LONGERROR: { return; } } } catch (Exception ex) { debug.push_log("MidiInDevice.MidiInProc"); debug.push_log(" ex=" + ex); } }