Example #1
0
 public static function endOfTrack()
 {
     return MidiMessage::metaMessage(47, array());
 }
Example #2
0
 private function readEvent(MidiTrackReaderHelper $helper)
 {
     $statusByte = $this->readUnsignedByte();
     $helper->remainingBytes--;
     $savedByte = 0;
     $runningStatusApplies = false;
     if ($statusByte < 0x80) {
         switch ($helper->runningStatusByte) {
             case -1:
                 throw new Exception('Corrupted MIDI file: status byte is missing');
                 break;
             default:
                 $runningStatusApplies = true;
                 $savedByte = $statusByte;
                 $statusByte = $helper->runningStatusByte;
                 break;
         }
     }
     $type = $this->getType($statusByte);
     if ($type == MidiReader::STATUS_ONE_BYTE) {
         $data = 0;
         if ($runningStatusApplies) {
             $data = $savedByte;
         } else {
             $data = $this->readUnsignedByte();
             $helper->remainingBytes--;
             $helper->runningStatusByte = $statusByte;
         }
         return new MidiEvent(MidiMessage::shortMessage($statusByte & 0xf0, $statusByte & 0xf, $data), $helper->ticks);
     } else {
         if ($type == MidiReader::STATUS_TWO_BYTES) {
             $data1 = 0;
             if ($runningStatusApplies) {
                 $data1 = $savedByte;
             } else {
                 $data1 = $this->readUnsignedByte();
                 $helper->remainingBytes--;
                 $helper->runningStatusByte = $statusByte;
             }
             $helper->remainingBytes--;
             return new MidiEvent(MidiMessage::shortMessage($statusByte & 0xf0, $statusByte & 0xf, $data1, $this->readUnsignedByte()), $helper->ticks);
         } else {
             if ($type == MidiReader::STATUS_SYSEX) {
                 if (MidiReader::CANCEL_RUNNING_STATUS_ON_META_AND_SYSEX) {
                     $helper->runningStatusByte = -1;
                 }
                 $dataLength = $this->readVariableLengthQuantity($helper);
                 $data = array();
                 for ($i = 0; $i < $dataLength; $i++) {
                     $data[$i] = $this->readUnsignedByte();
                     $helper->remainingBytes--;
                 }
             } else {
                 if ($type == MidiReader::STATUS_META) {
                     if (MidiReader::CANCEL_RUNNING_STATUS_ON_META_AND_SYSEX) {
                         $helper->runningStatusByte = -1;
                     }
                     $typeByte = $this->readUnsignedByte();
                     $helper->remainingBytes--;
                     $dataLength = $this->readVariableLengthQuantity($helper);
                     $data = array();
                     for ($i = 0; $i < $dataLength; $i++) {
                         $data[$i] = $this->readUnsignedByte();
                         $helper->remainingBytes--;
                     }
                     return new MidiEvent(MidiMessage::metaMessage($typeByte, $data), $helper->ticks);
                 }
             }
         }
     }
     return null;
 }
Example #3
0
 protected function writeMetaMessage(MidiMessage $message)
 {
     $length = 0;
     $data = $message->getData();
     $this->writeUnsignedBytes(array(255));
     $this->writeUnsignedBytes(array($message->getCommand()));
     $length += 2;
     if (is_array($data)) {
         $length += $this->writeVariableLengthQuantity(count($data));
         $this->writeUnsignedBytes($data);
         $length += count($data);
     } else {
         $length += $this->writeVariableLengthQuantity(strlen($data));
         $this->writeUnsignedBytes(array($data));
         $length += strlen($data);
     }
     return $length;
 }