home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / MMSTEREO / MLTWAVE.CPP < prev    next >
Text File  |  1995-05-01  |  10KB  |  239 lines

  1. #include "mltwave.hpp"
  2.  
  3. /*------------------------------------------------------------------------------
  4. | WAVE::WAVE                                                                   |
  5. |                                                                              |
  6. |                                                                              |
  7. ------------------------------------------------------------------------------*/
  8. WAVE::WAVE( IMMWaveAudio*     aWavePlayer,
  9.             unsigned long     windowid,
  10.             IWindow*          parent,
  11.             IWindow*          owner)
  12.    : IMMPlayerPanel(windowid,parent,owner,IMMDevice::waveAudio),
  13.      readout     (WAVEREADOUTID, this,this),
  14.      name        (WAVENAMEID, this, this),
  15.      loadit      (LOADID,this,this,IRectangle(),
  16.                   IWindow::visible | IAnimatedButton::animateWhenLatched),
  17.      saveButton  (SAVEID,this,this),
  18.      lineIn      (WAVELINEINID,this,this),
  19.      microphone  (WAVEMICROPHONEID,this,this),
  20.      pWavePlayer(aWavePlayer),
  21.      observer(0),
  22.      handler(0)
  23. {
  24.    setPlayableDevice(pWavePlayer);
  25.  
  26.    rec = new IAnimatedButton(RECID,this,this,IRectangle(),
  27.                              ICustomButton::autoLatch |
  28.                              ICustomButton::latchable |
  29.                              IWindow::visible | IAnimatedButton::animateWhenLatched);
  30.    saveButton.setText("Save");
  31.    lineIn.enableAutoSelect();
  32.    lineIn.setText("LineIn");
  33.    microphone.enableAutoSelect();
  34.    microphone.setText("Microphone");
  35.    microphone.select();
  36.  
  37.    //Add the additional buttons to the player panel.
  38.    addToCell(rec      ,     7, 1, 1, 2);
  39.    addToCell(&lineIn   ,    8, 1, 1, 1);
  40.    addToCell(µphone,   8, 2, 1, 1);
  41.    addToCell(&saveButton,   9, 1, 1, 1);
  42.    addToCell(&readout ,    11, 1, 1, 2);
  43.    addToCell(&loadit  ,    13, 1, 1, 2);
  44.    addToCell(&name    ,     2, 3, 5, 1);
  45.    rec->disable();
  46.    playButton()->disable();
  47.    saveButton.disable();
  48.    rewindButton()->disable();
  49.    fastForwardButton()->disable();
  50.  
  51.    //Put the bitmaps on the buttons.
  52.    rec->setBitmaps(IAnimatedButton::record);
  53.    loadit.setBitmaps(IAnimatedButton::eject);
  54.  
  55.    //Put text on the buttons.
  56.    rec->setText("Record");
  57.    loadit.setText("Load");
  58.  
  59.    //Set up the title
  60.    name.setText("Class Library Wave Player");
  61.    name.setForegroundColor(IColor(IColor::red));
  62.  
  63.    //Set up the display
  64.    IFont("LED",10).setWindowFont(&readout);
  65.    readout.setForegroundColor(IColor(0,160,0));
  66.    readout.setBackgroundColor(IColor(IColor::black));
  67.    readout.setText("HR:MIN:SEC 00:00:00");
  68.    readout.setLimit(22);
  69.  
  70.    if (pWavePlayer)
  71.    {
  72.       handler  = new WaveHandler();
  73.       handler->handleEventsFor(this);
  74.       observer = new WaveObserver(*this);
  75.       observer->handleNotificationsFor(*pWavePlayer);
  76.    }
  77. }
  78.  
  79.  
  80. /*------------------------------------------------------------------------------
  81. | WAVE::~WAVE                                                                      |
  82. |                                                                              |
  83. |                                                                              |
  84. ------------------------------------------------------------------------------*/
  85. WAVE::~WAVE()
  86. {
  87.   if (observer)
  88.      observer->stopHandlingNotificationsFor(*pWavePlayer);
  89.   if (handler)
  90.      handler->stopHandlingEventsFor(this);
  91. }
  92.  
  93. /*------------------------------------------------------------------------------
  94. | WAVE::recordButton                                                           |
  95. |                                                                              |
  96. |                                                                              |
  97. ------------------------------------------------------------------------------*/
  98. IAnimatedButton* WAVE::recordButton () const
  99. {
  100.   return rec;
  101. }
  102.  
  103. /*------------------------------------------------------------------------------
  104. | WAVE::wavePlayer                                                             |
  105. |                                                                              |
  106. |                                                                              |
  107. ------------------------------------------------------------------------------*/
  108. IMMWaveAudio* WAVE::wavePlayer () const
  109. {
  110.   return pWavePlayer;
  111. }
  112.  
  113.  
  114. /*------------------------------------------------------------------------------
  115. | WaveHandler::WaveHandler                                                     |
  116. |                                                                              |
  117. |                                                                              |
  118. ------------------------------------------------------------------------------*/
  119. WaveHandler::WaveHandler()
  120.           : WaveHandler::Inherited()
  121. {}
  122.  
  123. /*------------------------------------------------------------------------------
  124. | WaveHandler::command                                                         |
  125. |                                                                              |
  126. ------------------------------------------------------------------------------*/
  127. Boolean WaveHandler::command(ICommandEvent& event)
  128. {
  129.   Boolean handled = false;
  130.   WAVE* panel = 0;
  131.  
  132.   switch (event.commandId())
  133.   {
  134.     case LOADID:
  135.          {
  136.          panel = (WAVE*) (event.window());
  137.          IFileDialog::Settings fdSettings;
  138.          fdSettings.setTitle("Load a new audio file");
  139.          fdSettings.setFileName("*.wav");
  140.          IFileDialog fd(panel->desktopWindow(),panel,fdSettings);
  141.          if (fd.pressedOK())
  142.          {
  143.             panel->wavePlayer()->loadOnThread(fd.fileName());
  144.             stop(*panel);
  145.             panel->wavePlayer()->startPositionTracking(IMMTime(3000));
  146.             panel->recordButton()->enable();
  147.             panel->playButton()->enable();
  148.             panel->rewindButton()->enable();
  149.             panel->fastForwardButton()->enable();
  150.             panel->saveButton.enable();
  151.          }
  152.          }
  153.          handled=true;
  154.          break;
  155.     case SAVEID:
  156.          panel = (WAVE*) (event.window());
  157.          stop(*panel);
  158.          panel->wavePlayer()->save();
  159.          handled=true;
  160.          break;
  161.     case RECID:
  162.          panel = (WAVE*) (event.window());
  163.          panel->stopButton()->enable();
  164.          panel->pauseButton()->disable();
  165.          panel->playButton()->disable();
  166.          panel->rewindButton()->disable();
  167.          panel->fastForwardButton()->disable();
  168.          if (panel->lineIn.isSelected())
  169.             panel->wavePlayer()->enableConnector(IMMDevice::lineIn);
  170.          else
  171.             panel->wavePlayer()->enableConnector(IMMDevice::microphones);
  172.          panel->wavePlayer()->acquire(IMMDevice::isolatedExclusive);
  173.          panel->wavePlayer()->record();
  174.          handled = true;
  175.          break;
  176.     default:
  177.          handled = Inherited::command(event);
  178.          break;
  179.   } /* endswitch */
  180.   return handled;
  181. }
  182.  
  183. /*------------------------------------------------------------------------------
  184. | WaveHandler::stop                                                            |
  185. |                                                                              |
  186. ------------------------------------------------------------------------------*/
  187. Boolean WaveHandler::stop(const IMMPlayerPanel& panel)
  188. {
  189.   panel.playButton()->enable();
  190.   panel.rewindButton()->enable();
  191.   panel.fastForwardButton()->enable();
  192.   Boolean retval = Inherited::stop(panel);
  193.   panel.playableDevice()->release();
  194.   return retval;
  195. }
  196.  
  197. /*------------------------------------------------------------------------------
  198. | WaveObserver::WaveObserver                                                   |
  199. |                                                                              |
  200. |                                                                              |
  201. ------------------------------------------------------------------------------*/
  202. WaveObserver::WaveObserver(WAVE& wavePanel)
  203.           : WaveObserver::Inherited(),
  204.           panel(wavePanel)
  205. {}
  206.  
  207. /*------------------------------------------------------------------------------
  208. | WaveObserver::dispatchNotificationEvent                                      |
  209. |                                                                              |
  210. |                                                                              |
  211. ------------------------------------------------------------------------------*/
  212. WaveObserver& WaveObserver::dispatchNotificationEvent(const INotificationEvent& event)
  213. {
  214.    if (event.notificationId() == IMMDevice::positionChangeId)
  215.    {
  216.       IMMPositionChangeEvent* positionEvent = (IMMPositionChangeEvent*)(event.eventData().asUnsignedLong());
  217.  
  218.       IMMTime time(positionEvent->position());
  219.       panel.readout.setText(IString("HR:MIN:SEC ") +
  220.                       IString(time.hours()).rightJustify(2,'0') +
  221.                       ":" +
  222.                       IString(time.minutes()).rightJustify(2,'0') +
  223.                       ":" +
  224.                       IString(time.seconds()).rightJustify(2,'0'));
  225.       if (!panel.playButton()->isLatched())
  226.          if (panel.wavePlayer()->mode()==IMMDevice::playing)
  227.             panel.playButton()->latch();
  228.    } /* endif */
  229.    if (event.notificationId() == IMMDevice::commandNotifyId)
  230.    {
  231.       IMMNotifyEvent* notifyEvent = (IMMNotifyEvent*)(event.eventData().asUnsignedLong());
  232.       if (notifyEvent->command() == IMMNotifyEvent::play)
  233.          panel.playButton()->unlatch();
  234.       if (notifyEvent->command() == IMMNotifyEvent::record)
  235.          panel.recordButton()->unlatch();
  236.    } /* endif */
  237.    return *this;
  238. }
  239.