예제 #1
0
파일: sound.c 프로젝트: hhirsch/netrek
/*
 * Open an audio file, check for recognized type, and read.
 */
static short *sfxLoadAudioFile(char *dirName, char *fileName, AFframecount *size) {
	int fd;
	int sampleWidth;
	int sampleFmt;
	char *name;
	short *data;
	AFfilehandle file;

	if (!(name = malloc(strlen(dirName)+strlen(fileName)+2)))
		return NULL;

	(void) sprintf(name, "%s/%s", dirName, fileName);

	if ((fd=open(name, O_RDONLY)) < 0) {
		(void) fprintf(stderr, "Could not open audio file `%s': %s\n", name, strerror(errno));
		free(name);
		return NULL;
	}

	switch(AFidentifyfd(fd)) {
	  case AF_FILE_AIFF:
	  case AF_FILE_AIFFC:
		break;

	  default:
		(void) fprintf(stderr, "%s: unrecognized file type -- convert to AIFF or AIFC\n", name);
		free(name);
		return NULL;
	}
	file = AFopenfd(fd, "r", AF_NULL_FILESETUP);

	if (file == AF_NULL_FILEHANDLE) {
		fprintf(stderr, "%s: failed to attach an audio file struct\n", name);
		free(name);
		return NULL;
	}

	if ((int)AFgetchannels(file, AF_DEFAULT_TRACK) != 2) {
		fprintf(stderr, "%s: does not have 2 channels\n", name);
		free(name);
		AFclosefile(file);
		return NULL;
	}

	if (AFgetrate(file, AF_DEFAULT_TRACK) != 16000) {
		fprintf(stderr, "%s: is not recorded at 16 KHz\n", name);
		free(name);
		AFclosefile(file);
		return NULL;
	}

	AFgetsampfmt(file, AF_DEFAULT_TRACK, &sampleFmt, &sampleWidth);
	if (sampleWidth != 16) {
		fprintf(stderr, "%s: is not recorded with 16 bit samples\n", name);
		free(name);
		AFclosefile(file);
		return NULL;
	}
	free(name);

	*size = AFgetframecnt(file, AF_DEFAULT_TRACK);

	if ((data=(short *)malloc((size_t)(*size * 2 * sizeof(short)))) == NULL) {
		(void) fprintf(stderr, "%s: out of memory for %lld samples\n", name, *size);
		*size = 0;
		AFclosefile(file);
		return NULL;
	}

	if (AFreadframes(file, AF_DEFAULT_TRACK, data, (int)*size) != (int)*size) {
		(void) fprintf(stderr, "%s: error reading\n", name);
		free(data);
		data = NULL;
		*size = 0L;
	}

	*size *= 2L;
	AFclosefile(file);
	return data;
}
예제 #2
0
main() 
{
  LS_DATA ls_data;
  FILE *fp;
  double gains[MAX_CHANNELS];
  int azimuth=-10;
  int elevation=14;
  double *gainptr,  gain;
  short *inptr,*outptr;
  short out[BUFFER_LENGTH][MAX_CHANNELS];
  ALconfig c;
  AFfilehandle fh;
  ALport p;

  short *in;
  long frames;

  int i,j,k;
  int numchannels = 8; /* change this according your output device*/
  int ls_set_dim = 3;
  int ls_num = 8;
  int ls_dirs[MAX_FIELD_AM]={-30,0,  30,0, -45,45,  45,45,  -90,0, 
			     90,0,  180,0,  180,45};
  /* change these according to your loudspeaker positioning*/
  

  /* defining loudspeaker data */
  define_loudspeakers(&ls_data, ls_set_dim, ls_num, ls_dirs);
     /* ls_data is a struct containing matrices etc
     ls_set_dim  is 2 if loudspeakers are on a (horizontal) plane
     ls_set_dim  is 3 if also elevated or descended loudpeakers exist
     ls_num is the number of loudspeakers
     ls_dirs is an array containing the angular directions of loudsp*/

  
  /* gain factors for virtual source in direction
     (int azimuth, int elevation) */
  vbap(gains, &ls_data, azimuth, elevation);  
     /* panning monophonic stream  float *in 
     to multiple outputs           float *out[]
     with gain factors             float *gains  */
  

  /* input audio*/
  if((fh=afOpenFile("myaiff.aiff","r",0))==NULL){
    fprintf(stderr, "Could not open file myaiff.aiff\n");
    exit(-1);
  }
  frames=AFgetframecnt(fh,AF_DEFAULT_TRACK);
  if(afGetChannels(fh, AF_DEFAULT_TRACK) != 1){
    fprintf(stderr, "Supports only mono aiff-files\n");
    exit(-1);
  }
  in= malloc(frames*sizeof(short)*2);
  afReadFrames(fh,AF_DEFAULT_TRACK,in,frames);


  /*opening the audio port*/
  c = alNewConfig();
  if (!c) {
    printf("Couldn't create ALconfig:%s\n", alGetErrorString(oserror()));
    exit(-1);
  }
  alSetChannels(c,numchannels);
  ALsetqueuesize(c,BUFFER_LENGTH);
  p = alOpenPort("alVBAP example","w",c);
  if (!p) {
    printf("port open failed:%s\n", alGetErrorString(oserror()));
    exit(-1);
  }
  


  fprintf(stderr,"\nPanning audio");
  for(j=0;j<(frames/BUFFER_LENGTH);j++){
    inptr = &(in[j*BUFFER_LENGTH]); /* audio to be panned  */
    outptr= out[0];         

    for (i=0; i<BUFFER_LENGTH; i++){    /* panning */ 
      gainptr=gains;
      for (k=0; k<numchannels; k++){
	*outptr++ = (short) ((double) *inptr * *gainptr++); 
      }
      inptr++;
    }
    alWriteFrames(p, out, BUFFER_LENGTH); /* write frames */
    fprintf(stderr,".");
  }

  /*write rest samples out*/

  inptr = &(in[j*BUFFER_LENGTH]); /* partial buffer  */
  outptr= out[0];
  for (i=0; i<(frames-BUFFER_LENGTH*j); i++){    /* panning */ 
    gainptr=gains;
    for (k=0; k<numchannels; k++){
      *outptr++ = (short) ((double) *inptr * *gainptr++); 
    }
    inptr++;
  }
  for (;i<BUFFER_LENGTH; i++){    /* zeros  */ 
    for (k=0; k<numchannels; k++){
      *outptr++ = 0; 
    }
  }

  alWriteFrames(p, out, BUFFER_LENGTH); /* write frames */
  fprintf(stderr,".");

  printf("\n\nDone!\n\n");
}