📄 wav.c
字号:
/*** Copyright (C) 1999-2000 Erik de Castro Lopo <erikd@zip.com.au>** ** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU Lesser General Public License as published by** the Free Software Foundation; either version 2.1 of the License, or** (at your option) any later version.** ** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU Lesser General Public License for more details.** ** You should have received a copy of the GNU Lesser General Public License** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <stdio.h>#include <unistd.h>#include <string.h>#include <ctype.h>#include "sndfile.h"#include "config.h"#include "sfendian.h"#include "common.h"#include "pcm.h"#include "ulaw.h"#include "alaw.h"#include "wav.h"/*------------------------------------------------------------------------------** List of known WAV format tags*/enum{ WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Corporation */ WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM format */ WAVE_FORMAT_MS_ADPCM = 0x0002, /* Microsoft ADPCM */ WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* Micrososft 32 bit float format */ WAVE_FORMAT_IBM_CVSD = 0x0005, /* IBM Corporation */ WAVE_FORMAT_ALAW = 0x0006, /* Microsoft Corporation */ WAVE_FORMAT_MULAW = 0x0007, /* Microsoft Corporation */ WAVE_FORMAT_OKI_ADPCM = 0x0010, /* OKI */ WAVE_FORMAT_IMA_ADPCM = 0x0011, /* Intel Corporation */ WAVE_FORMAT_MEDIASPACE_ADPCM = 0x0012, /* Videologic */ WAVE_FORMAT_SIERRA_ADPCM = 0x0013, /* Sierra Semiconductor Corp */ WAVE_FORMAT_G723_ADPCM = 0x0014, /* Antex Electronics Corporation */ WAVE_FORMAT_DIGISTD = 0x0015, /* DSP Solutions, Inc. */ WAVE_FORMAT_DIGIFIX = 0x0016, /* DSP Solutions, Inc. */ WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic Corporation */ WAVE_FORMAT_MEDIAVISION_ADPCM = 0x0018, /* Media Vision, Inc. */ WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha Corporation of America */ WAVE_FORMAT_SONARC = 0x0021, /* Speech Compression */ WAVE_FORMAT_DSPGROUP_TRUESPEECH = 0x0022, /* DSP Group, Inc */ WAVE_FORMAT_ECHOSC1 = 0x0023, /* Echo Speech Corporation */ WAVE_FORMAT_AUDIOFILE_AF18 = 0x0024, /* Audiofile, Inc. */ WAVE_FORMAT_APTX = 0x0025, /* Audio Processing Technology */ WAVE_FORMAT_AUDIOFILE_AF10 = 0x0026, /* Audiofile, Inc. */ WAVE_FORMAT_DOLBY_AC2 = 0x0030, /* Dolby Laboratories */ WAVE_FORMAT_GSM610 = 0x0031, /* Microsoft Corporation */ WAVE_FORMAT_MSNAUDIO = 0x0032, /* Microsoft Corporation */ WAVE_FORMAT_ANTEX_ADPCME = 0x0033, /* Antex Electronics Corporation */ WAVE_FORMAT_CONTROL_RES_VQLPC = 0x0034, /* Control Resources Limited */ WAVE_FORMAT_DIGIREAL = 0x0035, /* DSP Solutions, Inc. */ WAVE_FORMAT_DIGIADPCM = 0x0036, /* DSP Solutions, Inc. */ WAVE_FORMAT_CONTROL_RES_CR10 = 0x0037, /* Control Resources Limited */ WAVE_FORMAT_NMS_VBXADPCM = 0x0038, /* Natural MicroSystems */ WAVE_FORMAT_ROCKWELL_ADPCM = 0x003B, /* Rockwell International */ WAVE_FORMAT_ROCKWELL_DIGITALK = 0x003C, /* Rockwell International */ WAVE_FORMAT_G721_ADPCM = 0x0040, /* Antex Electronics Corporation */ WAVE_FORMAT_MPEG = 0x0050, /* Microsoft Corporation */ WAVE_FORMAT_MPEGLAYER3 = 0x0055, /* MPEG 3 Layer 1 */ IBM_FORMAT_MULAW = 0x0101, /* IBM mu-law format */ IBM_FORMAT_ALAW = 0x0102, /* IBM a-law format */ IBM_FORMAT_ADPCM = 0x0103, /* IBM AVC Adaptive Differential PCM format */ WAVE_FORMAT_CREATIVE_ADPCM = 0x0200, /* Creative Labs, Inc */ WAVE_FORMAT_FM_TOWNS_SND = 0x0300, /* Fujitsu Corp. */ WAVE_FORMAT_OLIGSM = 0x1000, /* Ing C. Olivetti & C., S.p.A. */ WAVE_FORMAT_OLIADPCM = 0x1001, /* Ing C. Olivetti & C., S.p.A. */ WAVE_FORMAT_OLICELP = 0x1002, /* Ing C. Olivetti & C., S.p.A. */ WAVE_FORMAT_OLISBC = 0x1003, /* Ing C. Olivetti & C., S.p.A. */ WAVE_FORMAT_OLIOPR = 0x1004, /* Ing C. Olivetti & C., S.p.A. */ WAVE_FORMAT_EXTENSIBLE = 0xFFFE} ;/*------------------------------------------------------------------------------ * Macros to handle big/little endian issues. */#if (CPU_IS_LITTLE_ENDIAN == 1)# define MAKE_MARKER(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))#elif (CPU_IS_BIG_ENDIAN == 1)# define MAKE_MARKER(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))#else# error "Cannot determine endian-ness of processor."#endif#define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F')) #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E')) #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' ')) #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a')) #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' ')) #define LIST_MARKER (MAKE_MARKER ('L', 'I', 'S', 'T')) #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't')) #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l')) #define INFO_MARKER (MAKE_MARKER ('I', 'N', 'F', 'O')) #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't')) #define adtl_MARKER (MAKE_MARKER ('a', 'd', 't', 'l')) #define labl_MARKER (MAKE_MARKER ('l', 'a', 'b', 'l')) #define note_MARKER (MAKE_MARKER ('n', 'o', 't', 'e')) #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't')) #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l')) #define bext_MARKER (MAKE_MARKER ('b', 'e', 'x', 't')) #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T')) #define DISP_MARKER (MAKE_MARKER ('D', 'I', 'S', 'P')) /*------------------------------------------------------------------------------ * Private static functions. */static int read_fmt_chunk (SF_PRIVATE *psf, WAV_FMT *wav_fmt) ;static int write_header (SF_PRIVATE *psf, WAV_FMT *wav_fmt, unsigned int size, int do_fact) ;static const char* wav_format_str (int k) ;static void le2h_wav_fmt (WAV_FMT *fmt) ;static void h2le_wav_fmt (WAV_FMT *fmt) ;/*------------------------------------------------------------------------------ * Public functions. */int wav_open_read (SF_PRIVATE *psf){ WAV_FMT wav_fmt ; FACT_CHUNK fact_chunk ; unsigned int dword, marker, RIFFsize ; int parsestage = 0, error, format = 0 ; psf->sf.seekable = SF_TRUE ; while (1) { fread (&marker, sizeof (marker), 1, psf->file) ; switch (marker) { case RIFF_MARKER : if (parsestage != 0) return SFE_WAV_NO_RIFF ; fread (&dword, sizeof (dword), 1, psf->file) ; RIFFsize = LE2H_INT (dword) ; if (psf->filelength < RIFFsize + 2 * sizeof (dword)) { dword = psf->filelength - 2 * sizeof (dword); psf_sprintf (psf, "RIFF : %d (should be %d)\n", RIFFsize, dword) ; RIFFsize = dword ; } else psf_sprintf (psf, "RIFF : %d\n", RIFFsize) ; parsestage = 1 ; break ; case WAVE_MARKER : if (parsestage != 1) return SFE_WAV_NO_WAVE ; psf_sprintf (psf, "WAVE\n") ; parsestage = 2 ; break ; case fmt_MARKER : if (parsestage != 2) return SFE_WAV_NO_FMT ; if ((error = read_fmt_chunk (psf, &wav_fmt))) return error ; format = wav_fmt.format ; parsestage = 3 ; break ; case data_MARKER : if (parsestage < 3) return SFE_WAV_NO_DATA ; fread (&dword, sizeof (dword), 1, psf->file) ; psf->datalength = LE2H_INT (dword) ; psf->dataoffset = ftell (psf->file) ; if (psf->filelength < psf->dataoffset + psf->datalength) { psf_sprintf (psf, "data : %d (should be %d)\n", psf->datalength, psf->filelength - psf->dataoffset) ; psf->datalength = psf->filelength - psf->dataoffset ; } else psf_sprintf (psf, "data : %d\n", psf->datalength) ; if (format == WAVE_FORMAT_MS_ADPCM && psf->datalength % 2) { psf->datalength ++ ; psf_sprintf (psf, "*** Data length odd. Increasing it by 1.\n") ; } ; fseek (psf->file, psf->datalength, SEEK_CUR) ; dword = ftell (psf->file) ; if (dword != (off_t) (psf->dataoffset + psf->datalength)) psf_sprintf (psf, "*** fseek past end error ***\n", dword, psf->dataoffset + psf->datalength) ; break ; case fact_MARKER : fread (&dword, sizeof (dword), 1, psf->file) ; dword = LE2H_INT (dword) ; fread (&fact_chunk, sizeof (fact_chunk), 1, psf->file) ; if (dword > sizeof (fact_chunk)) fseek (psf->file, (int) (dword - sizeof (fact_chunk)), SEEK_CUR) ; fact_chunk.samples = LE2H_INT (fact_chunk.samples) ; psf_sprintf (psf, "%D : %d\n", marker, dword) ; psf_sprintf (psf, " samples : %d\n", fact_chunk.samples) ; break ; case cue_MARKER : case LIST_MARKER : case INFO_MARKER : case smpl_MARKER : case bext_MARKER : case MEXT_MARKER : case DISP_MARKER : fread (&dword, sizeof (dword), 1, psf->file) ; dword = LE2H_INT (dword) ; psf_sprintf (psf, "%D : %d\n", marker, dword) ; fseek (psf->file, (int) dword, SEEK_CUR) ; break ; default : if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF) && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF)) { fread (&dword, sizeof (dword), 1, psf->file) ; psf_sprintf (psf, "%D : %d (unknown marker)\n", marker, dword) ; fseek (psf->file, (int) dword, SEEK_CUR) ; break ; } ; if ((dword = ftell (psf->file)) & 0x03) { psf_sprintf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ; fseek (psf->file, -3, SEEK_CUR) ; break ; } ; psf_sprintf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ; break ; } ; /* switch (dword) */ if (ferror (psf->file)) { psf_sprintf (psf, "*** Error on file handle. ***\n", marker) ; clearerr (psf->file) ; break ; } ; if (ftell (psf->file) >= (int) (psf->filelength - (2 * sizeof (dword)))) break ; } ; /* while (1) */ if (! psf->dataoffset) return SFE_WAV_NO_DATA ; psf->current = 0 ; psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */ psf->sf.sections = 1 ; fseek (psf->file, psf->dataoffset, SEEK_SET) ; psf->close = (func_close) wav_close ; if (psf->blockwidth) { if (psf->filelength - psf->dataoffset < psf->datalength) psf->sf.samples = (psf->filelength - psf->dataoffset) / psf->blockwidth ; else psf->sf.samples = psf->datalength / psf->blockwidth ; } ; switch (format) { case WAVE_FORMAT_PCM : case WAVE_FORMAT_EXTENSIBLE : break ; case WAVE_FORMAT_MULAW : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ; psf->read_short = (func_short) ulaw_read_ulaw2s ; psf->read_int = (func_int) ulaw_read_ulaw2i ; psf->read_double = (func_double) ulaw_read_ulaw2d ; return 0 ; case WAVE_FORMAT_ALAW : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ; psf->read_short = (func_short) alaw_read_alaw2s ; psf->read_int = (func_int) alaw_read_alaw2i ; psf->read_double = (func_double) alaw_read_alaw2d ; return 0 ; case WAVE_FORMAT_MS_ADPCM : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ; if ((error = msadpcm_reader_init (psf, &wav_fmt))) return error ; psf->read_short = (func_short) msadpcm_read_s ; psf->read_int = (func_int) msadpcm_read_i ; psf->read_double = (func_double) msadpcm_read_d ; psf->seek_func = (func_seek) msadpcm_seek ; return 0 ; case WAVE_FORMAT_IMA_ADPCM : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ; if ((error = wav_ima_reader_init (psf, &wav_fmt))) return error ; psf->read_short = (func_short) ima_read_s ; psf->read_int = (func_int) ima_read_i ; psf->read_double = (func_double) ima_read_d ; psf->seek_func = (func_seek) ima_seek ; return 0 ; case WAVE_FORMAT_GSM610 : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ; if ((error = wav_gsm610_reader_init (psf, &wav_fmt))) return error ; psf->read_short = (func_short) wav_gsm610_read_s ; psf->read_int = (func_int) wav_gsm610_read_i ; psf->read_double = (func_double) wav_gsm610_read_d ; psf->seek_func = NULL ; /* Not seekable */ return 0 ; case WAVE_FORMAT_IEEE_FLOAT : psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; if (CAN_READ_WRITE_x86_IEEE) { psf->read_short = (func_short) pcm_read_f2s ; psf->read_int = (func_int) pcm_read_f2i ; psf->read_double = (func_double) pcm_read_f2d ; } else { psf->read_short = (func_short) wav_read_x86f2s ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -