📄 aiff.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"/*------------------------------------------------------------------------------ * 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 FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M')) #define AIFF_MARKER (MAKE_MARKER ('A', 'I', 'F', 'F')) #define AIFC_MARKER (MAKE_MARKER ('A', 'I', 'F', 'C')) #define COMM_MARKER (MAKE_MARKER ('C', 'O', 'M', 'M')) #define SSND_MARKER (MAKE_MARKER ('S', 'S', 'N', 'D')) #define MARK_MARKER (MAKE_MARKER ('M', 'A', 'R', 'K')) #define INST_MARKER (MAKE_MARKER ('I', 'N', 'S', 'T')) #define APPL_MARKER (MAKE_MARKER ('A', 'P', 'P', 'L')) #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' ')) #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E')) #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H')) #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O')) #define FVER_MARKER (MAKE_MARKER ('F', 'V', 'E', 'R')) #define NONE_MARKER (MAKE_MARKER ('N', 'O', 'N', 'E')) #define REAL_COMM_SIZE (2+4+2+10)/*------------------------------------------------------------------------------ * Typedefs for file chunks. */typedef struct{ short numChannels ; unsigned int numSampleFrames ; short sampleSize ; unsigned char sampleRate [10] ;} COMM_CHUNK ; typedef struct{ unsigned int offset ; unsigned int blocksize ;} SSND_CHUNK ; typedef struct { short playMode; int beginLoop; int endLoop;} INST_CHUNK ;/*------------------------------------------------------------------------------ * Private static functions. */static int aiff_close (SF_PRIVATE *psf) ;static int tenbytefloat2int (unsigned char *bytes) ;static void uint2tenbytefloat (unsigned int num, unsigned char *bytes) ;static void endswap_comm_fmt (COMM_CHUNK *comm){ comm->numChannels = ENDSWAP_SHORT (comm->numChannels) ; comm->numSampleFrames = ENDSWAP_INT (comm->numSampleFrames) ; comm->sampleSize = ENDSWAP_SHORT (comm->sampleSize) ;} /* endswap_comm_fmt */static void endswap_ssnd_fmt (SSND_CHUNK *ssnd){ ssnd->offset = ENDSWAP_INT (ssnd->offset) ; ssnd->blocksize = ENDSWAP_INT (ssnd->blocksize) ;} /* endswap_ssnd_fmt *//*------------------------------------------------------------------------------** Public functions.*/int aiff_open_read (SF_PRIVATE *psf){ COMM_CHUNK comm_fmt ; SSND_CHUNK ssnd_fmt ; int marker, dword ; long FORMsize, commsize, SSNDsize ; int filetype, parsestage = 0, done = 0 ; while (! done) { fread (&marker, sizeof (marker), 1, psf->file) ; switch (marker) { case FORM_MARKER : if (parsestage != 0) return SFE_AIFF_NO_FORM ; fread (&dword, sizeof (dword), 1, psf->file) ; FORMsize = BE2H_INT (dword) ; if (FORMsize != psf->filelength - 2 * sizeof (dword)) { dword = psf->filelength - 2 * sizeof (dword); psf_sprintf (psf, "FORM : %d (should be %d)\n", FORMsize, dword) ; FORMsize = dword ; } else psf_sprintf (psf, "FORM : %d\n", FORMsize) ; parsestage = 1 ; break ; case AIFC_MARKER : case AIFF_MARKER : if (parsestage != 1) return SFE_AIFF_NO_FORM ; filetype = marker ; psf_sprintf (psf, " %D\n", marker) ; parsestage = 2 ; break ; case COMM_MARKER : if (parsestage != 2) return SFE_AIFF_NO_FORM ; fread (&dword, sizeof (dword), 1, psf->file) ; commsize = BE2H_INT (dword) ; /* The COMM chunk has an int aligned to a word boundary. Some procesors ** are not able to deal with this (ie bus fault) so we have to take ** special care. */ fread (&(comm_fmt.numChannels), sizeof (comm_fmt.numChannels), 1, psf->file) ; fread (&(comm_fmt.numSampleFrames), sizeof (comm_fmt.numSampleFrames), 1, psf->file) ; fread (&(comm_fmt.sampleSize), sizeof (comm_fmt.sampleSize), 1, psf->file) ; fread (&(comm_fmt.sampleRate), sizeof (comm_fmt.sampleRate), 1, psf->file) ; if (CPU_IS_LITTLE_ENDIAN) endswap_comm_fmt (&comm_fmt) ; psf->sf.samplerate = tenbytefloat2int (comm_fmt.sampleRate) ; psf->sf.samples = comm_fmt.numSampleFrames ; psf->sf.channels = comm_fmt.numChannels ; psf->sf.pcmbitwidth = comm_fmt.sampleSize ; psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM); psf->sf.sections = 1 ; psf_sprintf (psf, " COMM : %d\n", commsize) ; psf_sprintf (psf, " Sample Rate : %d\n", psf->sf.samplerate) ; psf_sprintf (psf, " Samples : %d\n", comm_fmt.numSampleFrames) ; psf_sprintf (psf, " Channels : %d\n", comm_fmt.numChannels) ; psf_sprintf (psf, " Sample Size : %d\n", comm_fmt.sampleSize) ; if (commsize > REAL_COMM_SIZE) { fread (&dword, sizeof (dword), 1, psf->file) ; if (dword != NONE_MARKER) { psf_sprintf (psf, "AIFC : Unimplemented format : %D\n", dword) ; return SFE_UNIMPLEMENTED ; } ; fseek (psf->file, commsize - (long) (sizeof (dword) + REAL_COMM_SIZE), SEEK_CUR) ; } ; parsestage = 3 ; break ; case SSND_MARKER : if (parsestage != 3) return SFE_AIFF_NO_SSND ; fread (&dword, sizeof (dword), 1, psf->file) ; SSNDsize = BE2H_INT (dword) ; fread (&ssnd_fmt, sizeof (SSND_CHUNK), 1, psf->file) ; if (CPU_IS_LITTLE_ENDIAN) endswap_ssnd_fmt (&ssnd_fmt) ; psf->dataoffset = ftell (psf->file) ; psf->datalength = psf->filelength - psf->dataoffset ; if (SSNDsize != psf->datalength + sizeof (SSND_CHUNK)) psf_sprintf (psf, " SSND : %d (should be %d)\n", SSNDsize, psf->datalength + sizeof (SSND_CHUNK)) ; else psf_sprintf (psf, " SSND : %d\n", SSNDsize) ; psf_sprintf (psf, " Offset : %d\n", ssnd_fmt.offset) ; psf_sprintf (psf, " Block Size : %d\n", ssnd_fmt.blocksize) ; 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) ; parsestage = 4 ; break ; case NAME_MARKER : case AUTH_MARKER : case ANNO_MARKER : case c_MARKER : case FVER_MARKER : if (parsestage < 2) return SFE_AIFF_NO_FORM ; fread (&dword, sizeof (dword), 1, psf->file) ; dword = BE2H_INT (dword) ; psf_sprintf (psf, " %D : %d\n", marker, dword) ; fseek (psf->file, dword, SEEK_CUR) ; break ; case MARK_MARKER : case INST_MARKER : case APPL_MARKER : if (parsestage < 2) return SFE_AIFF_NO_FORM ; fread (&dword, sizeof (dword), 1, psf->file) ; dword = BE2H_INT (dword) ; psf_sprintf (psf, " %D : %d\n", marker, dword) ; fseek (psf->file, 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, 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) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -