home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / SoundAndMusic / SoundKit / SoundEditor / SaveToController.m < prev    next >
Text File  |  1992-07-07  |  4KB  |  159 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. #import "SaveToController.h"
  8. #import <appkit/appkit.h>
  9. #import <soundkit/soundkit.h>
  10.  
  11. @implementation SaveToController
  12.  
  13. #define LINEAR_ROW 0
  14. #define MULAW_ROW 1
  15. #define COMPRESSED_ROW 2
  16.  
  17. static int _dataFormatToRow(int dataFormat)
  18. {
  19.     int format_row;
  20.  
  21.     switch (dataFormat) {
  22.    e SND_FORMAT_LINEAR_16:
  23.     case SND_FORMAT_EMPHASIZED:
  24.     format_row = LINEAR_ROW;
  25.     break;
  26.     case SND_FORMAT_MULAW_8:
  27.     format_row = MULAW_ROW;
  28.     break;
  29.     case SND_FORMAT_COMPRESSED:
  30.     case SND_FORMAT_COMPRESSED_EMPHASIZED:
  31.     format_row = COMPRESSED_ROW;
  32.     break;
  33.     default:
  34.     return LINEAR_ROW; /* Try to convert unsupported formats to linear */
  35.     }
  36.     return format_row;
  37. }
  38.  
  39. static int _rowToDataFormat(int row)
  40. {
  41.     int dataFormat;
  42.     switch (row) {
  43.     case LINEAR_ROW:
  44.     dataFormat = SND_FORMAT_LINEAR_16;
  45.     break;
  46.     case MULAW_ROW:
  47.     dataFormat = SND_FORMAT_MULAW_8;
  48.     break;
  49.     case COMPRESSED_ROW:
  50.     dataFormat = SND_FORMAT_COMPRESSED;
  51.     break;
  52.     default:
  53.     return -1;
  54.     }
  55.     return dataFormat;
  56. }
  57.  
  58. /*
  59.  * Note that the Sound object's convertToFormat: method will convert
  60.  * to or from any sampling rate.  However, to minimize confusion for
  61.  * casual users, we restrict the choices to the three which arise
  62.  * naturally on the NeXT Computer.
  63.  */
  64. static const double supportedRates[3] 
  65.     = { SND_RATE_CODEC, SND_RATE_LOW, SND_RATE_HIGH };
  66.  
  67. static double _rowToSamplingRate(int row)
  68. {
  69.     if (row < 0 || row > 2) row = 2;
  70.     return supportedRates[row];
  71. }
  72.  
  73. static int deq(double d1, double d2)
  74. /* Return 1 if d1 and d2 are "equal" to within one int step */
  75. {
  76.     return ((abs(d1-d2)<=1.0) ? 1 : 0);
  77. }
  78.  
  79. static int _samplingRateToRow(double srate)
  80. {
  81.     int i;
  82.     for (i=0; i<3; i++)
  83.       if (deq(srate,supportedRates[i]))
  84.     return i;
  85.     return 2; /* Try to convert unsupported rates to 44.1 KHz */
  86. }
  87.  
  88. - soundTemplate
  89. /* 
  90.  * Create an empty sound conveying the requested format
  91.  */
  92. {
  93.     if (!newSound)
  94.       newSound = [[Sound alloc] init];
  95.     [newSound    setDataSize:sizeof(SNDCompressionSubheader)
  96.              /* Conceptually, dataSize is 0 above. However,
  97.         if the new format is COMPRESSED a "subheader" 
  98.         containing compression parameters is required, and
  99.         for historical reasons, the subheader is counted
  100.         as data for allocation purposes. */
  101.     dataFormat:newDataFormat
  102.     samplingRate:newSamplingRate
  103.     channelCount:newChannelCount
  104.     infoSize:0];      
  105.     return newSound;
  106. }
  107.  
  108. - setSound:nSound
  109. {
  110.     double samplingRate;
  111.     int channelCount;
  112.     int dataFormat;
  113.     int format_row;
  114.     int srate_row;
  115.     int size;
  116.  
  117.     sound = nSound;
  118.  
  119.     newSamplingRate = samplingRate = [sound samplingRate];
  120.     newChannelCount = channelCount =und channelCount];
  121.     newDataFormat = dataFormat = [sound dataFormat];
  122.     size = [sound dataSize];
  123.  
  124.     format_row = _dataFormatToRow(dataFormat);
  125.     srate_row = _samplingRateToRow(samplingRate);
  126.  
  127.     [newChnMtx selectCellAt:channelCount-1:0];
  128.     [newFmtMtx selectCellAt:format_row:0];
  129.     [newFsMtx selectCellAt:srate_row:0];
  130.  
  131.     return self;
  132. }
  133.  
  134. - revert:sender
  135. {
  136.     [self setSound:sound];
  137.     return self;
  138. }
  139.  
  140. - setNewChn:sender
  141. {
  142.     newChannelCount = 1+[sender selectedRow];
  143.     return self;
  144. }
  145.  
  146. - setNewFmt:sender
  147. {
  148.     newDataFormat = _rowToDataFormat([sender selectedRow]);
  149.     return self;
  150. }
  151.  
  152. - setNewFs:sender
  153. {
  154.     newSamplingRate = _rowToSamplingRate([sender selectedRow]);
  155.     return self;
  156. }
  157.  
  158. @end
  159.