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

📄 sound.c

📁 音频信号的重采样程序,如44.1K的WAV转换成采样频率为48K的WAV.
💻 C
📖 第 1 页 / 共 3 页
字号:
/* sound.c */#include <mus-config.h>#if USE_SND  #include "snd.h"#endif#include <math.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <stddef.h>#include <sys/types.h>#include <sys/stat.h>#include <time.h>#include <stdarg.h>#if (defined(HAVE_LIBC_H) && (!defined(HAVE_UNISTD_H)))  #include <libc.h>#else  #if (!(defined(_MSC_VER)))    #include <unistd.h>  #endif  #if HAVE_STRING_H    #include <string.h>  #endif#endif#include "_sndlib.h"#include "sndlib-strings.h"static mus_error_handler_t *mus_error_handler = NULL;mus_error_handler_t *mus_error_set_handler(mus_error_handler_t *new_error_handler) {  mus_error_handler_t *old_handler;  old_handler = mus_error_handler;  mus_error_handler = new_error_handler;  return(old_handler);}#define MUS_ERROR_BUFFER_SIZE 1024static char *mus_error_buffer = NULL;int mus_error(int error, const char *format, ...){  va_list ap;  if (format == NULL) return(MUS_ERROR); /* else bus error in Mac OSX */  if (mus_error_buffer == NULL)    mus_error_buffer = (char *)CALLOC(MUS_ERROR_BUFFER_SIZE, sizeof(char));  va_start(ap, format);#if HAVE_VSNPRINTF  vsnprintf(mus_error_buffer, MUS_ERROR_BUFFER_SIZE, format, ap);#else  vsprintf(mus_error_buffer, format, ap);#endif  va_end(ap);  if (mus_error_handler)    (*mus_error_handler)(error, mus_error_buffer);  else     {      fprintf(stderr, mus_error_buffer);      fputc('\n', stderr);    }  return(MUS_ERROR);}static mus_print_handler_t *mus_print_handler = NULL;mus_print_handler_t *mus_print_set_handler(mus_print_handler_t *new_print_handler) {  mus_print_handler_t *old_handler;  old_handler = mus_print_handler;  mus_print_handler = new_print_handler;  return(old_handler);}void mus_print(const char *format, ...){  va_list ap;  if (mus_error_buffer == NULL)    mus_error_buffer = (char *)CALLOC(MUS_ERROR_BUFFER_SIZE, sizeof(char));  if (mus_print_handler)    {      va_start(ap, format);#if HAVE_VSNPRINTF      vsnprintf(mus_error_buffer, MUS_ERROR_BUFFER_SIZE, format, ap);#else      vsprintf(mus_error_buffer, format, ap);#endif      va_end(ap);      (*mus_print_handler)(mus_error_buffer);    }  else    {      va_start(ap, format);      vfprintf(stdout, format, ap);      va_end(ap);    }}static const char *mus_initial_error_names[] = {  "no error", "no frequency method", "no phase method", "null gen arg to method", "no length method",  "no free method", "no describe method", "no data method", "no scaler method",  "memory allocation failed", "unstable two pole error",  "can't open file", "no sample input", "no sample output",  "no such channel", "no file name provided", "no location method", "no channel method",  "no such fft window", "unsupported data format", "header read failed",  "unsupported header type", "file descriptors not initialized", "not a sound file", "file closed", "write error",  "header write failed", "can't open temp file", "interrupted", "bad envelope",  "audio channels not available", "audio srate not available", "audio format not available",  "no audio input available", "audio configuration not available",   "no audio lines available", "audio write error", "audio size not available", "audio device not available",  "can't close audio", "can't open audio", "audio read error", "audio amp not available",  "can't write audio", "can't read audio", "no audio read permission",   "can't close file", "arg out of range",  "midi open error", "midi read error", "midi write error", "midi close error", "midi init error", "midi misc error",  "no channels method", "no hop method", "no width method", "no file-name method", "no ramp method", "no run method",  "no increment method", "no offset method",  "no xcoeff method", "no ycoeff method", "no xcoeffs method", "no ycoeffs method", };static char **mus_error_names = NULL;static int mus_error_names_size = 0;static int mus_error_tag = MUS_INITIAL_ERROR_TAG;int mus_make_error(char *error_name) {  int new_error, err, len, i;  new_error = mus_error_tag++;  err = new_error - MUS_INITIAL_ERROR_TAG;  if (error_name)    {      if (err >= mus_error_names_size)	{	  if (mus_error_names_size == 0)	    {	      mus_error_names_size = 8;	      mus_error_names = (char **)CALLOC(mus_error_names_size, sizeof(char *));	    }	  else	    {	      len = mus_error_names_size;	      mus_error_names_size += 8;	      mus_error_names = (char **)REALLOC(mus_error_names, mus_error_names_size * sizeof(char *));	      for (i = len; i < mus_error_names_size; i++) mus_error_names[i] = NULL;	    }	}      len = strlen(error_name);      mus_error_names[err] = (char *)CALLOC(len + 1, sizeof(char));      strcpy(mus_error_names[err], error_name);    }  return(new_error);}const char *mus_error_type_to_string(int err){  if (err >= 0)    {      if (err < MUS_INITIAL_ERROR_TAG)	return(mus_initial_error_names[err]);      else	{	  err -= MUS_INITIAL_ERROR_TAG;	  if ((mus_error_names) && (err < mus_error_names_size))	    return(mus_error_names[err]);	}    }  return("unknown mus error");}static void default_mus_error(int ignore, char *msg){  /* default error handler simply prints the error message */  fprintf(stderr, msg);}static time_t local_file_write_date(const char *filename){  struct stat statbuf;  int err;  err = stat(filename, &statbuf);  if (err < 0) return((time_t)0);  return((time_t)(statbuf.st_mtime));}static bool sndlib_initialized = false;int mus_sound_initialize(void){  int err = MUS_NO_ERROR;  if (!sndlib_initialized)    {      sndlib_initialized = true;      mus_error_handler = default_mus_error;      err = mus_header_initialize();      if (err == MUS_NO_ERROR) 	err = mus_audio_initialize();      return(err);    }  return(MUS_NO_ERROR);}int mus_sample_bits(void){  /* this to check for inconsistent loads */#if SNDLIB_USE_FLOATS  return(sizeof(Float));#else  return(MUS_SAMPLE_BITS);#endif}typedef struct {  char *file_name;  /* full path -- everything is keyed to this name */  int table_pos;  off_t *aux_comment_start, *aux_comment_end;  int *loop_modes, *loop_starts, *loop_ends;  int markers, base_detune, base_note;  int *marker_ids, *marker_positions;  off_t samples, true_file_length;  off_t data_location;  int srate, chans, header_type, data_format, original_sound_format, datum_size;   off_t comment_start, comment_end;  int type_specifier, bits_per_sample, block_align, fact_samples;  time_t write_date;  mus_sample_t *maxamps;  off_t *maxtimes;} sound_file;static int sound_table_size = 0;static sound_file **sound_table = NULL;static sound_file *previous_sf = NULL; /* memoized search */static int previous_freed_sf = -1;static void free_sound_file(sound_file *sf){  previous_sf = NULL;  if (sf)    {      sound_table[sf->table_pos] = NULL;      previous_freed_sf = sf->table_pos;      if (sf->aux_comment_start) FREE(sf->aux_comment_start);      if (sf->aux_comment_end) FREE(sf->aux_comment_end);      if (sf->file_name) FREE(sf->file_name);      if (sf->loop_modes) FREE(sf->loop_modes);      if (sf->loop_starts) FREE(sf->loop_starts);      if (sf->loop_ends) FREE(sf->loop_ends);      if (sf->marker_ids) FREE(sf->marker_ids);      if (sf->marker_positions) FREE(sf->marker_positions);      if (sf->maxamps) FREE(sf->maxamps);      if (sf->maxtimes) FREE(sf->maxtimes);      FREE(sf);    }}static sound_file *add_to_sound_table(const char *name){  int i, pos;  pos = previous_freed_sf;  if (pos == -1)    {      for (i = 0; i < sound_table_size; i++)	if (sound_table[i] == NULL) 	  {	    pos = i;	    break;	  }      if (pos == -1)	{	  pos = sound_table_size;	  sound_table_size += 16;	  if (sound_table == NULL)	    sound_table = (sound_file **)CALLOC(sound_table_size, sizeof(sound_file *));	  else 	    {	      sound_table = (sound_file **)REALLOC(sound_table, sound_table_size * sizeof(sound_file *));	      for (i = pos; i < sound_table_size; i++) sound_table[i] = NULL;	    }	}    }  else previous_freed_sf = -1;  sound_table[pos] = (sound_file *)CALLOC(1, sizeof(sound_file));  sound_table[pos]->table_pos = pos;  sound_table[pos]->file_name = (char *)CALLOC(strlen(name) + 1, sizeof(char));  strcpy(sound_table[pos]->file_name, name);  return(sound_table[pos]);}int mus_sound_prune(void){  int i, pruned = 0;  for (i = 0; i < sound_table_size; i++)    if ((sound_table[i]) && 	(!(mus_file_probe(sound_table[i]->file_name))))      {	free_sound_file(sound_table[i]);	sound_table[i] = NULL;	pruned++;      }  return(pruned);}int mus_sound_forget(const char *name){  int i, len;  bool free_name = false;  char *short_name = NULL;  if (name == NULL) return(MUS_ERROR);  if (name[0] == '/')    {      len = strlen(name);      for (i = 0; i < len; i++)	if (name[i] == '/')	  short_name = (char *)(name + i + 1);    }  else    {      short_name = mus_expand_filename(name);      free_name = true;    }  previous_sf = NULL;  if (name)    for (i = 0; i < sound_table_size; i++)      if ((sound_table[i]) &&	  ((strcmp(name, sound_table[i]->file_name) == 0) ||	   ((short_name) && 	    (strcmp(short_name, sound_table[i]->file_name) == 0))))	{	  free_sound_file(sound_table[i]);	  sound_table[i] = NULL;	}  if (free_name) FREE(short_name);  return(MUS_NO_ERROR);}static sound_file *check_write_date(const char *name, sound_file *sf){  if (sf)    {      time_t date;      date = local_file_write_date(name);      if (date == sf->write_date)	return(sf);      else 	{	  if ((sf->header_type == MUS_RAW) && (mus_header_no_header(name)))	    {	      int chan;	      off_t data_size;	      /* sound has changed since we last read it, but it has no header, so	       * the only sensible thing to check is the new length (i.e. caller	       * has set other fields by hand)	       */

⌨️ 快捷键说明

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