home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / maestro / source / dtr / record.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  8.8 KB  |  274 lines

  1. /*
  2.  * Copyright (c) 1990, 1991 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and 
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name
  8.  * Stanford may not be used in any advertising or publicity relating to
  9.  * the software without the specific, prior written permission of
  10.  * Stanford.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  15.  *
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
  19.  * ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  */
  23.  
  24. /* $Header: /Source/Media/collab/DTR/RCS/record.c,v 1.0 92/01/07 14:21:05 drapeau Exp Locker: derek $ */
  25. /* $Log:    record.c,v $
  26.  * Revision 1.0  92/01/07  14:21:05  drapeau
  27.  * Modified StopRecord() to clean up keeping track of currentSoundFile
  28.  * variable and updating the appropriate headers.
  29.  * Modified RemoveSoundFileInTmp(), using the unlink() system call
  30.  * instead of the more awkward use of the system() function.
  31.  * Also, Made a number of cosmetic changes to make code easier to read and
  32.  * to conform to programming specifications.
  33.  * 
  34.  * Revision 0.19  91/09/18  22:47:36  derek
  35.  * The following things are done:
  36.  * 1.    The Makefile is changed corresponding to the changes in collab/.
  37.  * 2.    Copyright messages included.
  38.  * 3.    Some minor bugs fixed.
  39.  * 
  40.  * Revision 0.18  91/08/27  18:06:11  derek
  41.  * The SizeToFit bug is fixed.
  42.  * 
  43.  * Revision 0.17  91/08/13  20:38:38  derek
  44.  * The buttons (play, pause, record) will now flash after they are pressed.
  45.  * This only applies to times when audio files (not edit-lists) are 
  46.  * played.
  47.  * 
  48.  * Revision 0.16  91/08/07  16:24:42  derek
  49.  * The Edit list part of DTR is done.  OpenPanel is also incorporated.
  50.  * 
  51.  * Revision 0.15  91/07/30  11:45:46  derek
  52.  * I have fixed the tmp file conflict bug.
  53.  * 
  54.  * Revision 0.14  91/07/24  12:52:11  derek
  55.  * Disk editing is done.  Now the application can record sound infinitely,
  56.  * as long as there is disk space available.  Command line args are also
  57.  * supported.
  58.  * 
  59.  * Revision 0.13  91/07/23  21:25:25  derek
  60.  * This version is not ready for release.  Disk space editing is half-done:
  61.  * the application can play an infinite sound and the canvases can handle
  62.  * infinite sound files.  The app is pretty bug free too, I think.  The
  63.  * weakness is that it cannot record sond infinitely.
  64.  * 
  65.  * Revision 0.12  91/06/26  15:55:51  derek
  66.  * I have reformatted the code to conform coding specs.
  67.  * 
  68.  * Revision 0.11  91/06/05  15:00:07  derek
  69.  * checking in after porting the code to collab
  70.  * 
  71.  * Revision 0.10  1991/04/25  01:51:52  derek
  72.  * This version is checked in on 4/24/91
  73.  * */
  74. static char rcsid[] = "$Header: /Source/Media/collab/DTR/RCS/record.c,v 1.0 92/01/07 14:21:05 drapeau Exp Locker: derek $";
  75.  
  76. #include "dtr.h"
  77. #include "dtr_ui.h"
  78.  
  79. void
  80.   InitRecord()
  81. {
  82.   tmpnam(SoundFileInTmp);
  83. }
  84.  
  85.  
  86. void
  87.   StartRecord()
  88. {
  89.   Audio_info tmpinfo;
  90.   unsigned   Ilen;
  91.   char       *info = NULL;
  92.   extern     dtr_editListPanelPopUp_objects *dtr_editListPanelPopUp;
  93.   
  94.   EVENT("Start_Record");
  95.   
  96.   (void) audio_pause_record(Audio_fd);                    /*  Pause the input and flush the read queue.       */
  97.   (void) audio_flush_record(Audio_fd);
  98.   
  99.   Buffer.hdr = Device_rhdr;
  100.   Buffer.display.position = 0;
  101.   Buffer.display.last = -1;
  102.   Buffer.draining = FALSE;
  103.   Buffer.hdr.data_size = 0;
  104. /**************
  105.   AllocBuffer(audio_secs_to_bytes(&Buffer.hdr, (double)MAX_RECORD_TIME));
  106. **************/
  107.   
  108.   Buffer.display.start = -1;                        /*  Initializing start and end points for display.. */
  109.   Buffer.play.start = 0;                        /*  ...and play.                                    */
  110.   Buffer.display.end = -1;
  111.   Buffer.play.end = 0;
  112.   
  113.   ClearVUMeterCanvas();
  114.   
  115.   ActiveFlag |= RECORD;                            /*  Restart input and reset counters.  SIGPOLL...   */
  116.   AUDIO_INITINFO(&tmpinfo);                        /*  ...will start reading.                          */
  117.   tmpinfo.record.pause = 0;
  118.   tmpinfo.record.samples = 0;
  119.   tmpinfo.record.error = 0;
  120.   (void) audio_setinfo(Audio_fd, &tmpinfo);
  121.   ResetVUMeter();                            /*  Reset VU Meter.                                 */
  122. /*
  123.   SetWaveEditModeOff();                            
  124. */
  125.   FileReady = TRUE;
  126.   SoundBufferReady = FALSE;
  127.   BufferSaved = FALSE;
  128.   tmpSoundFd = open(SoundFileInTmp,                    /*  Try to open a new file for saving the sound.    */
  129.             O_WRONLY | O_CREAT | O_TRUNC,
  130.             0666);
  131.   tmpSoundHdr = Buffer.hdr;
  132.   tmpSoundHdr.data_size = 0;
  133.   if ((tmpSoundFd < 0) ||
  134.       (audio_write_filehdr(tmpSoundFd, &tmpSoundHdr, info, Ilen)
  135.        != AUDIO_SUCCESS))
  136.   {
  137.     fprintf(stderr, "Error:  Recording error\n");
  138.     exit(0);
  139.   }
  140.   
  141.   xv_set(dtr_editListPanelPopUp->addButton,                
  142.      PANEL_INACTIVE, TRUE, NULL);
  143.   xv_set(dtr_editListPanelPopUp->modifyButton,
  144.      PANEL_INACTIVE, TRUE, NULL);
  145.   xv_set(dtr_editListPanelPopUp->deleteButton,                
  146.      PANEL_INACTIVE, TRUE, NULL);
  147.   xv_set(dtr_editListPanelPopUp->deleteAllButton,
  148.      PANEL_INACTIVE, TRUE, NULL);
  149.   SetButtonGlowTimer(GLOW_INTERVAL);
  150. }                                    /* end function StartRecord */
  151.  
  152.  
  153. /*
  154.  *  This is the routine that is called from the main loop that reads
  155.  *  sound from the device.  Now, when recording, the VUMeter will
  156.  *  be updated by this function instead of by having a timer calling
  157.  *  UpdateVUMeter.
  158.  */
  159. void
  160.   RecordService()
  161. {
  162.   int    readsize;
  163.   int    rtn;
  164.   static  unsigned char  data[512];
  165.   int   level;
  166.   int   i;
  167.   
  168.   EVENT("Record_Service");
  169.   
  170.   if (ioctl(Audio_fd, FIONREAD, &readsize) < 0)
  171.   {
  172.     PERROR("FIONREAD");
  173.     return;
  174.   }
  175.   
  176.   if ((readsize == 0) || ((readsize < SCOPE_WIDTH) &&            /*  Dont bother if there is nothing to read, or...  */
  177.               !Buffer.draining))                /*  ...if not much (and we're not draining input.). */
  178.     return;                                /*  Also, Buffer.hdr.alloc_size = MAX_RECORD_SIZE.. */
  179.                                     /*  ...(due to StartRecord().                       */
  180.   
  181.   for (;;)
  182.   {
  183.     rtn = read(Audio_fd, (char *)data, 512);
  184.     for (level = 0, i = 0; i < 512; i+=2)                /*  Start updating VUMeter.                         */
  185.     {
  186.       if (abs((int)audio_u2c(data[i])) > level)
  187.     level = abs((int)audio_u2c(data[i]));
  188.     }
  189.     CalculateLevelAndDisplayVUMeter(level);
  190.     if (rtn < 0)
  191.     {
  192.       if (errno != EWOULDBLOCK)
  193.     PERROR("read");
  194.       break;
  195.     }
  196.     else
  197.     {
  198.       write(tmpSoundFd, (char *)data, rtn);
  199.     }
  200.     Buffer.hdr.data_size += rtn;
  201.     if (ioctl(Audio_fd, FIONREAD, &readsize) < 0)
  202.       return;
  203.   }
  204. }                                    /* end function RecordService */
  205.  
  206.  
  207. void
  208.   StopRecord()
  209. {
  210.   unsigned    u;
  211.   extern        dtr_mainWindow_objects               *dtr_mainWindow;
  212.   extern        dtr_editListPanelPopUp_objects          *dtr_editListPanelPopUp;
  213.   
  214.   EVENT("Stop_Record");
  215.   ActiveFlag &= ~RECORD;
  216.   if (WaitFlag & RECORD)                        /*  If waiting for device open, quit waiting now.   */
  217.   {
  218.     WaitFlag &= ~RECORD;
  219.     return;
  220.   }
  221.   (void) audio_pause_record(Audio_fd);                    /*  Pause the device so that we can drain the input */
  222.   Buffer.draining = TRUE;                        /*  Read residual input, if any.                    */
  223.   RecordService();
  224.   Buffer.draining = FALSE;
  225.   (void) audio_get_record_error(Audio_fd, &u);
  226.   Audio_state.record.error = u;
  227.   if (!ActiveFlag)
  228.   {
  229.     AudioFlushClose();
  230.   }
  231.   audio_rewrite_filesize(tmpSoundFd, Buffer.hdr.data_size);
  232.   close(tmpSoundFd);
  233.   ClearVUMeterCanvas();
  234.   if (Buffer.play.end == 0)                        /*  Set new endpoints (if not selected during...    */
  235.   {                                    /*  ...record) & redisplay.                         */
  236.     Buffer.display.end = Buffer.play.end = 
  237.       Buffer.hdr.data_size - 1;
  238.   }
  239.   Buffer.display.start = Buffer.play.start;
  240.   FileUpdate();
  241.   UpdateMessageDisplay();
  242.   FrameStartingSecond = 0.0;
  243.   sprintf(currentSoundFile, "%s", SoundFileInTmp);
  244.   UpdateHeader(TRUE);
  245.   SoundBufferReady = ReadSoundFile(TRUE);
  246.   FileReady = FALSE;
  247.   SameSoundFile = TRUE;
  248.   ResetWaveCanvas();
  249.   ResetGlobalWaveCanvas();
  250.   RepaintWaveCanvas();
  251.   RepaintGlobalWaveCanvas();
  252.   CancelButtonGlowTimer();
  253.   xv_set(dtr_editListPanelPopUp->addButton,
  254.      PANEL_INACTIVE, FALSE, NULL);
  255.   xv_set(dtr_editListPanelPopUp->modifyButton,
  256.      PANEL_INACTIVE, FALSE, NULL);
  257.   xv_set(dtr_editListPanelPopUp->deleteButton,                
  258.      PANEL_INACTIVE, FALSE, NULL);
  259.   xv_set(dtr_editListPanelPopUp->deleteAllButton,
  260.      PANEL_INACTIVE, FALSE, NULL);
  261.   xv_set(dtr_editListPanelPopUp->editListScrollList,
  262.      PANEL_INACTIVE, FALSE, NULL);
  263.  
  264.   SetWaveEditModeOn();
  265.  
  266. }                                    /* end function StopRecord */
  267.  
  268.  
  269. void
  270.   RemoveSoundFileInTmp()
  271. {
  272.   unlink(SoundFileInTmp);
  273. }
  274.