Ejemplo n.º 1
0
static PyObject *Snd_SPBRecord(PyObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSErr _err;
	SPBPtr inParamPtr;
	Boolean asynchFlag;
	if (!PyArg_ParseTuple(_args, "O&b",
	                      SPBObj_Convert, &inParamPtr,
	                      &asynchFlag))
		return NULL;
	_err = SPBRecord(inParamPtr,
	                 asynchFlag);
	if (_err != noErr) return PyMac_Error(_err);
	Py_INCREF(Py_None);
	_res = Py_None;
	return _res;
}
Ejemplo n.º 2
0
int audio_open(snd_node *n, long *f)
{
  buffer_state *data = (buffer_state *)malloc(sizeof(buffer_state));
  n->u.audio.descriptor = (void *)data;
  OSErr	err;
  Fixed sampleRateFixed;

  data->frameSize = snd_bytes_per_frame(n);

  data->bufferSize = (int) (n->format.srate * (double)data->frameSize);
  if (n->u.audio.latency > 0.0)
    data->bufferSize = (int)(n->format.srate * n->u.audio.latency) * data->frameSize;

  /* Calculate sample rate as an unsigned fixed-point number */
  if (n->format.srate > 65535.0 ||
    n->format.srate < 1.0)
    sampleRateFixed = 0xAC440000; /* Fixed for 44100 */
  else {
    unsigned short numerator = (unsigned short)n->format.srate;
    unsigned short denominator = (unsigned short)(65536.0*(n->format.srate - numerator));
    sampleRateFixed = (numerator << 16) | denominator;
  }

  /* Open device for recording or playback, depending on mode selected */

  if (n->write_flag == SND_READ) {
    /* recording */
    short gainControl = 0; /* off */
    short numChannels = n->format.channels;
    short continuousRecording = 1; /* on */
    short playthroughVolume = 0; /* off */
    OSType quality = 'cd  ';
    short sampleSize = 16;
    short twos = 0; /* i.e. signed */
    OSType compression = 'NONE';
    SoundInfoList infoList;
    int len, i, x, y, z;
    short selected = 0;
    char *device;
    char *input;
    char **buffer;
    char oneInput[256];
    char *deviceData;
  
    data->recording = 1;
    
    len = strlen(n->u.audio.devicename);
    device = new char[len+1];
    input = new char[len+1];
    
    strcpy(device, n->u.audio.devicename);
    input[0] = 0;
    for(i=0; i<len; i++) {
       if (device[i] == '\n') {
          device[i] = 0;
          strcpy(input, &device[i+1]);
          i = len;
       }
    }
    
    err = SPBOpenDevice(c2pstr(device), siWritePermission, &data->refnum);
    if (err)
      return !SND_SUCCESS;
    
    SPBGetDeviceInfo (data->refnum, siInputAvailable, &infoList);

    SPBGetDeviceInfo (data->refnum, siInputSourceNames, &buffer);
    deviceData = buffer[0] + 2;
   
    for(int x=0; x<infoList.count; x++) {
      int y = *deviceData++;
      z = 0;
      while(y) {
        oneInput[z++] = *deviceData++;
        y--;
      }
      oneInput[z] = 0;
      if (!strcmp(oneInput, input))
        selected = x;
    }
    
    selected++;
    SPBSetDeviceInfo (data->refnum, siInputSource, &selected);
    
    SPBSetDeviceInfo(data->refnum, 'qual', &quality);

    SPBSetDeviceInfo(data->refnum, 'agc ', &gainControl);
    
    SPBSetDeviceInfo(data->refnum, 'srat', &sampleRateFixed);

    SPBSetDeviceInfo(data->refnum, 'plth', &playthroughVolume);
    
    err = SPBSetDeviceInfo(data->refnum, 'ssiz', &sampleSize);
    if (err)
      return !SND_SUCCESS;

    err = SPBSetDeviceInfo(data->refnum, 'chan', &numChannels);
    if (err)
      return !SND_SUCCESS;
  
    err = SPBSetDeviceInfo(data->refnum, 'cont', &continuousRecording);
    if (err)
      return !SND_SUCCESS;
  
    err = SPBSetDeviceInfo(data->refnum, 'twos', &twos);
    if (err)
      return !SND_SUCCESS;

    err = SPBSetDeviceInfo(data->refnum, 'comp', &compression);
    if (err)
      return !SND_SUCCESS;
      
    data->recBuffer = (char *)malloc(data->bufferSize);
    data->recqStart = 0;
    data->recqEnd = 0;
    data->starved = 0;
    
    data->params.inRefNum = data->refnum;
    data->params.count = 0; /* data->bufferSize; /* bytes to record */
    data->params.milliseconds = 0; /* param will be ignored; use count */
    data->params.bufferLength = 0; /* ignore buffer */
    data->params.bufferPtr = NULL; /* ignore buffer */
    data->params.completionRoutine = NULL;
    data->params.userLong = (long)data;
    data->params.unused1 = 0;
    data->params.interruptRoutine = NewSIInterruptUPP(recordingCallback);

    err = SPBRecord(&data->params, true);
    if (err)
      return !SND_SUCCESS;
    
  }
  else {
    /* playback */

    data->recording = 0;
    data->chan = NULL;
    
    
    
    
    
    ////////////////////////
    
    
    err = SndNewChannel(&data->chan, sampledSynth, 0, NULL);
  	
    if (err)
      return !SND_SUCCESS;
  	  
    data->buffer = (char *)malloc(data->bufferSize);
    
    data->nextBufferSize = data->bufferSize * 3;
    data->nextBuffer = (char *)malloc(data->nextBufferSize);   

    if (!data->buffer || !data->nextBuffer)
      return !SND_SUCCESS;
    
    data->chan->callBack = NewSndCallBackUPP(playbackCallback);
    
    data->header.samplePtr = data->buffer;
    data->header.numChannels = n->format.channels;
    data->header.sampleRate = sampleRateFixed;
    data->header.loopStart = 0;
    data->header.loopEnd = 0;
    data->header.encode = cmpSH;
    data->header.baseFrequency = kMiddleC;
    // data->header.AIFFSampleRate = 0;  -- this parameter is unused
    data->header.markerChunk = NULL;
    data->header.format = kSoundNotCompressed;
    data->header.futureUse2 = NULL;
    data->header.stateVars = NULL;
    data->header.leftOverSamples = NULL;
    data->header.compressionID = 0;
    data->header.packetSize = 0;
    data->header.snthID = 0;
    data->header.sampleSize = 16;
    data->header.sampleArea[0] = 0;
    
    data->playCmd.cmd = bufferCmd;
    data->playCmd.param1 = 0; //unused
    data->playCmd.param2 = (long)&data->header;
    
    data->callCmd.cmd = callBackCmd;
    data->callCmd.param1 = 0;
    data->callCmd.param2 = (long)data;
    
    data->curBuffer = 0;
    data->curSize = 0;
    data->firstTime = 1;
    data->finished = 0;
    data->busy = 0;
    data->flushing = 0;
  }
  
  return SND_SUCCESS;
}