toggle method

void toggle(
  1. ChordChart grade
)

Toca a grade (ou para, se já toca). Sem motor, não faz nada.

Implementation

void toggle(ChordChart grade) {
  if (state.playing) {
    stop();
    return;
  }
  final engine = this.engine;
  if (engine == null) return;
  // Antes de agendar: quem toca agora para (e limpa o que agendou).
  coordinator?.started(stop);
  final now = engine.nowSeconds;
  final startAt =
      (now > engine.earliestScheduleSeconds
          ? now
          : engine.earliestScheduleSeconds) +
      0.15;
  final events = chartAudio(grade, bpm: kChartListenBpm, startAt: startAt);
  engine.schedule([
    for (final event in events)
      if (event.at >= engine.earliestScheduleSeconds) event,
  ]);
  final seconds = startAt - now + chartSeconds(grade, bpm: kChartListenBpm);
  _stopTimer = clock.timer(
    Duration(milliseconds: (seconds * 1000).round()),
    stop,
  );
  emit(const ChartMarkState(playing: true));
}