openMSX
CassettePlayer.cc
Go to the documentation of this file.
1 // TODO:
2 // - improve consistency when a reset occurs: tape is removed when you were
3 // recording, but it is not removed when you were playing
4 // - specify prefix for auto file name generation when recording (setting?)
5 // - append to existing wav files when recording (record command), but this is
6 // basically a special case (pointer at the end) of:
7 // - (partly) overwrite an existing wav file from any given time index
8 // - seek in cassette images for the next and previous file (using empty space?)
9 // - (partly) overwrite existing wav files with new tape data (not very hi prio)
10 // - handle read-only cassette images (e.g.: CAS images or WAV files with a RO
11 // flag): refuse to go to record mode when those are selected
12 // - smartly auto-set the position of tapes: if you insert an existing WAV
13 // file, it will have the position at the start, assuming PLAY mode by
14 // default. When specifiying record mode at insert (somehow), it should be
15 // at the back.
16 // Alternatively, we could remember the index in tape images by storing the
17 // index in some persistent data file with its SHA1 sum as it was as we last
18 // saw it. When there are write actions to the tape, the hash has to be
19 // recalculated and replaced in the data file. An optimization would be to
20 // first simply check on the length of the file and fall back to SHA1 if that
21 // results in multiple matches.
22 
23 #include "CassettePlayer.hh"
24 #include "Connector.hh"
25 #include "CassettePort.hh"
26 #include "CommandController.hh"
27 #include "DeviceConfig.hh"
28 #include "HardwareConfig.hh"
29 #include "XMLElement.hh"
30 #include "FileContext.hh"
31 #include "FilePool.hh"
32 #include "File.hh"
33 #include "WavImage.hh"
34 #include "CasImage.hh"
35 #include "CliComm.hh"
36 #include "MSXMotherBoard.hh"
37 #include "Reactor.hh"
38 #include "GlobalSettings.hh"
39 #include "CommandException.hh"
40 #include "EventDistributor.hh"
41 #include "FileOperations.hh"
42 #include "WavWriter.hh"
43 #include "TclObject.hh"
44 #include "DynamicClock.hh"
45 #include "EmuDuration.hh"
46 #include "StringOp.hh"
47 #include "serialize.hh"
48 #include "unreachable.hh"
49 #include "memory.hh"
50 #include <algorithm>
51 #include <cassert>
52 
53 using std::string;
54 using std::vector;
55 
56 namespace openmsx {
57 
58 static const unsigned RECORD_FREQ = 44100;
59 static const double OUTPUT_AMP = 60.0;
60 
61 static XMLElement createXML()
62 {
63  XMLElement xml("cassetteplayer");
64  xml.addChild("sound").addChild("volume", "5000");
65  return xml;
66 }
67 
69  : ResampledSoundDevice(hwConf.getMotherBoard(), getName(), getDescription(), 1)
70  , syncEndOfTape(hwConf.getMotherBoard().getScheduler())
71  , syncAudioEmu (hwConf.getMotherBoard().getScheduler())
72  , tapePos(EmuTime::zero)
73  , prevSyncTime(EmuTime::zero)
74  , audioPos(0)
75  , motherBoard(hwConf.getMotherBoard())
76  , tapeCommand(
77  motherBoard.getCommandController(),
78  motherBoard.getStateChangeDistributor(),
79  motherBoard.getScheduler())
80  , loadingIndicator(
81  motherBoard.getReactor().getGlobalSettings().getThrottleManager())
82  , autoRunSetting(
83  motherBoard.getCommandController(),
84  "autoruncassettes", "automatically try to run cassettes", true)
85  , sampcnt(0)
86  , state(STOP)
87  , lastOutput(false)
88  , motor(false), motorControl(true)
89  , syncScheduled(false)
90 {
91  setInputRate(44100); // Initialize with dummy value
92 
93  removeTape(EmuTime::zero);
94 
95  static XMLElement xml = createXML();
96  registerSound(DeviceConfig(hwConf, xml));
97 
99  OPENMSX_BOOT_EVENT, *this);
100  motherBoard.getMSXCliComm().update(CliComm::HARDWARE, getName(), "add");
101 }
102 
104 {
105  unregisterSound();
106  if (auto* c = getConnector()) {
107  c->unplug(getCurrentTime());
108  }
110  OPENMSX_BOOT_EVENT, *this);
111  motherBoard.getMSXCliComm().update(CliComm::HARDWARE, getName(), "remove");
112 }
113 
114 void CassettePlayer::autoRun()
115 {
116  if (!playImage) return;
117 
118  // try to automatically run the tape, if that's set
119  CassetteImage::FileType type = playImage->getFirstFileType();
120  if (!autoRunSetting.getBoolean() || type == CassetteImage::UNKNOWN) {
121  return;
122  }
123  string instr1, instr2;
124  switch (type) {
126  instr1 = R"(RUN\"CAS:\")";
127  break;
129  instr1 = R"(BLOAD\"CAS:\",R)";
130  break;
132  // Note that CLOAD:RUN won't work: BASIC ignores stuff
133  // after the CLOAD command. That's why it's split in two.
134  instr1 = "CLOAD";
135  instr2 = "RUN";
136  break;
137  default:
138  UNREACHABLE; // Shouldn't be possible
139  }
140  string command =
141  "namespace eval ::openmsx {\n"
142  " variable auto_run_bp\n"
143 
144  " proc auto_run_cb {args} {\n"
145  " variable auto_run_bp\n"
146  " debug remove_bp $auto_run_bp\n"
147  " unset auto_run_bp\n"
148 
149  // Without the 0.1s delay here, the type command gets messed up
150  // on MSX1 machines for some reason (starting to type too early?)
151  " after time 0.1 \"type [lindex $args 0]\\\\r\"\n"
152 
153  " set next [lrange $args 1 end]\n"
154  " if {[llength $next] == 0} return\n"
155 
156  // H_READ isn't called after CLOAD, but H_MAIN is. However, it's
157  // also called right after H_READ, so we wait a little before
158  // setting up the breakpoint.
159  " set cmd1 \"openmsx::auto_run_cb $next\"\n"
160  " set cmd2 \"set openmsx::auto_run_bp \\[debug set_bp 0xFF0C 1 \\\"$cmd1\\\"\\]\"\n" // H_MAIN
161  " after time 0.2 $cmd2\n"
162  " }\n"
163 
164  " if {[info exists auto_run_bp]} {debug remove_bp $auto_run_bp\n}\n"
165  " set auto_run_bp [debug set_bp 0xFF07 1 {\n" // H_READ
166  " openmsx::auto_run_cb " + instr1 + " " + instr2 + "\n"
167  " }]\n"
168 
169  // re-trigger hook(s), needed when already booted in BASIC
170  " type_via_keyboard \'\\r\n"
171  "}";
172  try {
173  motherBoard.getCommandController().executeCommand(command);
174  } catch (CommandException& e) {
175  motherBoard.getMSXCliComm().printWarning(
176  "Error executing loading instruction using command \"" + command + "\" for AutoRun: " +
177  e.getMessage() + "\n Please report a bug.");
178  }
179 }
180 
181 string CassettePlayer::getStateString() const
182 {
183  switch (getState()) {
184  case PLAY: return "play";
185  case RECORD: return "record";
186  case STOP: return "stop";
187  }
188  UNREACHABLE; return {};
189 }
190 
191 bool CassettePlayer::isRolling() const
192 {
193  // Is the tape 'rolling'?
194  // is true when:
195  // not in stop mode (there is a tape inserted and not at end-of-tape)
196  // AND [ user forced playing (motorcontrol=off) OR motor enabled by
197  // software (motor=on) ]
198  return (getState() != STOP) && (motor || !motorControl);
199 }
200 
201 double CassettePlayer::getTapePos(EmuTime::param time)
202 {
203  sync(time);
204  return (tapePos - EmuTime::zero).toDouble();
205 }
206 
207 double CassettePlayer::getTapeLength(EmuTime::param time)
208 {
209  if (playImage) {
210  return (playImage->getEndTime() - EmuTime::zero).toDouble();
211  } else if (getState() == RECORD) {
212  return getTapePos(time);
213  } else {
214  return 0.0;
215  }
216 }
217 
218 void CassettePlayer::checkInvariants() const
219 {
220  switch (getState()) {
221  case STOP:
222  assert(!recordImage);
223  if (playImage) {
224  // we're at end-of tape
225  assert(!getImageName().empty());
226  } else {
227  // no tape inserted, imageName may or may not be empty
228  }
229  break;
230  case PLAY:
231  assert(!getImageName().empty());
232  assert(!recordImage);
233  assert(playImage);
234  break;
235  case RECORD:
236  assert(!getImageName().empty());
237  assert(recordImage);
238  assert(!playImage);
239  break;
240  default:
241  UNREACHABLE;
242  }
243 }
244 
245 void CassettePlayer::setState(State newState, const Filename& newImage,
246  EmuTime::param time)
247 {
248  sync(time);
249 
250  // set new state if different from old state
251  State oldState = getState();
252  if (oldState == newState) return;
253 
254  // cannot directly switch from PLAY to RECORD or vice-versa,
255  // (should always go via STOP)
256  assert(!((oldState == PLAY) && (newState == RECORD)));
257  assert(!((oldState == RECORD) && (newState == PLAY)));
258 
259  // stuff for leaving the old state
260  // 'recordImage==nullptr' can happen in case of loadstate.
261  if ((oldState == RECORD) && recordImage) {
262  flushOutput();
263  bool empty = recordImage->isEmpty();
264  recordImage.reset();
265  if (empty) {
266  // delete the created WAV file, as it is useless
267  FileOperations::unlink(getImageName().getResolved()); // ignore errors
268  setImageName(Filename());
269  }
270  }
271 
272  // actually switch state
273  state = newState;
274  setImageName(newImage);
275 
276  // stuff for entering the new state
277  if (newState == RECORD) {
278  partialOut = 0.0;
279  partialInterval = 0.0;
280  lastX = lastOutput ? OUTPUT_AMP : -OUTPUT_AMP;
281  lastY = 0.0;
282  }
283  motherBoard.getMSXCliComm().update(
284  CliComm::STATUS, "cassetteplayer", getStateString());
285 
286  updateLoadingState(time); // sets SP for tape-end detection
287 
288  checkInvariants();
289 }
290 
291 void CassettePlayer::updateLoadingState(EmuTime::param time)
292 {
293  assert(prevSyncTime == time); // sync() must be called
294  // TODO also set loadingIndicator for RECORD?
295  // note: we don't use isRolling()
296  loadingIndicator.update(motor && (getState() == PLAY));
297 
298  syncEndOfTape.removeSyncPoint();
299  if (isRolling() && (getState() == PLAY)) {
300  syncEndOfTape.setSyncPoint(time + (playImage->getEndTime() - tapePos));
301  }
302 }
303 
304 void CassettePlayer::setImageName(const Filename& newImage)
305 {
306  casImage = newImage;
307  motherBoard.getMSXCliComm().update(
308  CliComm::MEDIA, "cassetteplayer", casImage.getResolved());
309 }
310 
311 void CassettePlayer::insertTape(const Filename& filename)
312 {
313  if (!filename.empty()) {
314  FilePool& filePool = motherBoard.getReactor().getFilePool();
315  try {
316  // first try WAV
317  playImage = make_unique<WavImage>(filename, filePool);
318  } catch (MSXException& e) {
319  try {
320  // if that fails use CAS
321  playImage = make_unique<CasImage>(
322  filename, filePool,
323  motherBoard.getMSXCliComm());
324  } catch (MSXException& e2) {
325  throw MSXException(
326  "Failed to insert WAV image: \"" +
327  e.getMessage() +
328  "\" and also failed to insert CAS image: \"" +
329  e2.getMessage() + '\"');
330  }
331  }
332  } else {
333  // This is a bit tricky, consider this scenario: we switch from
334  // RECORD->PLAY, but we didn't actually record anything: The
335  // removeTape() call above (indirectly) deletes the empty
336  // recorded wav image and also clears imageName. Now because
337  // the 'filename' parameter is passed by reference, and because
338  // getImageName() returns a reference, this 'filename'
339  // parameter now also is an empty string.
340  }
341 
342  // possibly recreate resampler
343  unsigned inputRate = playImage ? playImage->getFrequency() : 44100;
344  if (inputRate != getInputRate()) {
345  setInputRate(inputRate);
346  createResampler();
347  }
348 
349  setImageName(filename);
350 }
351 
352 void CassettePlayer::playTape(const Filename& filename, EmuTime::param time)
353 {
354  // Temporally go to STOP state:
355  // RECORD: First close the recorded image. Otherwise it goes wrong
356  // if you switch from RECORD->PLAY on the same image.
357  // PLAY: Go to stop because we temporally violate some invariants
358  // (tapePos can be beyond end-of-tape).
359  setState(STOP, getImageName(), time); // keep current image
360  insertTape(filename);
361  rewind(time); // sets PLAY mode
362  autoRun();
363 }
364 
365 void CassettePlayer::rewind(EmuTime::param time)
366 {
367  sync(time); // before tapePos changes
368  assert(getState() != RECORD);
369  tapePos = EmuTime::zero;
370  audioPos = 0;
371 
372  if (getImageName().empty()) {
373  // no image inserted, do nothing
374  assert(getState() == STOP);
375  } else {
376  // keep current image
377  setState(PLAY, getImageName(), time);
378  }
379  updateLoadingState(time);
380 }
381 
382 void CassettePlayer::recordTape(const Filename& filename, EmuTime::param time)
383 {
384  removeTape(time); // flush (possible) previous recording
385  recordImage = make_unique<Wav8Writer>(filename, 1, RECORD_FREQ);
386  tapePos = EmuTime::zero;
387  setState(RECORD, filename, time);
388 }
389 
390 void CassettePlayer::removeTape(EmuTime::param time)
391 {
392  sync(time); // before tapePos changes
393  playImage.reset();
394  tapePos = EmuTime::zero;
395  setState(STOP, Filename(), time);
396 }
397 
398 void CassettePlayer::setMotor(bool status, EmuTime::param time)
399 {
400  if (status != motor) {
401  sync(time);
402  motor = status;
403  updateLoadingState(time);
404  }
405 }
406 
407 void CassettePlayer::setMotorControl(bool status, EmuTime::param time)
408 {
409  if (status != motorControl) {
410  sync(time);
411  motorControl = status;
412  updateLoadingState(time);
413  }
414 }
415 
416 int16_t CassettePlayer::readSample(EmuTime::param time)
417 {
418  if (getState() == PLAY) {
419  // playing
420  sync(time);
421  return isRolling() ? playImage->getSampleAt(tapePos) : 0;
422  } else {
423  // record or stop
424  return 0;
425  }
426 }
427 
428 void CassettePlayer::setSignal(bool output, EmuTime::param time)
429 {
430  sync(time);
431  lastOutput = output;
432 }
433 
434 void CassettePlayer::sync(EmuTime::param time)
435 {
436  EmuDuration duration = time - prevSyncTime;
437  prevSyncTime = time;
438 
439  updateTapePosition(duration, time);
440  generateRecordOutput(duration);
441 }
442 
443 void CassettePlayer::updateTapePosition(
444  EmuDuration::param duration, EmuTime::param time)
445 {
446  if (!isRolling() || (getState() != PLAY)) return;
447 
448  tapePos += duration;
449  assert(tapePos <= playImage->getEndTime());
450 
451  // synchronize audio with actual tape position
452  if (!syncScheduled) {
453  // don't sync too often, this improves sound quality
454  syncScheduled = true;
455  syncAudioEmu.setSyncPoint(time + EmuDuration::sec(1));
456  }
457 }
458 
459 void CassettePlayer::generateRecordOutput(EmuDuration::param duration)
460 {
461  if (!recordImage || !isRolling()) return;
462 
463  double out = lastOutput ? OUTPUT_AMP : -OUTPUT_AMP;
464  double samples = duration.toDouble() * RECORD_FREQ;
465  double rest = 1.0 - partialInterval;
466  if (rest <= samples) {
467  // enough to fill next interval
468  partialOut += out * rest;
469  fillBuf(1, int(partialOut));
470  samples -= rest;
471 
472  // fill complete intervals
473  int count = int(samples);
474  if (count > 0) {
475  fillBuf(count, int(out));
476  }
477  samples -= count;
478 
479  // partial last interval
480  partialOut = samples * out;
481  partialInterval = 0.0;
482  } else {
483  partialOut += samples * out;
484  partialInterval += samples;
485  }
486 }
487 
488 void CassettePlayer::fillBuf(size_t length, double x)
489 {
490  assert(recordImage);
491  static const double A = 252.0 / 256.0;
492 
493  double y = lastY + (x - lastX);
494 
495  while (length) {
496  size_t len = std::min(length, BUF_SIZE - sampcnt);
497  for (size_t j = 0; j < len; ++j) {
498  buf[sampcnt++] = int(y) + 128;
499  y *= A;
500  }
501  length -= len;
502  assert(sampcnt <= BUF_SIZE);
503  if (BUF_SIZE == sampcnt) {
504  flushOutput();
505  }
506  }
507  lastY = y;
508  lastX = x;
509 }
510 
511 void CassettePlayer::flushOutput()
512 {
513  try {
514  recordImage->write(buf, 1, unsigned(sampcnt));
515  sampcnt = 0;
516  recordImage->flush(); // update wav header
517  } catch (MSXException& e) {
518  motherBoard.getMSXCliComm().printWarning(
519  "Failed to write to tape: " + e.getMessage());
520  }
521 }
522 
523 
524 const string& CassettePlayer::getName() const
525 {
526  static const string pluggableName("cassetteplayer");
527  return pluggableName;
528 }
529 
531 {
532  // TODO: this description is not entirely accurate, but it is used
533  // as an identifier for this audio device in e.g. Catapult. We should
534  // use another way to identify audio devices A.S.A.P.!
535 
536  return "Cassetteplayer, use to read .cas or .wav files.";
537 }
538 
539 void CassettePlayer::plugHelper(Connector& conn, EmuTime::param time)
540 {
541  sync(time);
542  lastOutput = static_cast<CassettePort&>(conn).lastOut();
543 }
544 
545 void CassettePlayer::unplugHelper(EmuTime::param time)
546 {
547  // note: may not throw exceptions
548  setState(STOP, getImageName(), time); // keep current image
549 }
550 
551 
552 void CassettePlayer::generateChannels(int** buffers, unsigned num)
553 {
554  // Single channel device: replace content of buffers[0] (not add to it).
555  if ((getState() != PLAY) || !isRolling()) {
556  buffers[0] = nullptr;
557  return;
558  }
559  playImage->fillBuffer(audioPos, buffers, num);
560  audioPos += num;
561 }
562 
563 
564 int CassettePlayer::signalEvent(const std::shared_ptr<const Event>& event)
565 {
566  if (event->getType() == OPENMSX_BOOT_EVENT) {
567  if (!getImageName().empty()) {
568  // Reinsert tape to make sure everything is reset.
569  try {
570  playTape(getImageName(), getCurrentTime());
571  } catch (MSXException& e) {
572  motherBoard.getMSXCliComm().printWarning(
573  "Failed to insert tape: " + e.getMessage());
574  }
575  }
576  }
577  return 0;
578 }
579 
580 void CassettePlayer::execEndOfTape(EmuTime::param time)
581 {
582  // tape ended
583  sync(time);
584  assert(tapePos == playImage->getEndTime());
585  motherBoard.getMSXCliComm().printWarning(
586  "Tape end reached... stopping. "
587  "You may need to insert another tape image "
588  "that contains side B. (Or you used the wrong "
589  "loading command.)");
590  setState(STOP, getImageName(), time); // keep current image
591 }
592 
593 void CassettePlayer::execSyncAudioEmu(EmuTime::param time)
594 {
595  if (getState() == PLAY) {
596  updateStream(time);
597  sync(time);
598  DynamicClock clk(EmuTime::zero);
599  clk.setFreq(playImage->getFrequency());
600  audioPos = clk.getTicksTill(tapePos);
601  }
602  syncScheduled = false;
603 }
604 
605 
606 // class TapeCommand
607 
608 CassettePlayer::TapeCommand::TapeCommand(
609  CommandController& commandController_,
610  StateChangeDistributor& stateChangeDistributor_,
611  Scheduler& scheduler_)
612  : RecordedCommand(commandController_, stateChangeDistributor_,
613  scheduler_, "cassetteplayer")
614 {
615 }
616 
617 void CassettePlayer::TapeCommand::execute(
618  array_ref<TclObject> tokens, TclObject& result, EmuTime::param time)
619 {
620  auto& cassettePlayer = OUTER(CassettePlayer, tapeCommand);
621  if (tokens.size() == 1) {
622  // Returning Tcl lists here, similar to the disk commands in
623  // DiskChanger
624  result.addListElement(getName() + ':');
625  result.addListElement(cassettePlayer.getImageName().getResolved());
626 
627  TclObject options;
628  options.addListElement(cassettePlayer.getStateString());
629  result.addListElement(options);
630 
631  } else if (tokens[1] == "new") {
632  string directory = "taperecordings";
633  string prefix = "openmsx";
634  string extension = ".wav";
636  (tokens.size() == 3) ? tokens[2].getString() : string{},
637  directory, prefix, extension);
638  cassettePlayer.recordTape(Filename(filename), time);
639  result.setString("Created new cassette image file: " + filename +
640  ", inserted it and set recording mode.");
641 
642  } else if (tokens[1] == "insert" && tokens.size() == 3) {
643  try {
644  result.setString("Changing tape");
645  Filename filename(tokens[2].getString().str(), userFileContext());
646  cassettePlayer.playTape(filename, time);
647  } catch (MSXException& e) {
648  throw CommandException(e.getMessage());
649  }
650 
651  } else if (tokens[1] == "motorcontrol" && tokens.size() == 3) {
652  if (tokens[2] == "on") {
653  cassettePlayer.setMotorControl(true, time);
654  result.setString("Motor control enabled.");
655  } else if (tokens[2] == "off") {
656  cassettePlayer.setMotorControl(false, time);
657  result.setString("Motor control disabled.");
658  } else {
659  throw SyntaxError();
660  }
661 
662  } else if (tokens.size() != 2) {
663  throw SyntaxError();
664 
665  } else if (tokens[1] == "motorcontrol") {
666  result.setString(string("Motor control is ") +
667  (cassettePlayer.motorControl ? "on" : "off"));
668 
669  } else if (tokens[1] == "record") {
670  result.setString("TODO: implement this... (sorry)");
671 
672  } else if (tokens[1] == "play") {
673  if (cassettePlayer.getState() == CassettePlayer::RECORD) {
674  try {
675  result.setString("Play mode set, rewinding tape.");
676  cassettePlayer.playTape(
677  cassettePlayer.getImageName(), time);
678  } catch (MSXException& e) {
679  throw CommandException(e.getMessage());
680  }
681  } else if (cassettePlayer.getState() == CassettePlayer::STOP) {
682  throw CommandException("No tape inserted or tape at end!");
683  } else {
684  // PLAY mode
685  result.setString("Already in play mode.");
686  }
687 
688  } else if (tokens[1] == "eject") {
689  result.setString("Tape ejected");
690  cassettePlayer.removeTape(time);
691 
692  } else if (tokens[1] == "rewind") {
693  string r;
694  if (cassettePlayer.getState() == CassettePlayer::RECORD) {
695  try {
696  r = "First stopping recording... ";
697  cassettePlayer.playTape(
698  cassettePlayer.getImageName(), time);
699  } catch (MSXException& e) {
700  throw CommandException(e.getMessage());
701  }
702  }
703  cassettePlayer.rewind(time);
704  r += "Tape rewound";
705  result.setString(r);
706 
707  } else if (tokens[1] == "getpos") {
708  result.setDouble(cassettePlayer.getTapePos(time));
709 
710  } else if (tokens[1] == "getlength") {
711  result.setDouble(cassettePlayer.getTapeLength(time));
712 
713  } else {
714  try {
715  result.setString("Changing tape");
716  Filename filename(tokens[1].getString().str(), userFileContext());
717  cassettePlayer.playTape(filename, time);
718  } catch (MSXException& e) {
719  throw CommandException(e.getMessage());
720  }
721  }
722  //if (!cassettePlayer.getConnector()) {
723  // cassettePlayer.cliComm.printWarning("Cassetteplayer not plugged in.");
724  //}
725 }
726 
727 string CassettePlayer::TapeCommand::help(const vector<string>& tokens) const
728 {
729  string helptext;
730  if (tokens.size() >= 2) {
731  if (tokens[1] == "eject") {
732  helptext =
733  "Well, just eject the cassette from the cassette "
734  "player/recorder!";
735  } else if (tokens[1] == "rewind") {
736  helptext =
737  "Indeed, rewind the tape that is currently in the "
738  "cassette player/recorder...";
739  } else if (tokens[1] == "motorcontrol") {
740  helptext =
741  "Setting this to 'off' is equivalent to "
742  "disconnecting the black remote plug from the "
743  "cassette player: it makes the cassette player "
744  "run (if in play mode); the motor signal from the "
745  "MSX will be ignored. Normally this is set to "
746  "'on': the cassetteplayer obeys the motor control "
747  "signal from the MSX.";
748  } else if (tokens[1] == "play") {
749  helptext =
750  "Go to play mode. Only useful if you were in "
751  "record mode (which is currently the only other "
752  "mode available).";
753  } else if (tokens[1] == "new") {
754  helptext =
755  "Create a new cassette image. If the file name is "
756  "omitted, one will be generated in the default "
757  "directory for tape recordings. Implies going to "
758  "record mode (why else do you want a new cassette "
759  "image?).";
760  } else if (tokens[1] == "insert") {
761  helptext =
762  "Inserts the specified cassette image into the "
763  "cassette player, rewinds it and switches to play "
764  "mode.";
765  } else if (tokens[1] == "record") {
766  helptext =
767  "Go to record mode. NOT IMPLEMENTED YET. Will be "
768  "used to be able to resume recording to an "
769  "existing cassette image, previously inserted with "
770  "the insert command.";
771  } else if (tokens[1] == "getpos") {
772  helptext =
773  "Return the position of the tape, in seconds from "
774  "the beginning of the tape.";
775  } else if (tokens[1] == "getlength") {
776  helptext =
777  "Return the length of the tape in seconds.";
778  }
779  } else {
780  helptext =
781  "cassetteplayer eject "
782  ": remove tape from virtual player\n"
783  "cassetteplayer rewind "
784  ": rewind tape in virtual player\n"
785  "cassetteplayer motorcontrol "
786  ": enables or disables motor control (remote)\n"
787  "cassetteplayer play "
788  ": change to play mode (default)\n"
789  "cassetteplayer record "
790  ": change to record mode (NOT IMPLEMENTED YET)\n"
791  "cassetteplayer new [<filename>] "
792  ": create and insert new tape image file and go to record mode\n"
793  "cassetteplayer insert <filename> "
794  ": insert (a different) tape file\n"
795  "cassetteplayer getpos "
796  ": query the position of the tape\n"
797  "cassetteplayer getlength "
798  ": query the total length of the tape\n"
799  "cassetteplayer <filename> "
800  ": insert (a different) tape file\n";
801  }
802  return helptext;
803 }
804 
805 void CassettePlayer::TapeCommand::tabCompletion(vector<string>& tokens) const
806 {
807  if (tokens.size() == 2) {
808  static const char* const cmds[] = {
809  "eject", "rewind", "motorcontrol", "insert", "new",
810  "play", "getpos", "getlength",
811  //"record",
812  };
813  completeFileName(tokens, userFileContext(), cmds);
814  } else if ((tokens.size() == 3) && (tokens[1] == "insert")) {
815  completeFileName(tokens, userFileContext());
816  } else if ((tokens.size() == 3) && (tokens[1] == "motorcontrol")) {
817  static const char* const extra[] = { "on", "off" };
818  completeString(tokens, extra);
819  }
820 }
821 
822 bool CassettePlayer::TapeCommand::needRecord(array_ref<TclObject> tokens) const
823 {
824  return tokens.size() > 1;
825 }
826 
827 
828 static std::initializer_list<enum_string<CassettePlayer::State>> stateInfo = {
829  { "PLAY", CassettePlayer::PLAY },
830  { "RECORD", CassettePlayer::RECORD },
831  { "STOP", CassettePlayer::STOP }
832 };
834 
835 // version 1: initial version
836 // version 2: added checksum
837 template<typename Archive>
838 void CassettePlayer::serialize(Archive& ar, unsigned version)
839 {
840  if (recordImage) {
841  // buf, sampcnt
842  flushOutput();
843  }
844 
845  ar.serialize("casImage", casImage);
846 
847  Sha1Sum oldChecksum;
848  if (!ar.isLoader() && playImage) {
849  oldChecksum = playImage->getSha1Sum();
850  }
851  if (ar.versionAtLeast(version, 2)) {
852  string oldChecksumStr = oldChecksum.empty()
853  ? string{}
854  : oldChecksum.toString();
855  ar.serialize("checksum", oldChecksumStr);
856  oldChecksum = oldChecksumStr.empty()
857  ? Sha1Sum()
858  : Sha1Sum(oldChecksumStr);
859  }
860 
861  if (ar.isLoader()) {
862  FilePool& filePool = motherBoard.getReactor().getFilePool();
863  removeTape(getCurrentTime());
864  casImage.updateAfterLoadState();
865  if (!oldChecksum.empty() &&
866  !FileOperations::exists(casImage.getResolved())) {
867  auto file = filePool.getFile(FilePool::TAPE, oldChecksum);
868  if (file.is_open()) {
869  casImage.setResolved(file.getURL());
870  }
871  }
872  try {
873  insertTape(casImage);
874  } catch (MSXException&) {
875  if (oldChecksum.empty()) {
876  // It's OK if we cannot reinsert an empty
877  // image. One likely scenario for this case is
878  // the following:
879  // - cassetteplayer new myfile.wav
880  // - don't actually start saving to tape yet
881  // - create a savestate and load that state
882  // Because myfile.wav contains no data yet, it
883  // is deleted from the filesystem. So on a
884  // loadstate it won't be found.
885  } else {
886  throw;
887  }
888  }
889 
890  if (playImage && !oldChecksum.empty()) {
891  Sha1Sum newChecksum = playImage->getSha1Sum();
892  if (oldChecksum != newChecksum) {
893  motherBoard.getMSXCliComm().printWarning(
894  "The content of the tape " +
895  casImage.getResolved() +
896  " has changed since the time this "
897  "savestate was created. This might "
898  "result in emulation problems.");
899  }
900  }
901  }
902 
903  // only for RECORD
904  //double lastX;
905  //double lastY;
906  //double partialOut;
907  //double partialInterval;
908  //std::unique_ptr<WavWriter> recordImage;
909 
910  ar.serialize("tapePos", tapePos);
911  ar.serialize("prevSyncTime", prevSyncTime);
912  ar.serialize("audioPos", audioPos);
913  ar.serialize("state", state);
914  ar.serialize("lastOutput", lastOutput);
915  ar.serialize("motor", motor);
916  ar.serialize("motorControl", motorControl);
917 
918  if (ar.isLoader()) {
919  auto time = getCurrentTime();
920  if (playImage && (tapePos > playImage->getEndTime())) {
921  tapePos = playImage->getEndTime();
922  motherBoard.getMSXCliComm().printWarning("Tape position "
923  "beyond tape end! Setting tape position to end. "
924  "This can happen if you load a replay from an "
925  "older openMSX version with a different CAS-to-WAV "
926  "baud rate or when the tape image has been changed "
927  "compared to when the replay was created.");
928  }
929  if (state == RECORD) {
930  // TODO we don't support savestates in RECORD mode yet
931  motherBoard.getMSXCliComm().printWarning(
932  "Restoring a state where the MSX was saving to "
933  "tape is not yet supported. Emulation will "
934  "continue without actually saving.");
935  setState(STOP, getImageName(), time);
936  }
937  if (!playImage && (state == PLAY)) {
938  // This should only happen for manually edited
939  // savestates, though we shouldn't crash on it.
940  setState(STOP, getImageName(), time);
941  }
942  sync(time);
943  updateLoadingState(time);
944  }
945 }
948 
949 } // namespace openmsx
SERIALIZE_ENUM(CassettePlayer::State, stateInfo)
void setDouble(double value)
Definition: TclObject.cc:47
void plugHelper(Connector &connector, EmuTime::param time) override
static EmuDuration sec(unsigned x)
Definition: EmuDuration.hh:36
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:333
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
Represents something you can plug devices into.
Definition: Connector.hh:20
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
Connector * getConnector() const
Get the connector this Pluggable is plugged into.
Definition: Pluggable.hh:43
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:268
File getFile(FileType fileType, const Sha1Sum &sha1sum)
Search file with the given sha1sum.
Definition: FilePool.cc:316
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:167
void generateChannels(int **bufs, unsigned num) override
Abstract method to generate the actual sound data.
void printWarning(string_ref message)
Definition: CliComm.cc:20
Commands that directly influence the MSX state should send and events so that they can be recorded by...
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
double toDouble() const
Definition: EmuDuration.hh:46
EventDistributor & getEventDistributor()
Definition: Reactor.hh:78
virtual void update(UpdateType type, string_ref name, string_ref value)=0
const std::string & getMessage() const
Definition: MSXException.hh:13
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:172
void serialize(Archive &ar, unsigned version)
CassettePlayer(const HardwareConfig &hwConf)
virtual TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr)=0
Execute the given command.
bool empty() const
Definition: sha1.cc:241
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
void updateAfterLoadState()
After a loadstate we prefer to use the exact same file as before savestate.
Definition: Filename.cc:30
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:78
void update(bool newState)
Called by the device to indicate its loading state may have changed.
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:15
string parseCommandFileArgument(string_ref argument, string_ref directory, string_ref prefix, string_ref extension)
Helper function for parsing filename arguments in Tcl commands.
void setMotor(bool status, EmuTime::param time) override
Sets the cassette motor relay false = off true = on.
int16_t readSample(EmuTime::param time) override
Read wave data from cassette device.
This class represents a filename.
Definition: Filename.hh:17
void setFreq(unsigned freq)
Change the frequency at which this clock ticks.
Definition: DynamicClock.hh:86
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
bool exists(string_ref filename)
Does this file (directory) exists?
void setResolved(const std::string &resolved)
Change the resolved part of this filename E.g.
Definition: Filename.hh:44
CommandController & getCommandController()
FilePool & getFilePool()
Definition: Reactor.hh:87
void unplugHelper(EmuTime::param time) override
string_ref getDescription() const override
Description for this pluggable.
void setSignal(bool output, EmuTime::param time) override
Sets the cassette output signal false = low true = high.
FileContext userFileContext(string_ref savePath)
Definition: FileContext.cc:161
size_type size() const
Definition: array_ref.hh:61
std::string toString() const
Definition: sha1.cc:229
void addListElement(string_ref element)
Definition: TclObject.cc:69
const std::string & getName() const override
Name used to identify this pluggable.
void setString(string_ref value)
Definition: TclObject.cc:14
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
const std::string & getResolved() const
Definition: Filename.hh:25
const string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:407
#define OUTER(type, member)
Definition: outer.hh:38
unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
Definition: DynamicClock.hh:51
bool empty() const
Convenience method to test for empty filename.
Definition: Filename.cc:43
unsigned getInputRate() const
Definition: SoundDevice.hh:79
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:125
#define UNREACHABLE
Definition: unreachable.hh:35