/** * Reads some Beat informations * * @param integer $start * @param Measure $measure * @param Track $track * @param Tempo $tempo * * @return integer $time duration time */ public function readBeat($start, Measure $measure, Track $track, Tempo $tempo) { $flags = $this->reader->readUnsignedByte(); if (($flags & 0x40) != 0) { $this->reader->readUnsignedByte(); } $beat = new Beat(); $voice = $beat->getVoice(0); $duration = $this->reader->factory('GuitarProDuration')->readDuration($flags); $effect = new NoteEffect(); if (($flags & 0x2) != 0) { $this->reader->factory($this->getParserName() . 'Chord')->readChord($track->countStrings(), $beat); } if (($flags & 0x4) != 0) { $this->reader->factory('GuitarProText')->readText($beat); } if (($flags & 0x8) != 0) { $this->reader->factory($this->getParserName() . 'BeatEffects')->readBeatEffects($beat, $effect); } if (($flags & 0x10) != 0) { $this->reader->factory($this->getParserName() . 'MixChange')->readMixChange($tempo); } $stringFlags = $this->reader->readUnsignedByte(); for ($i = 6; $i >= 0; $i--) { if (($stringFlags & 1 << $i) != 0 && 6 - $i < $track->countStrings()) { $string = clone $track->getString(6 - $i + 1); $note = $this->reader->factory($this->getParserName() . 'Note')->readNote($string, $track, clone $effect); $voice->addNote($note); } } $beat->setStart($start); $voice->setEmpty(false); $voice->getDuration()->copyFrom($duration); $measure->addBeat($beat); return $duration->getTime(); }
private function writeBeat(Beat $beat, Measure $measure, $changeTempo) { $voice = $beat->getVoice(0); $duration = $voice->getDuration(); $flags = 0; if ($duration->isDotted() || $duration->isDoubleDotted()) { $flags |= 0x1; } if (!$duration->getDivision()->isEqual(DivisionType::normal())) { $flags |= 0x20; } if ($beat->isTextBeat()) { $flags |= 0x4; } if ($changeTempo) { $flags |= 0x10; } $effect = null; if ($voice->isRestVoice()) { $flags |= 0x40; } else { if ($voice->countNotes() > 0) { $note = $voice->getNote(0); $effect = $note->getEffect(); if ($effect->isVibrato() || $effect->isTremoloBar() || $effect->isTapping() || $effect->isSlapping() || $effect->isPopping() || $effect->isHarmonic() || $effect->isFadeIn() || $beat->getStroke()->getDirection() != Stroke::STROKE_NONE) { $flags |= 0x8; } } } $this->writeUnsignedByte($flags); if (($flags & 0x40) != 0) { $this->writeUnsignedByte(2); } $this->writeByte($this->parseDuration($duration)); if (($flags & 0x20) != 0) { $this->writeInt($duration->getDivision()->getEnters()); } if (($flags & 0x4) != 0) { $this->writeText($beat->getText()); } if (($flags & 0x8) != 0) { $this->writeBeatEffects($beat, $effect); } if (($flags & 0x10) != 0) { $this->writeMixChange($measure->getTempo()); } $stringFlags = 0; if (!$voice->isRestVoice()) { for ($i = 0; $i < $voice->countNotes(); $i++) { $playedNote = $voice->getNote($i); $string = 7 - $playedNote->getString(); $stringFlags |= 1 << $string; } } $this->writeUnsignedByte($stringFlags); for ($i = 6; $i >= 0; $i--) { if (($stringFlags & 1 << $i) != 0) { for ($n = 0; $n < $voice->countNotes(); $n++) { $playedNote = $voice->getNote($n); if ($playedNote->getString() == 6 - $i + 1) { $this->writeNote($playedNote); break; } } } } }
private function getStroke(Beat $beat, Beat $previous = null, array $stroke) { $direction = $beat->getStroke()->getDirection(); if ($previous === null || !($direction == Stroke::STROKE_NONE && $previous->getStroke()->getDirection() == Stroke::STROKE_NONE)) { if ($direction == Stroke::STROKE_NONE) { for ($i = 0; $i < count($stroke); $i++) { $stroke[$i] = 0; } } else { $stringUseds = 0; $stringCount = 0; for ($vIndex = 0; $vIndex < $beat->countVoices(); $vIndex++) { $voice = $beat->getVoice($vIndex); for ($nIndex = 0; $nIndex < $voice->countNotes(); $nIndex++) { $note = $voice->getNote($nIndex); if (!$note->isTiedNote()) { $stringUseds |= 0x1 << $note->getString() - 1; $stringCount++; } } } if ($stringCount > 0) { $strokeMove = 0; $strokeIncrement = $beat->getStroke()->getIncrementTime($beat); for ($i = 0; $i < count($stroke); $i++) { $index = $direction == Stroke::STROKE_DOWN ? count($stroke) - 1 - $i : $i; if (($stringUseds & 0x1 << $index) != 0) { $stroke[$index] = $strokeMove; $strokeMove += $strokeIncrement; } } } } } return $stroke; }
private function writeMeasure(Measure $measure, $changeTempo) { for ($v = 0; $v < 2; $v++) { $voices = array(); for ($m = 0; $m < $measure->countBeats(); $m++) { $beat = $measure->getBeat($m); if ($v < $beat->countVoices()) { $voice = $beat->getVoice($v); if (!$voice->isEmpty()) { $voices[] = $voice; } } } if (count($voices) > 0) { $this->writeInt(count($voices)); for ($i = 0; $i < count($voices); $i++) { $voice = $voices[$i]; $this->writeBeat($voice, $voice->getBeat(), $measure, $changeTempo && $i == 0); } } else { $count = $measure->getTimeSignature()->getNumerator(); $beat = new Beat(); if ($v < $beat->countVoices()) { $voice = $beat->getVoice($v); $voice->getDuration()->setValue($measure->getTimeSignature()->getDenominator()->getValue()); $voice->setEmpty(true); $this->writeInt($count); for ($i = 0; $i < $count; $i++) { $this->writeBeat($voice, $voice->getBeat(), $measure, $changeTempo && $i == 0); } } } } }
private function adjustStrings(Beat $beat) { $track = $beat->getMeasure()->getTrack(); $freeStrings = $track->getStrings(); $notesToRemove = array(); $notes = $beat->getVoice(0)->getNotes(); foreach ($notes as $note) { $string = $this->getStringForValue($freeStrings, $note->getValue()); for ($j = 0; $j < count($freeStrings); $j++) { $tempString = $freeStrings[$j]; if ($tempString->getNumber() == $string) { $note->setValue($note->getValue() - $tempString->getValue()); $note->setString($tempString->getNumber()); array_splice($freeStrings, $j, 1); break; } } //Cannot have more notes on same string if ($note->getString() < 1) { $notesToRemove[] = $note; } } // Remove notes while (count($notesToRemove) > 0) { $beat->getVoice(0)->removeNote($notesToRemove[0]); array_splice($notesToRemove, 0, 1); } }