openMSX
LaserdiscPlayer.cc
Go to the documentation of this file.
1 #include "LaserdiscPlayer.hh"
2 #include "CommandException.hh"
3 #include "CommandController.hh"
4 #include "EventDistributor.hh"
5 #include "FileContext.hh"
6 #include "DeviceConfig.hh"
7 #include "HardwareConfig.hh"
8 #include "XMLElement.hh"
9 #include "CassettePort.hh"
10 #include "CliComm.hh"
11 #include "Display.hh"
12 #include "GlobalSettings.hh"
13 #include "Reactor.hh"
14 #include "ReverseManager.hh"
15 #include "MSXMotherBoard.hh"
16 #include "PioneerLDControl.hh"
17 #include "LDRenderer.hh"
18 #include "RendererFactory.hh"
19 #include "Math.hh"
20 #include "likely.hh"
21 #include "one_of.hh"
22 #include <cstdint>
23 #include <cstdlib>
24 #include <iostream>
25 #include <memory>
26 
27 using std::string;
28 
29 namespace openmsx {
30 
31 // Command
32 
33 LaserdiscPlayer::Command::Command(
34  CommandController& commandController_,
35  StateChangeDistributor& stateChangeDistributor_,
36  Scheduler& scheduler_)
37  : RecordedCommand(commandController_, stateChangeDistributor_,
38  scheduler_, "laserdiscplayer")
39 {
40 }
41 
42 void LaserdiscPlayer::Command::execute(
43  span<const TclObject> tokens, TclObject& result, EmuTime::param time)
44 {
45  auto& laserdiscPlayer = OUTER(LaserdiscPlayer, laserdiscCommand);
46  if (tokens.size() == 1) {
47  // Returning Tcl lists here, similar to the disk commands in
48  // DiskChanger
49  result.addListElement(tmpStrCat(getName(), ':'),
50  laserdiscPlayer.getImageName().getResolved());
51  } else if (tokens[1] == "eject") {
52  checkNumArgs(tokens, 2, Prefix{2}, nullptr);
53  result = "Ejecting laserdisc.";
54  laserdiscPlayer.eject(time);
55  } else if (tokens[1] == "insert") {
56  checkNumArgs(tokens, 3, "filename");
57  try {
58  result = "Changing laserdisc.";
59  laserdiscPlayer.setImageName(string(tokens[2].getString()), time);
60  } catch (MSXException& e) {
61  throw CommandException(std::move(e).getMessage());
62  }
63  } else {
64  throw SyntaxError();
65  }
66 }
67 
68 string LaserdiscPlayer::Command::help(span<const TclObject> tokens) const
69 {
70  if (tokens.size() >= 2) {
71  if (tokens[1] == "insert") {
72  return "Inserts the specified laserdisc image into "
73  "the laserdisc player.";
74  } else if (tokens[1] == "eject") {
75  return "Eject the laserdisc.";
76  }
77  }
78  return "laserdiscplayer insert <filename> "
79  ": insert a (different) laserdisc image\n"
80  "laserdiscplayer eject "
81  ": eject the laserdisc\n";
82 }
83 
84 void LaserdiscPlayer::Command::tabCompletion(std::vector<string>& tokens) const
85 {
86  if (tokens.size() == 2) {
87  using namespace std::literals;
88  static constexpr std::array extra = {"eject"sv, "insert"sv};
89  completeString(tokens, extra);
90  } else if (tokens.size() == 3 && tokens[1] == "insert") {
91  completeFileName(tokens, userFileContext());
92  }
93 }
94 
95 // LaserdiscPlayer
96 
97 constexpr unsigned DUMMY_INPUT_RATE = 44100; // actual rate depends on .ogg file
98 
100  const HardwareConfig& hwConf, PioneerLDControl& ldControl_)
101  : ResampledSoundDevice(hwConf.getMotherBoard(), "laserdiscplayer",
102  "Laserdisc Player", 1, DUMMY_INPUT_RATE, true)
103  , syncAck (hwConf.getMotherBoard().getScheduler())
104  , syncOdd (hwConf.getMotherBoard().getScheduler())
105  , syncEven(hwConf.getMotherBoard().getScheduler())
106  , motherBoard(hwConf.getMotherBoard())
107  , ldControl(ldControl_)
108  , laserdiscCommand(motherBoard.getCommandController(),
109  motherBoard.getStateChangeDistributor(),
110  motherBoard.getScheduler())
111  , sampleClock(EmuTime::zero())
112  , start(EmuTime::zero())
113  , muteLeft(false)
114  , muteRight(false)
115  , remoteState(REMOTE_IDLE)
116  , remoteLastEdge(EmuTime::zero())
117  , remoteLastBit(false)
118  , remoteProtocol(IR_NONE)
119  , ack(false)
120  , seeking(false)
121  , playerState(PLAYER_STOPPED)
122  , autoRunSetting(
123  motherBoard.getCommandController(), "autorunlaserdisc",
124  "automatically try to run Laserdisc", true)
125  , loadingIndicator(
126  motherBoard.getReactor().getGlobalSettings().getThrottleManager())
127  , sampleReads(0)
128 {
129  motherBoard.getCassettePort().setLaserdiscPlayer(this);
130 
131  Reactor& reactor = motherBoard.getReactor();
132  reactor.getDisplay().attach(*this);
133 
134  createRenderer();
136  scheduleDisplayStart(getCurrentTime());
137 
138  static XMLElement* xml = [] {
139  auto& doc = XMLDocument::getStaticDocument();
140  auto* result = doc.allocateElement("laserdiscplayer");
141  result->setFirstChild(doc.allocateElement("sound"))
142  ->setFirstChild(doc.allocateElement("volume", "30000"));
143  return result;
144  }();
145  registerSound(DeviceConfig(hwConf, *xml));
146 }
147 
149 {
150  unregisterSound();
151  Reactor& reactor = motherBoard.getReactor();
152  reactor.getDisplay().detach(*this);
154 }
155 
156 void LaserdiscPlayer::scheduleDisplayStart(EmuTime::param time)
157 {
158  Clock<60000, 1001> frameClock(time);
159  // The video is 29.97Hz, however we need to do vblank processing
160  // at the full 59.94Hz
161  syncOdd .setSyncPoint(frameClock + 1);
162  syncEven.setSyncPoint(frameClock + 2);
163 }
164 
165 // The protocol used to communicate over the cable for commands to the
166 // laserdisc player is the NEC infrared protocol with minor deviations:
167 // 1) The leader pulse and space is a little shorter.
168 // 2) The remote does not send NEC repeats; full NEC codes are repeated
169 // after 20ms. The main unit does not understand NEC repeats.
170 // 3) No carrier modulation is done over the ext protocol.
171 //
172 // My Laserdisc player is an Pioneer LD-700 which has a remote called
173 // the CU-700. This is much like the CU-CLD106 which is described
174 // here: http://lirc.sourceforge.net/remotes/pioneer/CU-CLD106
175 // The codes and protocol are exactly the same.
176 void LaserdiscPlayer::extControl(bool bit, EmuTime::param time)
177 {
178  if (remoteLastBit == bit) return;
179  remoteLastBit = bit;
180 
181  // The tolerance here is based on actual measurements of an LD-700
182  EmuDuration duration = time - remoteLastEdge;
183  remoteLastEdge = time;
184  unsigned usec = duration.getTicksAt(1000000); // microseconds
185 
186  switch (remoteState) {
187  case REMOTE_IDLE:
188  if (bit) {
189  remoteBits = remoteBitNr = 0;
190  remoteState = REMOTE_HEADER_PULSE;
191  }
192  break;
193  case REMOTE_HEADER_PULSE:
194  if (5800 <= usec && usec < 11200) {
195  remoteState = NEC_HEADER_SPACE;
196  } else {
197  remoteState = REMOTE_IDLE;
198  }
199  break;
200  case NEC_HEADER_SPACE:
201  if (3400 <= usec && usec < 6200) {
202  remoteState = NEC_BITS_PULSE;
203  } else {
204  remoteState = REMOTE_IDLE;
205  }
206  break;
207  case NEC_BITS_PULSE:
208  if (usec >= 380 && usec < 1070) {
209  remoteState = NEC_BITS_SPACE;
210  } else {
211  remoteState = REMOTE_IDLE;
212  }
213  break;
214  case NEC_BITS_SPACE:
215  if (1260 <= usec && usec < 4720) {
216  // bit 1
217  remoteBits |= 1 << remoteBitNr;
218  } else if (usec < 300 || usec >= 1065) {
219  // error
220  remoteState = REMOTE_IDLE;
221  break;
222  }
223 
224  // since it does not matter how long the trailing pulse
225  // is, we process the button here. Note that real hardware
226  // needs the trailing pulse to be at least 200┬Ás
227  if (++remoteBitNr == 32) {
228  byte custom = ( remoteBits >> 0) & 0xff;
229  byte customCompl = (~remoteBits >> 8) & 0xff;
230  byte code = ( remoteBits >> 16) & 0xff;
231  byte codeCompl = (~remoteBits >> 24) & 0xff;
232  if (custom == customCompl &&
233  custom == 0xa8 &&
234  code == codeCompl) {
235  submitRemote(IR_NEC, code);
236  }
237  remoteState = REMOTE_IDLE;
238  } else {
239  remoteState = NEC_BITS_PULSE;
240  }
241 
242  break;
243  }
244 }
245 
246 void LaserdiscPlayer::submitRemote(RemoteProtocol protocol, unsigned code)
247 {
248  // The END command for seeking/waiting acknowledges repeats,
249  // Esh's Aurunmilla needs play as well.
250  if (protocol != remoteProtocol || code != remoteCode ||
251  (protocol == IR_NEC && (code == one_of(0x42u, 0x17u)))) {
252  remoteProtocol = protocol;
253  remoteCode = code;
254  remoteVblanksBack = 0;
255  remoteExecuteDelayed = true;
256  } else {
257  // remote ignored
258  remoteVblanksBack = 0;
259  remoteExecuteDelayed = false;
260  }
261 }
262 
264 {
265  return renderer->getRawFrame();
266 }
267 
268 void LaserdiscPlayer::setAck(EmuTime::param time, int wait)
269 {
270  // activate ACK for 'wait' milliseconds
271  syncAck.removeSyncPoint();
272  syncAck.setSyncPoint(time + EmuDuration::msec(wait));
273  ack = true;
274 }
275 
276 void LaserdiscPlayer::remoteButtonNEC(unsigned code, EmuTime::param time)
277 {
278 #ifdef DEBUG
279  string f;
280  switch (code) {
281  case 0x47: f = "C+"; break; // Increase playing speed
282  case 0x46: f = "C-"; break; // Decrease playing speed
283  case 0x43: f = "D+"; break; // Show Frame# & Chapter# OSD
284  case 0x4b: f = "L+"; break; // right
285  case 0x49: f = "L-"; break; // left
286  case 0x4a: f = "L@"; break; // stereo
287  case 0x58: f = "M+"; break; // multi speed forwards
288  case 0x55: f = "M-"; break; // multi speed backwards
289  case 0x17: f = "P+"; break; // play
290  case 0x16: f = "P@"; break; // stop
291  case 0x18: f = "P/"; break; // pause
292  case 0x54: f = "S+"; break; // frame step forward
293  case 0x50: f = "S-"; break; // frame step backwards
294  case 0x45: f = "X+"; break; // clear
295  case 0x41: f = 'F'; break; // seek frame
296  case 0x40: f = 'C'; break; // seek chapter
297  case 0x42: f = "END"; break; // done seek frame/chapter
298  case 0x00: f = '0'; break;
299  case 0x01: f = '1'; break;
300  case 0x02: f = '2'; break;
301  case 0x03: f = '3'; break;
302  case 0x04: f = '4'; break;
303  case 0x05: f = '5'; break;
304  case 0x06: f = '6'; break;
305  case 0x07: f = '7'; break;
306  case 0x08: f = '8'; break;
307  case 0x09: f = '9'; break;
308  case 0x5f: f = "WAIT FRAME"; break;
309 
310  case 0x53: // previous chapter
311  case 0x52: // next chapter
312  default: break;
313  }
314 
315  if (!f.empty()) {
316  std::cerr << "LaserdiscPlayer::remote " << f << '\n';
317  } else {
318  std::cerr << "LaserdiscPlayer::remote unknown " << std::hex << code << '\n';
319  }
320 #endif
321  // When not playing the following buttons work
322  // 0x17: start playing (ack sent)
323  // 0x16: eject (no ack)
324  // 0x49, 0x4a, 0x4b (ack sent)
325  // if 0x49 is a repeat then no ACK is sent
326  // if 0x49 is followed by 0x4a then ACK is sent
327  if (code == one_of(0x49u, 0x4au, 0x4bu)) {
328  updateStream(time);
329 
330  switch (code) {
331  case 0x4b: // L+ (both channels play the left channel)
332  stereoMode = LEFT;
333  break;
334  case 0x49: // L- (both channels play the right channel)
335  stereoMode = RIGHT;
336  break;
337  case 0x4a: // L@ (normal stereo)
338  stereoMode = STEREO;
339  break;
340  }
341 
342  setAck(time, 46);
343  } else if (playerState == PLAYER_STOPPED) {
344  switch (code) {
345  case 0x16: // P@
346  motherBoard.getMSXCliComm().printWarning(
347  "ejecting laserdisc");
348  eject(time);
349  break;
350  case 0x17: // P+
351  play(time);
352  break;
353  }
354 
355  // During playing, playing will be acked if not repeated
356  // within less than 115ms
357  } else {
358  // FIXME: while seeking, only a small subset of buttons work
359  bool nonseekack = true;
360 
361  switch (code) {
362  case 0x5f:
363  seekState = SEEK_WAIT;
364  seekNum = 0;
365  stillOnWaitFrame = false;
366  nonseekack = false;
367  break;
368  case 0x41:
369  seekState = SEEK_FRAME;
370  seekNum = 0;
371  break;
372  case 0x40:
373  seekState = SEEK_CHAPTER;
374  seekNum = 0;
375  nonseekack = video->getChapter(0) != 0;
376  break;
377  case 0x00:
378  case 0x01:
379  case 0x02:
380  case 0x03:
381  case 0x04:
382  case 0x05:
383  case 0x06:
384  case 0x07:
385  case 0x08:
386  case 0x09:
387  seekNum = seekNum * 10 + code;
388  break;
389  case 0x42:
390  switch (seekState) {
391  case SEEK_FRAME:
392  seekState = SEEK_NONE;
393  seekFrame(seekNum % 100000, time);
394  nonseekack = false;
395  break;
396  case SEEK_CHAPTER:
397  seekState = SEEK_NONE;
398  seekChapter(seekNum % 100, time);
399  nonseekack = false;
400  break;
401  case SEEK_WAIT:
402  seekState = SEEK_NONE;
403  waitFrame = seekNum % 100000;
404  if (waitFrame >= 101 && waitFrame < 200) {
405  auto frame = video->getChapter(
406  int(waitFrame - 100));
407  if (frame) waitFrame = frame;
408  }
409  break;
410  default:
411  seekState = SEEK_NONE;
412  break;
413  }
414  break;
415  case 0x45: // Clear "X+"
416  if (seekState != SEEK_NONE && seekNum != 0) {
417  seekNum = 0;
418  } else {
419  seekState = SEEK_NONE;
420  seekNum = 0;
421  }
422  waitFrame = 0;
423  break;
424  case 0x18: // P/
425  pause(time);
426  nonseekack = false;
427  break;
428  case 0x17: // P+
429  play(time);
430  nonseekack = false;
431  break;
432  case 0x16: // P@ (stop/eject)
433  stop(time);
434  nonseekack = false;
435  break;
436  case 0xff:
437  nonseekack = false;
438  seekState = SEEK_NONE;
439  break;
440  case 0x54: // S+ (frame step forward)
441  if (seekState == SEEK_WAIT) {
442  stillOnWaitFrame = true;
443  } else {
444  stepFrame(true);
445  }
446  break;
447  case 0x50: // S- (frame step backwards)
448  stepFrame(false);
449  break;
450  case 0x55: // M- (multispeed backwards)
451  // Not supported
452  motherBoard.getMSXCliComm().printWarning(
453  "The Laserdisc player received a command to "
454  "play backwards (M-). This is currently not "
455  "supported.");
456  nonseekack = false;
457  break;
458  case 0x58: // M+ (multispeed forwards)
459  playerState = PLAYER_MULTISPEED;
460  setFrameStep();
461  break;
462  case 0x46: // C- (play slower)
463  if (playingSpeed >= SPEED_STEP1) {
464  playingSpeed--;
465  frameStep = 1; // FIXME: is this correct?
466  }
467  break;
468  case 0x47: // C+ (play faster)
469  if (playingSpeed <= SPEED_X2) {
470  playingSpeed++;
471  frameStep = 1; // FIXME: is this correct?
472  }
473  break;
474  default:
475  motherBoard.getMSXCliComm().printWarning(
476  "The Laserdisc player received an unknown "
477  "command 0x", hex_string<2>(code));
478  nonseekack = false;
479  break;
480  }
481 
482  if (nonseekack) {
483  // All ACKs for operations which do not
484  // require seeking
485  setAck(time, 46);
486  }
487  }
488 }
489 
490 void LaserdiscPlayer::execSyncAck(EmuTime::param time)
491 {
492  updateStream(time);
493 
494  if (seeking && playerState == PLAYER_PLAYING) {
495  sampleClock.advance(time);
496  }
497 
498  ack = false;
499  seeking = false;
500 }
501 
502 void LaserdiscPlayer::execSyncFrame(EmuTime::param time, bool odd)
503 {
504  updateStream(time);
505 
506  if (!odd || (video && video->getFrameRate() == 60)) {
507  if ((playerState != PLAYER_STOPPED) &&
508  (currentFrame > video->getFrames())) {
509  playerState = PLAYER_STOPPED;
510  }
511 
512  if (auto* rawFrame = renderer->getRawFrame()) {
513  renderer->frameStart(time);
514 
515  if (isVideoOutputAvailable(time)) {
516  auto frame = currentFrame;
517  if (video->getFrameRate() == 60) {
518  frame *= 2;
519  if (odd) frame--;
520  }
521 
522  video->getFrameNo(*rawFrame, frame);
523 
524  if (!odd) {
525  nextFrame(time);
526  }
527  } else {
528  renderer->drawBlank(0, 128, 196);
529  }
530  renderer->frameEnd();
531  }
532 
533  // Update throttling
534  loadingIndicator.update(seeking || sampleReads > 500);
535  sampleReads = 0;
536 
537  if (!odd) {
538  scheduleDisplayStart(time);
539  }
540  }
541 
542  // Processing of the remote control happens at each frame
543  // (even and odd, so at 59.94Hz)
544  if (remoteProtocol == IR_NEC) {
545  if (remoteExecuteDelayed) {
546  remoteButtonNEC(remoteCode, time);
547  }
548 
549  if (++remoteVblanksBack > 6) {
550  remoteProtocol = IR_NONE;
551  }
552  }
553  remoteExecuteDelayed = false;
554 }
555 
556 void LaserdiscPlayer::setFrameStep()
557 {
558  switch (playingSpeed) {
559  case SPEED_X3:
560  case SPEED_X2:
561  case SPEED_X1:
562  frameStep = 1;
563  break;
564  case SPEED_1IN2:
565  frameStep = 2;
566  break;
567  case SPEED_1IN4:
568  frameStep = 4;
569  break;
570  case SPEED_1IN8:
571  frameStep = 8;
572  break;
573  case SPEED_1IN16:
574  frameStep = 16;
575  break;
576  case SPEED_STEP1:
577  frameStep = 30;
578  break;
579  case SPEED_STEP3:
580  frameStep = 90;
581  break;
582  }
583 }
584 
585 void LaserdiscPlayer::nextFrame(EmuTime::param time)
586 {
587  if (waitFrame && waitFrame == currentFrame) {
588  // Leave ACK raised until the next command
589  ack = true;
590  waitFrame = 0;
591 
592  if (stillOnWaitFrame) {
593  playingFromSample = getCurrentSample(time);
594  playerState = PLAYER_STILL;
595  stillOnWaitFrame = false;
596  }
597  }
598 
599  if (playerState == PLAYER_MULTISPEED) {
600  if (--frameStep) {
601  return;
602  }
603 
604  switch (playingSpeed) {
605  case SPEED_X3:
606  currentFrame += 3;
607  break;
608  case SPEED_X2:
609  currentFrame += 2;
610  break;
611  default:
612  currentFrame += 1;
613  break;
614  }
615  setFrameStep();
616  } else if (playerState == PLAYER_PLAYING) {
617  currentFrame++;
618  }
619 
620  // freeze if stop frame
621  if ((playerState == one_of(PLAYER_PLAYING, PLAYER_MULTISPEED))
622  && video->stopFrame(currentFrame)) {
623  // stop frame reached
624  playingFromSample = getCurrentSample(time);
625  playerState = PLAYER_STILL;
626  }
627 }
628 
629 void LaserdiscPlayer::setImageName(string newImage, EmuTime::param time)
630 {
631  stop(time);
632  oggImage = Filename(std::move(newImage), userFileContext());
633  video.emplace(oggImage, motherBoard.getMSXCliComm());
634 
635  unsigned inputRate = video->getSampleRate();
636  sampleClock.setFreq(inputRate);
637  if (inputRate != getInputRate()) {
638  setInputRate(inputRate);
639  createResampler();
640  }
641 }
642 
643 int LaserdiscPlayer::signalEvent(const Event& event) noexcept
644 {
645  if (getType(event) == EventType::BOOT && video) {
646  autoRun();
647  }
648  return 0;
649 }
650 
651 void LaserdiscPlayer::autoRun()
652 {
653  if (!autoRunSetting.getBoolean()) return;
654  if (motherBoard.getReverseManager().isReplaying()) {
655  // See comments in CassettePlayer::autoRun()
656  return;
657  }
658 
659  string var = "::auto_run_ld_counter";
660  string command = strCat(
661  "if ![info exists ", var, "] { set ", var, " 0 }\n"
662  "incr ", var, "\n"
663  "after time 2 \"if $", var, "==\\$", var, " { "
664  "type_via_keyboard 1CALLLD\\\\r }\"");
665  try {
666  motherBoard.getCommandController().executeCommand(command);
667  } catch (CommandException& e) {
668  motherBoard.getMSXCliComm().printWarning(
669  "Error executing loading instruction for AutoRun: ",
670  e.getMessage(), "\n Please report a bug.");
671  }
672 }
673 
674 void LaserdiscPlayer::generateChannels(float** buffers, unsigned num)
675 {
676  // Single channel device: replace content of buffers[0] (not add to it).
677  if (playerState != PLAYER_PLAYING || seeking || (muteLeft && muteRight)) {
678  buffers[0] = nullptr;
679  return;
680  }
681 
682  unsigned pos = 0;
683  size_t currentSample;
684 
685  if (unlikely(!sampleClock.before(start))) {
686  // Before playing of sounds begins
687  EmuDuration duration = sampleClock.getTime() - start;
688  unsigned len = duration.getTicksAt(video->getSampleRate());
689  if (len >= num) {
690  buffers[0] = nullptr;
691  return;
692  }
693 
694  for (; pos < len; ++pos) {
695  buffers[0][pos * 2 + 0] = 0.0f;
696  buffers[0][pos * 2 + 1] = 0.0f;
697  }
698 
699  currentSample = playingFromSample;
700  } else {
701  currentSample = getCurrentSample(start);
702  }
703 
704  unsigned drift = video->getSampleRate() / 30;
705 
706  if (currentSample > (lastPlayedSample + drift) ||
707  (currentSample + drift) < lastPlayedSample) {
708  // audio drift
709  lastPlayedSample = currentSample;
710  }
711 
712  int left = stereoMode == RIGHT ? 1 : 0;
713  int right = stereoMode == LEFT ? 0 : 1;
714 
715  while (pos < num) {
716  const AudioFragment* audio = video->getAudio(lastPlayedSample);
717 
718  if (!audio) {
719  if (pos == 0) {
720  buffers[0] = nullptr;
721  break;
722  } else {
723  for (; pos < num; ++pos) {
724  buffers[0][pos * 2 + 0] = 0.0f;
725  buffers[0][pos * 2 + 1] = 0.0f;
726  }
727  }
728  } else {
729  auto offset = unsigned(lastPlayedSample - audio->position);
730  unsigned len = std::min(audio->length - offset, num - pos);
731 
732  // maybe muting should be moved out of the loop?
733  for (unsigned i = 0; i < len; ++i, ++pos) {
734  buffers[0][pos * 2 + 0] = muteLeft ? 0.0f :
735  audio->pcm[left][offset + i];
736  buffers[0][pos * 2 + 1] = muteRight ? 0.0f :
737  audio->pcm[right][offset + i];
738  }
739 
740  lastPlayedSample += len;
741  }
742  }
743 }
744 
745 float LaserdiscPlayer::getAmplificationFactorImpl() const
746 {
747  return 2.0f;
748 }
749 
750 bool LaserdiscPlayer::updateBuffer(unsigned length, float* buffer,
751  EmuTime::param time)
752 {
753  bool result = ResampledSoundDevice::updateBuffer(length, buffer, time);
754  start = time; // current end-time is next start-time
755  return result;
756 }
757 
758 void LaserdiscPlayer::setMuting(bool left, bool right, EmuTime::param time)
759 {
760  updateStream(time);
761  muteLeft = left;
762  muteRight = right;
763 }
764 
765 void LaserdiscPlayer::play(EmuTime::param time)
766 {
767  if (video) {
768  updateStream(time);
769 
770  if (seeking) {
771  // Do not ACK, play while seeking
772  } else if (playerState == PLAYER_STOPPED) {
773  // Disk needs to spin up, which takes 9.6s on
774  // my Pioneer LD-92000. Also always seek to
775  // beginning (confirmed on real MSX and LD)
776  video->seek(1, 0);
777  lastPlayedSample = 0;
778  playingFromSample = 0;
779  currentFrame = 1;
780  // Note that with "fullspeedwhenloading" this
781  // should be reduced to.
782  setAck(time, 9600);
783  seekState = SEEK_NONE;
784  seeking = true;
785  waitFrame = 0;
786  stereoMode = STEREO;
787  playingSpeed = SPEED_1IN4;
788  } else if (playerState == PLAYER_PLAYING) {
789  // If Play command is issued while the player
790  // is already playing, then if no ACK is sent then
791  // Astron Belt will send LD1100 commands
792  setAck(time, 46);
793  } else if (playerState == PLAYER_MULTISPEED) {
794  // Should be hearing stuff again
795  playingFromSample = (currentFrame - 1ll) * 1001ll *
796  video->getSampleRate() / 30000ll;
797  sampleClock.advance(time);
798  setAck(time, 46);
799  } else {
800  // STILL or PAUSED
801  sampleClock.advance(time);
802  setAck(time, 46);
803  }
804  playerState = PLAYER_PLAYING;
805  }
806 }
807 
808 size_t LaserdiscPlayer::getCurrentSample(EmuTime::param time)
809 {
810  switch(playerState) {
811  case PLAYER_PAUSED:
812  case PLAYER_STILL:
813  return playingFromSample;
814  default:
815  return playingFromSample + sampleClock.getTicksTill(time);
816  }
817 }
818 
819 void LaserdiscPlayer::pause(EmuTime::param time)
820 {
821  if (playerState != PLAYER_STOPPED) {
822  updateStream(time);
823 
824  if (playerState == PLAYER_PLAYING) {
825  playingFromSample = getCurrentSample(time);
826  } else if (playerState == PLAYER_MULTISPEED) {
827  playingFromSample = (currentFrame - 1ll) * 1001ll *
828  video->getSampleRate() / 30000ll;
829  sampleClock.advance(time);
830  }
831 
832  playerState = PLAYER_PAUSED;
833  setAck(time, 46);
834  }
835 }
836 
837 void LaserdiscPlayer::stop(EmuTime::param time)
838 {
839  if (playerState != PLAYER_STOPPED) {
840  updateStream(time);
841  playerState = PLAYER_STOPPED;
842  }
843 }
844 
845 void LaserdiscPlayer::eject(EmuTime::param time)
846 {
847  stop(time);
848  oggImage = {};
849  video.reset();
850 }
851 
852 // Step one frame forwards or backwards. The frame will be visible and
853 // we won't be playing afterwards
854 void LaserdiscPlayer::stepFrame(bool forwards)
855 {
856  bool needseek = false;
857 
858  // Note that on real hardware, the screen goes dark momentarily
859  // if you try to step before the first frame or after the last one
860  if (playerState == PLAYER_STILL) {
861  if (forwards) {
862  if (currentFrame < video->getFrames()) {
863  currentFrame++;
864  }
865  } else {
866  if (currentFrame > 1) {
867  currentFrame--;
868  needseek = true;
869  }
870  }
871  }
872 
873  playerState = PLAYER_STILL;
874  int64_t samplePos = (currentFrame - 1ll) * 1001ll *
875  video->getSampleRate() / 30000ll;
876  playingFromSample = samplePos;
877 
878  if (needseek) {
879  if (video->getFrameRate() == 60)
880  video->seek(currentFrame * 2, samplePos);
881  else
882  video->seek(currentFrame, samplePos);
883  }
884 }
885 
886 void LaserdiscPlayer::seekFrame(size_t toFrame, EmuTime::param time)
887 {
888  if ((playerState != PLAYER_STOPPED) && video) {
889  updateStream(time);
890 
891  if (toFrame <= 0) toFrame = 1;
892  if (toFrame > video->getFrames()) toFrame = video->getFrames();
893 
894  // Seek time needs to be emulated correctly since
895  // e.g. Astron Belt does not wait for the seek
896  // to complete, it simply assumes a certain
897  // delay.
898  //
899  // This calculation is based on measurements on
900  // a Pioneer LD-92000.
901  auto dist = std::abs(int64_t(toFrame) - int64_t(currentFrame));
902  int seektime = (dist < 1000) // time in ms
903  ? (dist + 300)
904  : (1800 + dist / 12);
905 
906  int64_t samplePos = (toFrame - 1ll) * 1001ll *
907  video->getSampleRate() / 30000ll;
908 
909  if (video->getFrameRate() == 60) {
910  video->seek(toFrame * 2, samplePos);
911  } else {
912  video->seek(toFrame, samplePos);
913  }
914  playerState = PLAYER_STILL;
915  playingFromSample = samplePos;
916  currentFrame = toFrame;
917 
918  // Seeking clears the frame to wait for
919  waitFrame = 0;
920 
921  seeking = true;
922  setAck(time, seektime);
923  }
924 }
925 
926 void LaserdiscPlayer::seekChapter(int chapter, EmuTime::param time)
927 {
928  if ((playerState != PLAYER_STOPPED) && video) {
929  auto frameNo = video->getChapter(chapter);
930  if (!frameNo) return;
931  seekFrame(frameNo, time);
932  }
933 }
934 
935 int16_t LaserdiscPlayer::readSample(EmuTime::param time)
936 {
937  // Here we should return the value of the sample on the
938  // right audio channel, ignoring muting (this is done in the MSX)
939  // but honouring the stereo mode as this is done in the
940  // Laserdisc player
941  if (playerState == PLAYER_PLAYING && !seeking) {
942  auto sample = getCurrentSample(time);
943  if (const AudioFragment* audio = video->getAudio(sample)) {
944  ++sampleReads;
945  int channel = stereoMode == LEFT ? 0 : 1;
946  return int(audio->pcm[channel][sample - audio->position]
947  * 32767.f);
948  }
949  }
950  return 0;
951 }
952 
953 bool LaserdiscPlayer::isVideoOutputAvailable(EmuTime::param time)
954 {
955  updateStream(time);
956 
957  bool videoOut = [&] {
958  switch (playerState) {
959  case PLAYER_PLAYING:
960  case PLAYER_MULTISPEED:
961  case PLAYER_STILL:
962  return !seeking;
963  default:
964  return false;
965  }
966  }();
967  ldControl.videoIn(videoOut);
968 
969  return videoOut;
970 }
971 
972 void LaserdiscPlayer::preVideoSystemChange() noexcept
973 {
974  renderer.reset();
975 }
976 
977 void LaserdiscPlayer::postVideoSystemChange() noexcept
978 {
979  createRenderer();
980 }
981 
982 void LaserdiscPlayer::createRenderer()
983 {
984  Display& display = getMotherBoard().getReactor().getDisplay();
985  renderer = RendererFactory::createLDRenderer(*this, display);
986 }
987 
988 static constexpr std::initializer_list<enum_string<LaserdiscPlayer::RemoteState>> RemoteStateInfo = {
989  { "IDLE", LaserdiscPlayer::REMOTE_IDLE },
990  { "HEADER_PULSE", LaserdiscPlayer::REMOTE_HEADER_PULSE },
991  { "NEC_HEADER_SPACE", LaserdiscPlayer::NEC_HEADER_SPACE },
992  { "NEC_BITS_PULSE", LaserdiscPlayer::NEC_BITS_PULSE },
993  { "NEC_BITS_SPACE", LaserdiscPlayer::NEC_BITS_SPACE },
994 };
996 
997 static constexpr std::initializer_list<enum_string<LaserdiscPlayer::PlayerState>> PlayerStateInfo = {
998  { "STOPPED", LaserdiscPlayer::PLAYER_STOPPED },
999  { "PLAYING", LaserdiscPlayer::PLAYER_PLAYING },
1000  { "MULTISPEED", LaserdiscPlayer::PLAYER_MULTISPEED },
1001  { "PAUSED", LaserdiscPlayer::PLAYER_PAUSED },
1002  { "STILL", LaserdiscPlayer::PLAYER_STILL }
1003 };
1005 
1006 static constexpr std::initializer_list<enum_string<LaserdiscPlayer::SeekState>> SeekStateInfo = {
1007  { "NONE", LaserdiscPlayer::SEEK_NONE },
1008  { "CHAPTER", LaserdiscPlayer::SEEK_CHAPTER },
1009  { "FRAME", LaserdiscPlayer::SEEK_FRAME },
1010  { "WAIT", LaserdiscPlayer::SEEK_WAIT }
1011 };
1013 
1014 static constexpr std::initializer_list<enum_string<LaserdiscPlayer::StereoMode>> StereoModeInfo = {
1015  { "LEFT", LaserdiscPlayer::LEFT },
1016  { "RIGHT", LaserdiscPlayer::RIGHT },
1017  { "STEREO", LaserdiscPlayer::STEREO }
1018 };
1020 
1021 static constexpr std::initializer_list<enum_string<LaserdiscPlayer::RemoteProtocol>> RemoteProtocolInfo = {
1022  { "NONE", LaserdiscPlayer::IR_NONE },
1023  { "NEC", LaserdiscPlayer::IR_NEC },
1024 };
1026 
1027 // version 1: initial version
1028 // version 2: added 'stillOnWaitFrame'
1029 // version 3: reversed bit order of 'remoteBits' and 'remoteCode'
1030 // version 4: removed 'userData' from Schedulable
1031 template<typename Archive>
1032 void LaserdiscPlayer::serialize(Archive& ar, unsigned version)
1033 {
1034  // Serialize remote control
1035  ar.serialize("RemoteState", remoteState);
1036  if (remoteState != REMOTE_IDLE) {
1037  ar.serialize("RemoteBitNr", remoteBitNr,
1038  "RemoteBits", remoteBits);
1039  if (ar.versionBelow(version, 3)) {
1040  assert(Archive::IS_LOADER);
1041  remoteBits = Math::reverseNBits(remoteBits, remoteBitNr);
1042  }
1043  }
1044  ar.serialize("RemoteLastBit", remoteLastBit,
1045  "RemoteLastEdge", remoteLastEdge,
1046  "RemoteProtocol", remoteProtocol);
1047  if (remoteProtocol != IR_NONE) {
1048  ar.serialize("RemoteCode", remoteCode);
1049  if (ar.versionBelow(version, 3)) {
1050  assert(Archive::IS_LOADER);
1051  remoteCode = Math::reverseByte(remoteCode);
1052  }
1053  ar.serialize("RemoteExecuteDelayed", remoteExecuteDelayed,
1054  "RemoteVblanksBack", remoteVblanksBack);
1055  }
1056 
1057  // Serialize filename
1058  ar.serialize("OggImage", oggImage);
1059  if constexpr (Archive::IS_LOADER) {
1060  sampleReads = 0;
1061  if (!oggImage.empty()) {
1062  setImageName(oggImage.getResolved(), getCurrentTime());
1063  } else {
1064  video.reset();
1065  }
1066  }
1067  ar.serialize("PlayerState", playerState);
1068 
1069  if (playerState != PLAYER_STOPPED) {
1070  // Serialize seek state
1071  ar.serialize("SeekState", seekState);
1072  if (seekState != SEEK_NONE) {
1073  ar.serialize("SeekNum", seekNum);
1074  }
1075  ar.serialize("seeking", seeking);
1076 
1077  // Playing state
1078  ar.serialize("WaitFrame", waitFrame);
1079 
1080  // This was not yet implemented in openmsx 0.8.1 and earlier
1081  if (ar.versionAtLeast(version, 2)) {
1082  ar.serialize("StillOnWaitFrame", stillOnWaitFrame);
1083  }
1084 
1085  ar.serialize("ACK", ack,
1086  "PlayingSpeed", playingSpeed);
1087 
1088  // Frame position
1089  ar.serialize("CurrentFrame", currentFrame);
1090  if (playerState == PLAYER_MULTISPEED) {
1091  ar.serialize("FrameStep", frameStep);
1092  }
1093 
1094  // Audio position
1095  ar.serialize("StereoMode", stereoMode,
1096  "FromSample", playingFromSample,
1097  "SampleClock", sampleClock);
1098 
1099  if constexpr (Archive::IS_LOADER) {
1100  // If the samplerate differs, adjust accordingly
1101  if (video->getSampleRate() != sampleClock.getFreq()) {
1102  uint64_t pos = playingFromSample;
1103 
1104  pos *= video->getSampleRate();
1105  pos /= sampleClock.getFreq();
1106 
1107  playingFromSample = pos;
1108  sampleClock.setFreq(video->getSampleRate());
1109  }
1110 
1111  auto sample = getCurrentSample(getCurrentTime());
1112  if (video->getFrameRate() == 60)
1113  video->seek(currentFrame * 2, sample);
1114  else
1115  video->seek(currentFrame, sample);
1116  lastPlayedSample = sample;
1117  }
1118  }
1119 
1120  if (ar.versionAtLeast(version, 4)) {
1121  ar.serialize("syncEven", syncEven,
1122  "syncOdd", syncOdd,
1123  "syncAck", syncAck);
1124  } else {
1125  Schedulable::restoreOld(ar, {&syncEven, &syncOdd, &syncAck});
1126  }
1127 
1128  if constexpr (Archive::IS_LOADER) {
1129  (void)isVideoOutputAvailable(getCurrentTime());
1130  }
1131 }
1132 
1134 
1135 } // namespace openmsx
Definition: one_of.hh:7
bool getBoolean() const noexcept
virtual void setLaserdiscPlayer(LaserdiscPlayer *laserdisc)=0
Set the Laserdisc Player; when the motor control is off, sound is read from the laserdisc.
void printWarning(std::string_view message)
Definition: CliComm.cc:10
Represents a clock with a fixed frequency.
Definition: Clock.hh:19
virtual TclObject executeCommand(zstring_view command, CliConnection *connection=nullptr)=0
Execute the given command.
void detach(VideoSystemChangeListener &listener)
Definition: Display.cc:138
void attach(VideoSystemChangeListener &listener)
Definition: Display.cc:132
bool before(EmuTime::param e) const
Checks whether this clock's last tick is or is not before the given time stamp.
Definition: DynamicClock.hh:44
unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
Definition: DynamicClock.hh:51
unsigned getFreq() const
Returns the frequency (in Hz) at which this clock ticks.
void advance(EmuTime::param e)
Advance this clock in time until the last tick which is not past the given time.
void setFreq(unsigned freq)
Change the frequency at which this clock ticks.
EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
Definition: DynamicClock.hh:37
static constexpr EmuDuration msec(unsigned x)
Definition: EmuDuration.hh:41
constexpr unsigned getTicksAt(unsigned freq) const
Definition: EmuDuration.hh:105
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
bool empty() const
Convenience method to test for empty filename.
Definition: Filename.cc:21
const std::string & getResolved() const &
Definition: Filename.hh:47
void setMuting(bool left, bool right, EmuTime::param time)
int16_t readSample(EmuTime::param time)
void extControl(bool bit, EmuTime::param time)
MSXMotherBoard & getMotherBoard()
const RawFrame * getRawFrame() const
void serialize(Archive &ar, unsigned version)
void update(bool newState)
Called by the device to indicate its loading state may have changed.
ReverseManager & getReverseManager()
CommandController & getCommandController()
CassettePortInterface & getCassettePort()
void videoIn(bool enabled)
A video frame as output by the VDP scanline conversion unit, before any postprocessing filters are ap...
Definition: RawFrame.hh:14
Contains the main loop of openMSX.
Definition: Reactor.hh:68
EventDistributor & getEventDistributor()
Definition: Reactor.hh:82
Display & getDisplay()
Definition: Reactor.hh:86
bool updateBuffer(unsigned length, float *buffer, EmuTime::param time) override
Generate sample data.
static void restoreOld(Archive &ar, std::vector< Schedulable * > schedulables)
Definition: Schedulable.hh:77
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:139
unsigned getInputRate() const
Definition: SoundDevice.hh:119
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:118
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:134
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:89
static XMLDocument & getStaticDocument()
Definition: XMLElement.hh:258
Definition: span.hh:126
constexpr index_type size() const noexcept
Definition: span.hh:296
#define unlikely(x)
Definition: likely.hh:15
constexpr unsigned reverseNBits(unsigned x, unsigned bits)
Reverse the lower N bits of a given value.
Definition: Math.hh:119
constexpr uint8_t reverseByte(uint8_t a)
Reverse the bits in a byte.
Definition: Math.hh:169
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:741
std::unique_ptr< LDRenderer > createLDRenderer(LaserdiscPlayer &ld, Display &display)
Create the Laserdisc Renderer.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr unsigned DUMMY_INPUT_RATE
SERIALIZE_ENUM(CassettePlayer::State, stateInfo)
EventType getType(const Event &event)
Definition: Event.hh:645
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:173
#define OUTER(type, member)
Definition: outer.hh:41
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:998
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:659
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591