home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / SoundAndMusic / SoundKit / SoundEditor / SoundController.m < prev    next >
Text File  |  1992-06-25  |  7KB  |  335 lines

  1. /*
  2.  * You may freely copy, distribute and reuse the code in this example.  
  3.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  4.  * its fitness for any particular use.
  5.  */
  6.  
  7.  
  8. #import "SoundController.h"
  9. #import "SaveToController.h"
  10. #import "SoundDocument.h"
  11. #import <soundkit/soundkit.h>
  12. #import <appkit/Application.h>
  13. #import <appkit/OpenPanel.h>
  14. #import <appkit/Cursor.h>
  15. #import <appkit/Button.h>
  16. #import <appkit/Panel.h>
  17. #import <appkit/publicWraps.h>    /* for NXBeep() */
  18. #import <sys/param.h>         /* for MAXPATHLEN */
  19. #import <string.h>
  20. #import <objc/NXStringTable.h>
  21.  
  22. static char pathname[1024];
  23.  
  24. static BOOL getOpenPath(char *buf, char const *theType)
  25. {
  26.     static id openPanel = nil;
  27.     char const *fileTypes[2] = {0,0};
  28.     int opr;
  29.  
  30.     if (!openPanel)
  31.         openPanel = [OpenPanel new];
  32.     if (theType && *theType)
  33.     fileTypes[0] = theType;
  34.     [NXApp setAutoupdate:NO];
  35.     
  36.     if (*buf)
  37.       opr = [openPanel runModalForDirectory:buf
  38.          file:NULL types:fileTypes];
  39.     else
  40.       opr = [openPanel runModalForDirectory:"/NextLibrary/Sounds" 
  41.      file:"Basso.snd" types:fileTypes];
  42.     if (opr) {
  43.     strcpy(buf,[openPanel filename]);
  44.     [NXApp setAutoupdate:YES];
  45.     return YES;
  46.     } else {
  47.     [NXApp setAutoupdate:YES];
  48.     return NO;
  49.     }
  50. }
  51.  
  52.  
  53. static BOOL getSavePath(char *buf, char const *defaultPath, View *accessory)
  54. {
  55.     static id    savePanel = nil;
  56.     BOOL    ok;
  57.     char    dirName[1024], fileName[256];
  58.  
  59.     if (!savePanel) {
  60.         const char *const types[2] = {"snd", NULL};
  61.         savePanel = [SavePanel new];
  62.         [savePanel setRequiredFileType:types[0]];
  63.     }
  64.     [savePanel setAccessoryView:accessory];
  65.     [NXApp setAutoupdate:NO];
  66.     if (defaultPath &&faultPath) {
  67.     char *p;
  68.     strcpy(dirName,defaultPath);
  69.     if (p = rindex(dirName,'/')) {
  70.         strcpy(fileName, p+1);
  71.         *p = '\0';
  72.     } else {
  73.         strcpy(fileName,defaultPath);
  74.         fileName[0] = '\0';
  75.     }
  76.     ok = [savePanel runModalForDirectory:dirName file:fileName];
  77.     } else 
  78.     ok = [savePanel runModal];
  79.     [NXApp setAutoupdate:YES];
  80.     if (ok) {
  81.     strcpy(buf,[savePanel filename]);
  82.     return YES;
  83.     } else 
  84.     return NO;
  85. }
  86.  
  87.  
  88. @implementation SoundController
  89.  
  90. - init
  91. {
  92.     [super init];
  93.     [controlPanel removeFromEventMask:(NX_KEYDOWNMASK|NX_KEYUPMASK)];
  94.     [controlPanel setFloatingPanel:YES];
  95.     return self;
  96. }
  97.  
  98. - appDidInit:sender
  99. {
  100.     [self newSoundDoc:self];   /* User Interface guidelines recommend opening 
  101.                 a new document at launch time.  We do this in
  102.                 appDidInit: rather than init to make sure that
  103.                 the stringTable has been unarchived */ 
  104.     return self;
  105. }
  106.  
  107. - info:sender
  108. {
  109.     if (!infoPanel)
  110.     [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  111.     [infoPanel orderFront:self];
  112.     return self;    
  113. }
  114.  
  115. - newSoundDoc:sender
  116. {
  117.     static char filenamebuf[MAXPATHLEN+1];
  118.     strcpy(filenamebuf, [[OpenPanel new] directory]);
  119.                 /* doesn't instantiate a new OpenPanel 
  120.                    if one already exists */
  121.     strcat(filenamebuf,[stringTable valueForStringKey:"/UNTITLED"]);
  122.     [self setDocument:[[SoundDocument alloc] init]];
  123.     [currentDocument setFileName:filenamebuf];
  124.     return self;
  125. }
  126.  
  127.  
  128. - setDocument:aDocument
  129. {
  130.     [self stop:nil];
  131.     currentDocument = aDocument;
  132.     [recordButton setEnabled:([currentDocument isRecordable]? YES : NO)];
  133.     [playButton setEnabled:YES];
  134.     [currentDocument setDelegate:self];
  135.     return self;
  136. }
  137.  
  138. - document
  139. {
  140.     return currentDocument;
  141. }
  142.  
  143. - openFile:(char *)fileName
  144. {
  145.     id newDocument;
  146.     newDocument = [[SoundDocument alloc] init];
  147.     [newDocument setFileName:fileName];
  148.     [newDocument load:nil];
  149.     [self setDocument:newDocument];
  150.     return self;
  151. }
  152.  
  153. - open:sender
  154. {
  155.     if (getOpenPath(pathname,"snd"))
  156.     [self openFile:pathname];
  157.     return self;
  158. }
  159.  
  160. - saveAs:sender withAccessory:accessory
  161. {
  162.     char pathname[1024];
  163.     id doc = currentDocument;
  164.     if (accessory)
  165.     [saveToController setSound:[doc sound]];
  166.     if (doc && getSavePath(pathname,[doc fileName],accessory)) {
  167.     if (accessory)
  168.         [doc saveToFormat:[saveToController soundTemplate]
  169.              fileName: pathname];se {
  170.         [doc setFileName:pathname];
  171.         [doc save:sender];
  172.     }
  173.     }
  174.     return self;
  175. }
  176.  
  177. - saveAs:sender
  178. {
  179.     return [self saveAs:sender withAccessory:nil];
  180. }
  181.  
  182. - saveTo:sender
  183. {
  184.     return [self saveAs:sender withAccessory:saveToAccessoryView];
  185. }
  186.  
  187. - save:sender
  188. {
  189.     if (currentDocument) {
  190.     if ([currentDocument fileName]
  191.             && strcmp(strrchr([currentDocument fileName],'/'),"/UNTITLED"))
  192.         [currentDocument save:sender];
  193.     else
  194.         [self saveAs:sender];
  195.     }
  196.     return self;
  197. }
  198.  
  199. - revertToSaved:sender
  200. {
  201.     if (currentDocument)
  202.     [currentDocument revertToSaved:sender];
  203.     return self;
  204. }
  205.  
  206. - play:sender
  207. {
  208.     if (![currentDocument isPlayable]) {
  209.     NXBeep();
  210.     return nil;
  211.     }
  212.     if (currentDocument) {
  213.     [playButton setEnabled:NO];
  214.     [recordButton setEnabled:NO];
  215.     [stopButton setEnabled:YES];
  216.     [pauseButton setState:0];
  217.     [currentDocument play:sender];
  218.     }
  219.     return self;
  220. }
  221.  
  222. - willPlay:sender
  223. {
  224.     [meter setSound:[sender soundBeingProcessed]];
  225.     [meter run:self];
  226.     return self;
  227. }
  228.  
  229. - didPlay:sender
  230. {
  231.     [playButton setState:0];
  232.     [playButton setEnabled:YES];
  233.     [recordButton setState:0];
  234.     [recordButton setEnabled:YES];
  235.     [pauseButton setState:0];
  236.     [meter stop:self];
  237.     return self;
  238. }
  239.  
  240. - stop:sender
  241. {
  242.     if (currentDocument)
  243.     [currentDocument stop:sender];
  244.     [playButton setState:0];
  245.     [playButton setEnabled:YES];
  246.     [recordButton setState:0];
  247.     [recordButton setEnabled:YES];
  248.     [pauseButton setState:0];
  249.     return self;
  250. }
  251.  
  252. - pause:sender
  253. {
  254.     if (!currentDocument || (![playButton state] && ![recordButton state])) {
  255.     [pauseButton setState:0];
  256.     return self;
  257.     } else if ([pauseButton state])
  258.     [currentDocument pause:self];
  259.     else
  260.     [currentDocument resume:self];
  261.     return self;
  262. }
  263.  
  264. - record:sender
  265. {
  266.     if (currentDocument) {
  267.     [recordButton setEnabled:NO];
  268.     [playButton setEnabled:NO];
  269.     [stopButton setEnabled:YES];
  270.     [currentDocument record:sender];
  271.     }
  272.     return self;
  273. }
  274.  
  275. - willRecord:sender
  276. {
  277.     [meter setSound:[sender soundBeingProcessed]];
  278.     [meter run:self];
  279.     return self;
  280. }
  281.  
  282. - didRecord:sender
  283. {
  284.     [playButton setState:0];
  285.     [playButton setEnabled:YES];
  286.     [recordButton setState:0];
  287.     [recordButton setEnabled:YES];
  288.     [pauseButton setState:0];
  289.     [meter stop:self];
  290.     return self;
  291. }
  292.  
  293. - soundDidChange:sender
  294. {
  295.     if (currentDocument)
  296.         [currentDocument t];
  297.     return self;
  298. }
  299.  
  300. - hadError:sender
  301. {
  302.     int err = [[sender soundBeingProcessed] processingError];
  303.  
  304.     if ([playButton state])
  305.     NXRunAlertPanel([stringTable valueForStringKey:"Play error"],
  306.         SNDSoundError(err),
  307.         [stringTable valueForStringKey:"OK"],
  308.         NULL,NULL);
  309.     else if ([recordButton state])
  310.     NXRunAlertPanel([stringTable valueForStringKey:"Record error"],
  311.         SNDSoundError(err),
  312.         [stringTable valueForStringKey:"OK"],
  313.         NULL,NULL);
  314.     return [self stop:self];
  315. }
  316.  
  317. - appDidHide:sender
  318. {
  319.     [self stop:nil];
  320.     return self;
  321. }
  322.  
  323. - appDidBecomeActive:sender
  324. {
  325.     [controlPanel orderFront:nil];
  326.     return self;
  327. }
  328.  
  329. - stringTable
  330. {
  331.     return stringTable;    
  332. }
  333.  
  334. @end
  335.