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

📄 io.c

📁 音频信号的重采样程序,如44.1K的WAV转换成采样频率为48K的WAV.
💻 C
📖 第 1 页 / 共 3 页
字号:
}Float mus_file_set_prescaler(int tfd, Float val) {  io_fd *fd;  if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(0.0);  fd = io_fds[tfd];  fd->prescaler = val;   return(val);}char *mus_file_fd_name(int tfd){  io_fd *fd;  if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(NULL);  fd = io_fds[tfd];  return(fd->name);}int mus_file_set_chans(int tfd, int chans){  io_fd *fd;  if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);  fd = io_fds[tfd];  fd->chans = chans;  return(MUS_NO_ERROR);}/* ---------------- open, creat, close ---------------- */int mus_file_open_read(const char *arg) {  int fd;#ifdef MUS_WINDOZE  fd = OPEN(arg, O_RDONLY | O_BINARY, 0);#else  fd = OPEN(arg, O_RDONLY, 0);#endif  return(fd);}bool mus_file_probe(const char *arg) {#if HAVE_ACCESS  return(access(arg, F_OK) == 0);#else  int fd;#ifdef O_NONBLOCK  fd = OPEN(arg, O_RDONLY, O_NONBLOCK);#else  fd = OPEN(arg, O_RDONLY, 0);#endif  if (fd == -1) return(false);  CLOSE(fd, arg);  return(true);#endif}int mus_file_open_write(const char *arg){  int fd;#ifdef MUS_WINDOZE  if ((fd = OPEN(arg, O_RDWR | O_BINARY, 0)) == -1)#else  if ((fd = OPEN(arg, O_RDWR, 0)) == -1)#endif    fd = CREAT(arg, 0666);  /* equivalent to the new open(arg, O_RDWR | O_CREAT | O_TRUNC, 0666) */  else lseek(fd, 0L, SEEK_END);  return(fd);}int mus_file_create(const char *arg){  return(CREAT(arg, 0666));}int mus_file_reopen_write(const char *arg){  int fd;#ifdef MUS_WINDOZE  fd = OPEN(arg, O_RDWR | O_BINARY, 0);#else  fd = OPEN(arg, O_RDWR, 0);#endif  return(fd);}int mus_file_close(int fd){  io_fd *fdp;  int close_result = 0;  if ((io_fds == NULL) || (fd >= io_fd_size) || (fd < 0) || (io_fds[fd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);  fdp = io_fds[fd];#if USE_SND  CLOSE(fd, fdp->name);#else  close_result = close(fd);#endif  if (fdp->name) {FREE(fdp->name); fdp->name = NULL;}  FREE(fdp);  io_fds[fd] = NULL;  if (close_result < 0)    return(MUS_CANT_CLOSE_FILE);  return(MUS_NO_ERROR);}/* ---------------- seek ---------------- */off_t mus_file_seek_frame(int tfd, off_t frame){  io_fd *fd;  if (io_fds == NULL)     return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frame: no file descriptors!"));  if (tfd < 0)    return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frame: file descriptor = %d?", tfd));  if ((tfd >= io_fd_size) ||       (io_fds[tfd] == NULL))    return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED,		     "mus_file_seek_frame: file descriptors not realloc'd? (tfd: %d, io_fd_size: %d)", tfd, io_fd_size));  fd = io_fds[tfd];  if (fd->data_format == MUS_UNKNOWN)     return(mus_error(MUS_NOT_A_SOUND_FILE, "mus_file_seek_frame: invalid data format for %s", fd->name));  return(lseek(tfd, fd->data_location + (fd->chans * frame * fd->bytes_per_sample), SEEK_SET));}/* ---------------- mulaw/alaw conversions ---------------- * *      x : input signal with max value 32767 *     mu : compression parameter (mu = 255 used for telephony) *     y = (32767/log(1+mu))*log(1+mu*abs(x)/32767)*sign(x); -- this isn't right -- typo? *//* from sox g711.c */#define	QUANT_MASK	(0xf)		/* Quantization field mask. */#define	SEG_SHIFT	(4)		/* Left shift for segment number. */static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,  0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};static int search(int val, short *table, int size){  int i;  for (i = 0; i < size; i++) {if (val <= *table++) return (i);}  return (size);}static unsigned char to_alaw(int pcm_val){  int mask, seg;  if (pcm_val >= 0) mask = 0xD5; else {mask = 0x55; pcm_val = -pcm_val - 8;}  seg = search(pcm_val, seg_end, 8);  if (seg >= 8)	return (0x7F ^ mask);  else     {      unsigned char	aval;      aval = seg << SEG_SHIFT;      if (seg < 2) aval |= (pcm_val >> 4) & QUANT_MASK; else aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;      return (aval ^ mask);    }}static const int alaw[256] = { -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,  -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,  -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,  -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,  -344, -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424,  -88, -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168,  -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,  -688, -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848,  5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,  2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,  22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,  11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,  344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424,  88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168,  1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,  688, 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848};#define	BIAS		(0x84)		/* Bias for linear code. */static unsigned char to_mulaw(int pcm_val){  int mask;  int seg;  if (pcm_val < 0) {pcm_val = BIAS - pcm_val; mask = 0x7F;} else {pcm_val += BIAS; mask = 0xFF;}  seg = search(pcm_val, seg_end, 8);  if (seg >= 8) return (0x7F ^ mask);  else     {      unsigned char	uval;      uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);      return (uval ^ mask);    }}/* generated by SNDiMulaw on a NeXT */static const int mulaw[256] = {  -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860,   -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,   -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908,   -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772, -3644,   -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,   -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988,   -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428,   -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148,   -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124, 31100,   30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,   15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340,   8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604,   4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236,   2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052,   988, 924, 876, 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,   356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104, 96,   88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0};/* ---------------- read ---------------- */#define BUFLIM (64 * 1024)#if SNDLIB_USE_FLOATS  #define MUS_SAMPLE_UNSCALED(n) ((n) / 32768.0)#else  #define MUS_SAMPLE_UNSCALED(n) ((n) * (1 << (MUS_SAMPLE_BITS - 16)))#endifstatic int mus_read_any_1(int tfd, int beg, int chans, int nints, mus_sample_t **bufs, mus_sample_t **cm, char *inbuf){  int loclim;  io_fd *fd;  int bytes, j, lim, siz, total, leftover, total_read, k, loc, oldloc, siz_chans, buflim, format;  unsigned char *jchar;  char *charbuf = NULL;  mus_sample_t *buffer;  float prescaling;  bool from_buffer = false;  if (nints <= 0) return(0);  if (inbuf) from_buffer = true;  if (!from_buffer)    {      if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL))	return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_read: no file descriptors!"));      fd = io_fds[tfd];      if (fd->data_format == MUS_UNKNOWN) 	return(mus_error(MUS_FILE_CLOSED, "mus_read: invalid data format for %s", fd->name));      format = fd->data_format;      siz = fd->bytes_per_sample;      if ((format == MUS_OUT_FORMAT) && 	  (chans == 1) && 	  (beg == 0)#if SNDLIB_USE_FLOATS 	  && (fd->prescaler == 1.0)#endif	  )	{	  bytes = nints * siz;	  total = read(tfd, (char *)(bufs[0]), bytes);	  if (total != bytes)	    {	      if (total <= 0)		memset((void *)(bufs[0]), 0, bytes);	      else		{		  int i, last;		  last = beg + nints;		  for (i = total / siz; i < last; i++)		    bufs[0][i] = MUS_SAMPLE_0;		}	    }	  return(total / siz);	}      prescaling = (float)(fd->prescaler * MUS_FLOAT_TO_SAMPLE(1.0));      /* not MUS_FLOAT_TO_SAMPLE(fd->prescaler) here because there's a possible cast to int which can overflow */      charbuf = (char *)CALLOC(BUFLIM, sizeof(char));       if (charbuf == NULL) 	return(mus_error(MUS_MEMORY_ALLOCATION_FAILED, "mus_read: IO buffer allocation failed"));    }  else    {      charbuf = inbuf;      siz = mus_bytes_per_sample(tfd);      prescaling = (float)(MUS_FLOAT_TO_SAMPLE(1.0));      format = tfd;    }  siz_chans = siz * chans;  leftover = (nints * siz_chans);  k = (BUFLIM) % siz_chans;  if (k != 0) /* for example, 3 channel output of 1-byte (mulaw) samples will need a mod 3 buffer */    buflim = (BUFLIM) - k;  else buflim = BUFLIM;  total_read = 0;  loc = beg;  while (leftover > 0)    {      bytes = leftover;      if (bytes > buflim) 	{	  leftover = (bytes - buflim); 	  bytes = buflim;	}       else leftover = 0;      if (!from_buffer)	{	  total = read(tfd, charbuf, bytes); 	  if (total <= 0) 	    {	      /* zero out trailing section (some callers don't check the returned value) -- this added 9-May-99 */	      lim = beg + nints;	      if (loc < lim)		for (k = 0; k < chans; k++)		  if ((cm == NULL) || (cm[k]))		    {		      if (loc == 0)			memset((void *)(bufs[k]), 0, lim * sizeof(mus_sample_t));		      else			for (j = loc; j < lim; j++) 			  bufs[k][j] = MUS_SAMPLE_0;		    }	      FREE(charbuf);	      return(total_read);	    }	  lim = (int) (total / siz_chans);  /* this divide must be exact (hence the buflim calc above) */	}      else	{	  lim = nints; /* frames in this case */	  leftover = 0;	}      total_read += lim;      oldloc = loc;      for (k = 0; k < chans; k++)	{	  if ((cm == NULL) || (cm[k]))	    {	      buffer = (mus_sample_t *)(bufs[k]);	      if (buffer)		{		  loc = oldloc;		  loclim = loc + lim;		  jchar = (unsigned char *)charbuf;		  jchar += (k * siz);		  switch (format)		    {		    case MUS_BSHORT:               		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE(m_big_endian_short(jchar)); 		      break;		    case MUS_LSHORT: 		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE(m_little_endian_short(jchar)); 		      break;		    case MUS_BINT:              		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT_TO_SAMPLE(m_big_endian_int(jchar)); 		      break;		    case MUS_LINT: 		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT_TO_SAMPLE(m_little_endian_int(jchar)); 		      break;		    case MUS_BINTN:              		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT_TO_SAMPLE((m_big_endian_int(jchar) >> (32 - MUS_SAMPLE_BITS)));		      break;		    case MUS_LINTN: 		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT_TO_SAMPLE((m_little_endian_int(jchar) >> (32 - MUS_SAMPLE_BITS)));		      break;		    case MUS_MULAW:  	              		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE(mulaw[*jchar]); 		      break;		    case MUS_ALAW:                  		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE(alaw[*jchar]); 		      break;		    case MUS_BYTE:                		      for (; loc < loclim; loc++, jchar += siz_chans)			buffer[loc] = MUS_BYTE_TO_SAMPLE((signed char)(*jchar));		      break;		    case MUS_UBYTE:     	      		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_BYTE_TO_SAMPLE((int)(*jchar) - 128);		      break;		    case MUS_BFLOAT:		      if (prescaling == 1.0)			{			  for (; loc < loclim; loc++, jchar += siz_chans) 			    buffer[loc] = (mus_sample_t) (m_big_endian_float(jchar));			}		      else			{			  for (; loc < loclim; loc++, jchar += siz_chans) 			    buffer[loc] = (mus_sample_t) (prescaling * (m_big_endian_float(jchar)));			}		      break;		    case MUS_BFLOAT_UNSCALED:		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = (mus_sample_t) (MUS_SAMPLE_UNSCALED(m_big_endian_float(jchar)));		      break;		    case MUS_BDOUBLE:   		      for (; loc < loclim; loc++, jchar += siz_chans)			buffer[loc] = (mus_sample_t) (prescaling * (m_big_endian_double(jchar)));		      break;		    case MUS_BDOUBLE_UNSCALED:   		      for (; loc < loclim; loc++, jchar += siz_chans)			buffer[loc] = (mus_sample_t) (MUS_SAMPLE_UNSCALED(m_big_endian_double(jchar)));		      break;		    case MUS_LFLOAT:		      if (prescaling == 1.0)			{			  for (; loc < loclim; loc++, jchar += siz_chans) 			    buffer[loc] = (mus_sample_t) (m_little_endian_float(jchar));			}		      else			{			  for (; loc < loclim; loc++, jchar += siz_chans) 			    buffer[loc] = (mus_sample_t) (prescaling * (m_little_endian_float(jchar)));			}		      break;		    case MUS_LFLOAT_UNSCALED:    		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = (mus_sample_t) (MUS_SAMPLE_UNSCALED(m_little_endian_float(jchar)));		      break;		    case MUS_LDOUBLE:   		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = (mus_sample_t) (prescaling * (m_little_endian_double(jchar)));		      break;		    case MUS_LDOUBLE_UNSCALED:   		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = (mus_sample_t) (MUS_SAMPLE_UNSCALED(m_little_endian_double(jchar)));		      break;		    case MUS_UBSHORT:   		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE((int)(m_big_endian_unsigned_short(jchar)) - 32768);		      break;		    case MUS_ULSHORT:   		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_SHORT_TO_SAMPLE((int)(m_little_endian_unsigned_short(jchar)) - 32768);		      break;		    case MUS_B24INT:		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT24_TO_SAMPLE((int)(((jchar[0] << 24) + 								 (jchar[1] << 16) + 								 (jchar[2] << 8)) >> 8));		      break;		    case MUS_L24INT:   		      for (; loc < loclim; loc++, jchar += siz_chans) 			buffer[loc] = MUS_INT24_TO_SAMPLE((int)(((jchar[2] << 24) + 								 (jchar[1] << 16) + 								 (jchar[0] << 8)) >> 8));		      break;		    }		}	    }	}    }  if (!from_buffer) FREE(charbuf);  return(total_read);}int mus_file_read_any(int tfd, int beg, int chans, int nints, mus_sample_t **bufs, mus_sample_t **cm){  return(mus_read_any_1(tfd, beg, chans, nints, bufs, cm, NULL));}int mus_file_read_file(int tfd, int beg, int chans, int nints, mus_sample_t **bufs){  return(mus_read_any_1(tfd, beg, chans, nints, bufs, NULL, NULL));}int mus_file_read_buffer(int charbuf_data_format, int beg, int chans, int nints, mus_sample_t **bufs, char *charbuf){  return(mus_read_any_1(charbuf_data_format, beg, chans, nints, bufs, NULL, charbuf)); }int mus_file_read(int tfd, int beg, int end, int chans, mus_sample_t **bufs){  int num, rtn, i, k;  num = (end - beg + 1);  rtn = mus_read_any_1(tfd, beg, chans, num, bufs, NULL, NULL);  if (rtn == MUS_ERROR) return(MUS_ERROR);  if (rtn < num)     /* this zeroing can be fooled if the file is chunked and has trailing, non-data chunks */    for (k = 0; k < chans; k++)      {	mus_sample_t *buffer;	buffer = bufs[k];	i = rtn + beg;	/* this happens routinely in mus_outa + initial write (reads ahead in effect) */	memset((void *)(buffer + i), 0, (end - i + 1) * sizeof(mus_sample_t));      }  return(num);}

⌨️ 快捷键说明

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