home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / SimpleSoundMixingExample / Source / MixController.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.6 KB  |  104 lines

  1.  
  2. #import "MixController.h"
  3.  
  4. @implementation MixController
  5.  
  6. - appDidInit:sender
  7. {
  8.   sndOut = [[NXSoundOut alloc] init];
  9.   if (!sndOut)
  10.   {  fprintf(stderr, "Unable to get sound out device.  Bye!\n");
  11.      return nil;
  12.   }
  13.   [sndOut setReserved:YES];
  14.   snd1Stream = [[NXPlayStream alloc] initOnDevice:sndOut];
  15.   snd2Stream = [[NXPlayStream alloc] initOnDevice:sndOut];
  16.   tag = 1;
  17.   return self;
  18. }
  19.  
  20. - play:sender
  21. {
  22.   void          *snd1Data, *snd2Data;
  23.   unsigned int  snd1Bytes, snd2Bytes;
  24.   int           snd1Tag, snd2Tag, ret1, ret2;
  25.   unsigned int  snd1ChannelCount, snd2ChannelCount;
  26.   double        snd1Rate, snd2Rate;
  27.  
  28.  
  29.   [self setSound1Name:snd1Text];
  30.   [self setSound2Name:snd2Text];
  31.  
  32.   if ((!snd1) || (!snd2))
  33.   { return self;
  34.   }
  35.  
  36.   // snd1
  37.   snd1Data = (void *)[snd1 data];
  38.   snd1Bytes = (unsigned int)[snd1 dataSize];
  39.   snd1Tag = tag++;
  40.   snd1ChannelCount = [snd1 channelCount];
  41.   if (([snd1 samplingRate] != SND_RATE_LOW) && ([snd1 samplingRate] != SND_RATE_HIGH))
  42.   {  [snd1 convertToFormat:SND_FORMAT_LINEAR_16 
  43.            samplingRate:SND_RATE_LOW
  44.            channelCount:snd1ChannelCount
  45.      ];
  46.   }
  47.   snd1Rate = [snd1 samplingRate];
  48.   if ([snd1 dataFormat] != SND_FORMAT_LINEAR_16)
  49.   {  [snd1 convertToFormat:SND_FORMAT_LINEAR_16];
  50.   }
  51.   // snd2
  52.   snd2Data = (void *)[snd2 data];
  53.   snd2Bytes = (unsigned int)[snd2 dataSize];
  54.   snd2Tag = tag;
  55.   snd2ChannelCount = [snd2 channelCount];
  56.   if (([snd2 samplingRate] != SND_RATE_LOW) && ([snd2 samplingRate] != SND_RATE_HIGH))
  57.   {  [snd2 convertToFormat:SND_FORMAT_LINEAR_16 
  58.            samplingRate:SND_RATE_LOW
  59.            channelCount:snd2ChannelCount
  60.      ];
  61.   }
  62.   snd2Rate = [snd2 samplingRate];
  63.   if ([snd2 dataFormat] != SND_FORMAT_LINEAR_16)
  64.   {  [snd2 convertToFormat:SND_FORMAT_LINEAR_16];
  65.   }
  66.  
  67.   // activate!
  68.   [snd1Stream activate];
  69.   [snd2Stream activate];
  70.  
  71.   ret1 = [snd1Stream playBuffer:snd1Data size:snd1Bytes tag:snd1Tag channelCount:snd1ChannelCount samplingRate:(float)snd1Rate];
  72.   ret2 = [snd2Stream playBuffer:snd2Data size:snd2Bytes tag:snd2Tag channelCount:snd2ChannelCount samplingRate:(float)snd2Rate];
  73.  
  74.   fprintf(stderr, "ret1 == %d ret2 == %d\n", ret1, ret2);
  75.  
  76.   // I really should deactivate these precious resources, but you
  77.   // shouldn't deactivate until you're sure the whole sound has been
  78.   // played.  Since this is supposed to be a wicked simple example,
  79.   // I'll leave that for later. 
  80.  
  81.   return self;
  82. }
  83.  
  84. - setSound1Name:sender
  85. {
  86.   if (snd1)
  87.   {  [snd1 free];
  88.   }
  89.   snd1 = [[Sound alloc] initFromSoundfile:[sender stringValue]];
  90.   return self;
  91. }
  92.  
  93. - setSound2Name:sender
  94. {
  95.   if (snd2)
  96.   {  [snd2 free];
  97.   }
  98.   snd2 = [[Sound alloc] initFromSoundfile:[sender stringValue]];
  99.   return self;
  100. }
  101.  
  102.  
  103. @end
  104.