⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wave.c

📁 shine定点库
💻 C
字号:
/* wave.c
 *
 * Data input functions.
 */

#include "types.h"

/*
 * wave_close:
 * -----------
 */
void wave_close(void)
{
  fclose(config.wave.file);
}

/*
 * wave_open:
 * ----------
 * Opens and verifies the header of the Input Wave file. The file pointer is
 * left pointing to the start of the samples.
 */
void wave_open(int raw, int mono_from_stereo)
{

  static char *channel_mappings[] = {NULL,"mono","stereo"};
  static char *file_type[] = {"RIFF-WAVE PCM","RAW DATA PCM"};

  /* Wave file headers can vary from this, but we're only intereseted in this format */
  struct wave_header
  {
    char riff[4];             /* "RIFF" */
    unsigned long size;       /* length of rest of file = size of rest of header(36) + data length */
    char wave[4];             /* "WAVE" */
    char fmt[4];              /* "fmt " */
    unsigned long fmt_len;    /* length of rest of fmt chunk = 16 */
    unsigned short tag;       /* MS PCM = 1 */
    unsigned short channels;  /* channels, mono = 1, stereo = 2 */
    unsigned long samp_rate;  /* samples per second = 44100 */
    unsigned long byte_rate;  /* bytes per second = samp_rate * byte_samp = 176400 */
    unsigned short byte_samp; /* block align (bytes per sample) = channels * bits_per_sample / 8 = 4 */
    unsigned short bit_samp;  /* bits per sample = 16 for MS PCM (format specific) */
    char data[4];             /* "data" */
    unsigned long length;     /* data length (bytes) */
  } header;

  /* open input file */
  if((config.wave.file = fopen(config.infile,"rb")) == NULL)
    error("Unable to open file");

  if(raw)
  {
      fseek(config.wave.file, 0, SEEK_END); /* find length of file */
      header.length = ftell(config.wave.file);
      fseek(config.wave.file, 0, SEEK_SET);
      header.channels = (mono_from_stereo) ? 1 : 2;
      header.samp_rate = config.wave.samplerate; /* 44100 if not set by user */
      header.bit_samp = 16;     /* assumed */
      header.byte_samp = header.channels * header.bit_samp / 8;
      header.byte_rate = header.samp_rate * header.byte_samp;
  }
  else /* wave */
  {
      if (fread(&header, sizeof(header), 1, config.wave.file) != 1)
        error("Invalid Header");
      if(strncmp(header.riff,"RIFF",4) != 0) error("Not a MS-RIFF file");
      if(strncmp(header.wave,"WAVE",4) != 0) error("Not a WAVE audio");
      if(strncmp(header.fmt, "fmt ",4) != 0) error("Can't find format chunk");
      if(header.tag != 1)                    error("Unknown WAVE format");
      if(header.channels > 2)                error("More than 2 channels");
      if(header.bit_samp != 16)              error("Not 16 bit");
      if(strncmp(header.data,"data",4) != 0) error("Can't find data chunk");
  }

 // printf("byte_samp = %d\n", header.byte_samp);
 // printf("length = %ld\n", header.length );
  config.wave.channels      = header.channels;
 // printf("channels = %d\n", config.wave.channels);
  config.wave.samplerate    = header.samp_rate;
 // printf("samplerate = %ld\n", config.wave.samplerate);
  config.wave.bits          = header.bit_samp;
 // printf("bits = %d\n", config.wave.bits);
  config.wave.total_samples = header.length / header.byte_samp;
 // printf("total_samples = %ld\n", config.wave.total_samples);
  config.wave.length        = header.length / header.byte_rate;
  //printf("length = %ld\n", config.wave.length);
  
  if((config.wave.channels == 1) || mono_from_stereo)
  {
    config.mpeg.channels = 1;
    config.mpeg.mode = MODE_MONO;
  }

  printf("%s, %s %ldHz %dbit, Length: %2ld:%2ld:%2ld\n",
          file_type[raw],
          channel_mappings[config.wave.channels],
          config.wave.samplerate,
          config.wave.bits,
          config.wave.length/3600,(config.wave.length/60)%60,config.wave.length%60);
}

/*
 * wave_get:
 * ---------
 * Reads samples from the file in longs. A long can
 * hold one stereo or two mono samples.
 * Returns the address of the start of the next frame or NULL if there are no
 * more. The last frame will be padded with zero's.
 */
unsigned long int *wave_get(void)
{
  int n;
  static unsigned long buff[samp_per_frame];
  n = samp_per_frame2;
 // n = config.mpeg.samples_per_frame >> (2 - config.wave.channels);
  if( memcpy(buff, buf, sizeof(unsigned long)*(short)n) == NULL )
  {
  	printf("memcpy fails\n");
  	exit(1);
  }
  //p = fread(buff,sizeof(unsigned long),(short)n,config.wave.file);
  return buff;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -