Exemple #1
0
static void set_defaults()
{
    config.byte_order = DetermineByteOrder();
    if(config.byte_order==order_unknown) ERROR("Can't determine byte order");

    config.mpeg.type = 1;
    config.mpeg.layr = 2;
    config.mpeg.mode = (config.wave.channels==1) ? MODE_MONO :MODE_STEREO;
    config.mpeg.bitr = 128;
    config.mpeg.psyc = 2;
    config.mpeg.emph = 0; 
    config.mpeg.crc  = 0;
    config.mpeg.ext  = 0;
    config.mpeg.mode_ext  = 0;
    config.mpeg.copyright = 0;
    config.mpeg.original  = 1;
}
int read_samples_pcm(lame_global_flags *gfp,short sample_buffer[2304], int frame_size,int samples_to_read)
{
    int samples_read;
    int rcode;
    int iswav=(gfp->input_format==sf_wave);

    samples_read = fread(sample_buffer, sizeof(short), samples_to_read, musicin);
    if (ferror(musicin)) {
      fprintf(stderr, "Error reading input file\n");
      exit(2);
    }

    /*
       Samples are big-endian. If this is a little-endian machine
       we must swap
     */
    if ( NativeByteOrder == order_unknown )
      {
	NativeByteOrder = DetermineByteOrder();
	if ( NativeByteOrder == order_unknown )
	  {
	    fprintf( stderr, "byte order not determined\n" );
	    exit( 1 );
	  }
      }
    /* intel=littleEndian */
    if (!iswav && ( NativeByteOrder == order_littleEndian ))
      SwapBytesInWords( sample_buffer, samples_read );

    if (iswav && ( NativeByteOrder == order_bigEndian ))
      SwapBytesInWords( sample_buffer, samples_read );

    if (gfp->swapbytes==TRUE)
      SwapBytesInWords( sample_buffer, samples_read );


    rcode=samples_read;
    if (samples_read < frame_size) {
      if (samples_read<0) samples_read=0;
      /*fprintf(stderr,"Insufficient PCM input for one frame - fillout with zeros\n");
      */
      for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
    }
    return(rcode);
}
Exemple #3
0
wave_info_t *wave_init(char *inPath)
{
    unsigned char wave_header_buffer[40];   // HH fixed
    int wave_header_stereo = -1;
    unsigned long samplerate;
    enum byte_order NativeByteOrder = order_unknown;


    wave_info_t *wave_info = NULL;
    FILE *file;

    if ((file = fopen(inPath, "rb")) == NULL) {
        printf("WAV: cannot open input file: %s\n", inPath);
        return (NULL);
    }


    /************** WAVE ************************/
    /* Nick Burch <*****@*****.**> */
    /********************************************/
    /* Wave File Headers: (Dec) */
    /* 8-11 = "WAVE" */
    /* 22 = Stereo / Mono */
    /* 01 = mono, 02 = stereo */
    /* 24 = Sampling Frequency */
    /* 32 = Data Rate */
    /* 01 = x1 (8bit Mono) */
    /* 02 = x2 (8bit Stereo or */
    /* 16bit Mono) */
    /* 04 = x4 (16bit Stereo) */
    /********************************************/

    fseek(file, 0, SEEK_SET);
    fread(wave_header_buffer, 1, 40, file);

    if (wave_header_buffer[8] == 'W' && wave_header_buffer[9] == 'A'
        && wave_header_buffer[10] == 'V' && wave_header_buffer[11] == 'E') {
        printf("Parsing Wave File Header\n");
        if (NativeByteOrder == order_unknown) {
            NativeByteOrder = DetermineByteOrder();
            if (NativeByteOrder == order_unknown) {
                printf("byte order not determined\n");
                fclose(file);
                return (NULL);
            }
        }

        if (NativeByteOrder == order_littleEndian) {
            samplerate = wave_header_buffer[24] +
                (wave_header_buffer[25] << 8) +
                (wave_header_buffer[26] << 16) + (wave_header_buffer[27] << 24);
        } else {
            samplerate = wave_header_buffer[27] +
                (wave_header_buffer[26] << 8) +
                (wave_header_buffer[25] << 16) + (wave_header_buffer[24] << 24);
        }

        /* Wave File */
        switch (samplerate) {
            case 44100:
            case 48000:
            case 32000:
            case 24000:
            case 22050:
            case 16000:
                printf(">>> %ld Hz sampling freq selected\n", samplerate);
                break;
            default:
                /* Unknown Unsupported Frequency */
                printf(">>> Unknown samp freq %ld Hz in Wave Header\n", samplerate);
                printf(">>> Default 44.1 kHz samp freq selected\n");
                samplerate = 44100;
                break;
        }

        if ((long) wave_header_buffer[22] == 1) {
            printf(">>> Input Wave File is Mono\n");
            wave_header_stereo = 0;
        }
        if ((long) wave_header_buffer[22] == 2) {
            printf(">>> Input Wave File is Stereo\n");
            wave_header_stereo = 1;
        }
        if ((long) wave_header_buffer[32] == 1) {
            printf(">>> Input Wave File is 8 Bit\n");
            printf("Input File must be 16 Bit! Please Re-sample");
            fclose(file);
            return (NULL);
        }
        if ((long) wave_header_buffer[32] == 2) {
            if (wave_header_stereo == 1) {
                printf(">>> Input Wave File is 8 Bit\n");
                printf("Input File must be 16 Bit! Please Re-sample");
                fclose(file);
                return (NULL);
            }
        }

        /* should probably use the wave header to determine size here FIXME MFC Feb 2003 */
        if (fseek(file, 44, SEEK_SET) != 0) {
            /* there's a way of calculating the size of the wave header. i'll just jump 44 to start 
               with */
            printf("Could not seek to PCM sound data in \"%s\".\n", inPath);
            fclose(file);
            return (NULL);
        }
        // Successfully processed the wave header
        wave_info = (wave_info_t *) calloc(1, sizeof(wave_info_t));
        wave_info->soundfile = file;
        if (wave_header_stereo == 1)
            wave_info->channels = 2;
        else
            wave_info->channels = 1;
        wave_info->samplerate = samplerate;

        // UNKNOWN. But we really should be able to work 
        // it out. FIX THIS. MFC May03.
        wave_info->num_samples = -1;

        // Enable byte swap for big endian machines
        if (NativeByteOrder == order_bigEndian)
            wave_info->byteswap = 1;
        else
            wave_info->byteswap = 0;

        return (wave_info);
    }
    // not a wave file
    fclose(file);
    return (NULL);
}
FILE * OpenSndFile(lame_global_flags *gfp,const char* lpszFileName, int default_samp,
int default_channels)
{
  input_bitrate=0;
  if (gfp->input_format==sf_mp3) {
#ifdef AMIGA_MPEGA
    if (-1==lame_decode_initfile(lpszFileName,&num_channels,&samp_freq,&input_bitrate,&num_samples)) {
      fprintf(stderr,"Error reading headers in mp3 input file %s.\n", lpszFileName);
      exit(1);
    }
#endif
#ifdef HAVEMPGLIB
    if ((musicin = fopen(lpszFileName, "rb")) == NULL) {
      fprintf(stderr, "Could not find \"%s\".\n", lpszFileName);
      exit(1);
    }
    if (-1==lame_decode_initfile(musicin,&num_channels,&samp_freq,&input_bitrate,&num_samples)) {
      fprintf(stderr,"Error reading headers in mp3 input file %s.\n", lpszFileName);
      exit(1);
    }
#endif
    gs_wfInfo.samples=num_samples;
    gs_wfInfo.channels=num_channels;
    gs_wfInfo.samplerate=samp_freq;

  } else {

    /* Try to open the sound file */
    /* set some defaults incase input is raw PCM */
    gs_wfInfo.seekable=(gfp->input_format!=sf_raw);  /* if user specified -r, set to not seekable */
    gs_wfInfo.samplerate=default_samp;
    gs_wfInfo.pcmbitwidth=16;
    gs_wfInfo.channels=default_channels;
    if (DetermineByteOrder()==order_littleEndian) {
      if (gfp->swapbytes) gs_wfInfo.format=SF_FORMAT_RAW_BE;
      else gs_wfInfo.format=SF_FORMAT_RAW_LE;
    } else {
      if (gfp->swapbytes) gs_wfInfo.format=SF_FORMAT_RAW_LE;
      else gs_wfInfo.format=SF_FORMAT_RAW_BE;
    }

    gs_pSndFileIn=sf_open_read(lpszFileName,&gs_wfInfo);

        /* Check result */
	if (gs_pSndFileIn==NULL)
	{
	        sf_perror(gs_pSndFileIn);
		fprintf(stderr, "Could not open sound file \"%s\".\n", lpszFileName);
		exit(1);
	}

    if ((gs_wfInfo.format==SF_FORMAT_RAW_LE) || 
	(gs_wfInfo.format==SF_FORMAT_RAW_BE)) 
      gfp->input_format=sf_raw;

#ifdef _DEBUG_SND_FILE
	printf("\n\nSF_INFO structure\n");
	printf("samplerate        :%d\n",gs_wfInfo.samplerate);
	printf("samples           :%d\n",gs_wfInfo.samples);
	printf("channels          :%d\n",gs_wfInfo.channels);
	printf("pcmbitwidth       :%d\n",gs_wfInfo.pcmbitwidth);
	printf("format            :");

	/* new formats from [email protected]  1/2000 */
        if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_WAV)
	  printf("Microsoft WAV format (big endian). ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AIFF)
	  printf("Apple/SGI AIFF format (little endian). ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AU)
	  printf("Sun/NeXT AU format (big endian). ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AULE)
	  printf("DEC AU format (little endian). ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_RAW)
	  printf("RAW PCM data. ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_PAF)
	  printf("Ensoniq PARIS file format. ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_SVX)
	  printf("Amiga IFF / SVX8 / SV16 format. ");
	if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_NIST)
	  printf("Sphere NIST format. ");

	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM)
	  printf("PCM data in 8, 16, 24 or 32 bits.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT)
	  printf("32 bit Intel x86 floats.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_ULAW)
	  printf("U-Law encoded.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_ALAW)
	  printf("A-Law encoded.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_IMA_ADPCM)
	  printf("IMA ADPCM.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_MS_ADPCM)
	  printf("Microsoft ADPCM.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_BE)
	  printf("Big endian PCM data.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_LE)
	  printf("Little endian PCM data.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_S8)
	  printf("Signed 8 bit PCM.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8)
	  printf("Unsigned 8 bit PCM.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_SVX_FIB)
	  printf("SVX Fibonacci Delta encoding.");
	if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_SVX_EXP)
	  printf("SVX Exponential Delta encoding.");




	printf("\n");
	printf("pcmbitwidth       :%d\n",gs_wfInfo.pcmbitwidth);
	printf("sections          :%d\n",gs_wfInfo.sections);
	printf("seekable          :\n",gs_wfInfo.seekable);
#endif
  }

  if (gs_wfInfo.samples==MAX_U_32_NUM) {
    struct stat sb;
    /* try to figure out num_samples */
    if (0==stat(lpszFileName,&sb)) {
      /* try file size, assume 2 bytes per sample */
      if (gfp->input_format == sf_mp3) {
	FLOAT totalseconds = (sb.st_size*8.0/(1000.0*GetSndBitrate()));
	gs_wfInfo.samples= totalseconds*GetSndSampleRate();
      }else{
	gs_wfInfo.samples = sb.st_size/(2*GetSndChannels());
      }
    }
  }
  return musicin;    
}