home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscSoundPalette / MiscSoundUtil.subproj / MiscTapper.m < prev    next >
Encoding:
Text File  |  1995-03-25  |  3.1 KB  |  136 lines

  1. /*
  2.  
  3. MiscTapper
  4. Version 1.2
  5. Copyright (c) 1995 by Sean Luke
  6. Donated to the MiscKit
  7.  
  8. Permission to use, copy, modify, and distribute this material 
  9. for any purpose and without fee, under the restrictions as noted 
  10. in the MiscKit copyright notice, is hereby granted, provided that
  11. the MiscKit copyright notice and this permission notice 
  12. appear in all source copies, and that the author's name shall not
  13. be used in advertising or publicity pertaining to this 
  14. material without the specific, prior written permission 
  15. of the author.  SEAN O. LUKE  MAKES NO REPRESENTATIONS ABOUT THE
  16. ACCURACY OR SUITABILITY OF THIS MATERIAL FOR ANY PURPOSE.  
  17. IT IS PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  18.  
  19. */
  20.  
  21. #import "MiscTapper.h"
  22. #import <soundkit/soundkit.h>
  23. #import <stdio.h>
  24. #import <objc/objc-runtime.h>
  25.  
  26.  
  27.  
  28. #define            TAPPER_BUFFER_SIZE        65536
  29.     // buffer size to deliver to stream reading in from microphone,
  30.     // 64K of sound, or enough for about 8 seconds of CODEC.
  31. #define         TAPPER_TAG                 679201341        
  32.     // if this object pauses, try changing this arbitrary tag
  33. #define            TAPPER_REFRESH            .1
  34.     // fraction of a second tapper checks for finished data.
  35.  
  36.  
  37. // timed entry header
  38.  
  39. DPSTimedEntryProc tapper_timer
  40.     (DPSTimedEntry teNum,double now, void* the_tapper);
  41.  
  42. @implementation MiscTapper
  43.     
  44. - _update                    // Private
  45.     {
  46.     if (tapper_load) 
  47.         {
  48.         tapper_load=0;
  49.         [tapper_stream recordSize:TAPPER_BUFFER_SIZE tag:TAPPER_TAG];
  50.         }
  51.     return self;    
  52.     }    
  53.     
  54. - init
  55.     {
  56.     id returnval;
  57.     returnval=[super init];
  58.     tapper_device=[[NXSoundIn alloc] init];
  59.     tapper_stream=[[NXRecordStream alloc] initOnDevice:tapper_device];
  60.     [tapper_stream setDelegate:self];
  61.     return returnval;
  62.     }
  63.  
  64. - awake
  65.     {
  66.     id returnval;
  67.     returnval=[super awake];
  68.     tapper_device=[[NXSoundIn alloc] init];
  69.     tapper_stream=[[NXRecordStream alloc] initOnDevice:tapper_device];
  70.     [tapper_stream setDelegate:self];
  71.     return returnval;
  72.     }
  73.  
  74. - stop:sender
  75.     {
  76.     running=0;
  77.     if (teNum) DPSRemoveTimedEntry(teNum);
  78.     teNum=0;
  79.     [tapper_stream deactivate];
  80.     return self;
  81.     }
  82.     
  83.     
  84. - run:sender
  85.     {
  86.     if (running) return self;            // Already running.  Why re-run?
  87.     tapper_load=1;                        // This ensures that the tapper is
  88.                                         // ready to accept stream info.
  89.                                         // When deactivated a stream may not
  90.                                         // have finished recording, so
  91.                                         // tapper_load could possibly be 0!
  92.     running=1;
  93.     [tapper_stream activate];
  94.     if (teNum) DPSRemoveTimedEntry(teNum);
  95.     teNum=DPSAddTimedEntry(TAPPER_REFRESH, 
  96.         (DPSTimedEntryProc) tapper_timer,
  97.         (void*) self, (int) NX_RUNMODALTHRESHOLD);
  98.     return self;
  99.     }
  100.  
  101.  
  102. - free
  103.     {
  104.     running=0;                        // just in case
  105.     [tapper_stream deactivate];
  106.     if (teNum) DPSRemoveTimedEntry(teNum);
  107.     teNum=0;
  108.     [tapper_stream free];
  109.     [tapper_device free];
  110.     return [super free];
  111.     }
  112.  
  113. - soundStream:sender didRecordData:(void*) data 
  114.     size:(unsigned int) numBytes forBuffer:(int)tag
  115.     {
  116.     vm_deallocate(task_self(),(vm_address_t) data,(vm_size_t) numBytes);
  117.     tapper_load=1;
  118.     return self;
  119.     }
  120.     
  121.     
  122. @end
  123.  
  124.  
  125. // timed entry procedure for periodically updating
  126.  
  127. DPSTimedEntryProc tapper_timer
  128.     (DPSTimedEntry teNum,double now, void* the_tapper)
  129.     {
  130.     MiscTapper* temp_tapper=(MiscTapper*) the_tapper;
  131.     [temp_tapper _update];
  132.     return (void*) NULL;
  133.     }
  134.  
  135.  
  136.