/*****************************************************************************

    CaptureMovie -- Create a Quicktime Movie from Sequence Grabber Audio Data

    When you run this command line program, it creates an audio movie from
    microphone input during the run and places it in the same directory as
    this file under the name "my.mov".    
      
    Environment:
    
      OS X version 10.1.5

    Creation:
   
      open Project Builder 
      click file menu -- New Project
      click "Standard Tool" at the end of the list
      name=CaptureMovie, directory -- pick one
 
      ^click on CaptureMovie -- new Group 
        give it the name "Frameworks"
      ^click on Frameworks -- choose add frameworks
        choose QuickTime.framework -- reference style = default
      ^click on Frameworks -- choose add frameworks
        choose Carbon.framework -- reference style = default

      ^click on main.c under the "Source" group
        choose "Show Info"
        change Reference Style from "Group Relative" to "Project Relative"

 *****************************************************************************/


/*  Include the Quicktime header.
    =============================  */
    
    #include <QuickTime/QuickTime.h>


  
/*  M A I N
 *
 */
int main (int argc, char *argv[])
    {
    OSErr osErr;
    ComponentResult componentResult;
    FSSpec fsSpec;
    SGChannel soundChannel;
    SeqGrabComponent seqGrabComponent;

    
    EnterMovies ();                    /* Initialize Quicktime               */


/*  Open and initialize the default sequence grabber.
    =================================================  */
    
    seqGrabComponent = OpenDefaultComponent (SeqGrabComponentType, 0);
    osErr = GetMoviesError ();
    if (!seqGrabComponent || osErr)
      {
      printf ("OpenDefaultComponent failed %d\n", osErr); 
      goto bail; 
      }
    printf ("OpenDefaultComponent succeeded\n"); 
      
    osErr = SGInitialize (seqGrabComponent);
    if (osErr)
      {
      printf ("SGInitialize failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGInitialize succeeded\n"); 


/*  Set the file to receive the movie data.
    =======================================  */
    
    osErr = NativePathNameToFSSpec ("../my.mov", &fsSpec, 0);
    if (osErr && (osErr != fnfErr))    /* File-not-found error is ok         */
      { 
      printf ("NativePathNameToFSSpec failed %d\n", osErr); 
      goto bail; 
      }
    printf ("NativePathNameToFSSpec succeeded\n"); 

    componentResult = SGSetDataOutput
      (
      seqGrabComponent,                /* sequence grabber component.        */
      &fsSpec,                         /* the movie file destination.        */
      seqGrabToDisk                    /* flags - store to disk.             */
      );
    if (componentResult != noErr) 
      { 
      printf ("SGSetDataOutput failed %ld\n", componentResult); 
      goto bail; 
      }
    printf ("SGSetDataOutput succeeded\n"); 


/*  Create the sound channel.
    =========================  */

    osErr = SGNewChannel
      (
      seqGrabComponent,                /* sequence grabber component.        */
      SoundMediaType,                  /* media type                         */
      &soundChannel                    /* receives channel handle.           */
      );
    if (osErr)
      {
      printf ("SGNewChannel[sound] failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGNewChannel[sound] succeeded\n"); 
    
    osErr = SGSetChannelUsage
      (
      soundChannel,                    /* the channel to modify.             */
      seqGrabPreview | seqGrabRecord   /* channel properties.                */
      );
    if (osErr)
      {
      printf ("SGSetChannelUsage[sound] failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGSetChannelUsage[sound] succeeded\n"); 
          
          
/*  Normally you would set channel properties (compression, source, ...) here
    but that requires access to the screen.
    =========================================================================  */
    
//    SGSettingsDialog (seqGrabComponent, soundChannel, 0, nil, DoTheRightThing, 
//      nil, 0);
    
    
/*  Set a maximum recording time and perform the record for that amount of time.
    ============================================================================  */
    
    osErr = SGSetMaximumRecordTime (seqGrabComponent, 3 * 60);
    if (osErr)
      {
      printf ("SGSetMaximumRecordTime failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGSetMaximumRecordingTime succeeded\n"); 
    
    osErr = SGStartRecord (seqGrabComponent);
    if (osErr)
      {
      printf ("SGStartRecord failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGStartRecord succeeded\n"); 


/*  Each cycle of this loop calls the sequence grabber's idle function
    once so it can grab, compress, and write more data.
    ==================================================================  */
    
    while (!osErr)
      {
      osErr = SGIdle (seqGrabComponent);
      }
    if (osErr == grabTimeComplete) osErr = noErr;
    if (osErr)
      {
      printf ("\nSGIdle failed %d\n", osErr); 
      }
    printf ("\nSGIdle timed out\n"); 


/*  Dismantle the sequence grabber.
    ===============================  */
    
    osErr = SGStop (seqGrabComponent);
    if (osErr)
      {
      printf ("SGStop failed %d\n", osErr); 
      goto bail; 
      }
    printf ("SGStop succeeded\n"); 
    
    
  bail:    

    if (soundChannel)     SGDisposeChannel (seqGrabComponent, soundChannel);
    if (seqGrabComponent) CloseComponent (seqGrabComponent);
    
    ExitMovies ();                     /* Finalize Quicktime                 */
    
    return (0);
    }


/*============================(  End of Source  )============================*/

