📄 xi.c
字号:
/*** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>**** 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 "config.h"#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <ctype.h>#include "sndfile.h"#include "sfendian.h"#include "common.h"#include "float_cast.h"#define MAX_XI_SAMPLES 16/*------------------------------------------------------------------------------** Private static functions and tyepdefs.*/typedef struct{ /* Warning, this filename is NOT nul terminated. */ char filename [22] ; char software [20] ; char sample_name [22] ; int loop_begin, loop_end ; int sample_flags ; /* Data for encoder and decoder. */ short last_16 ;} XI_PRIVATE ;static int xi_close (SF_PRIVATE *psf) ;static int xi_write_header (SF_PRIVATE *psf, int calc_length) ;static int xi_read_header (SF_PRIVATE *psf) ;static int dpcm_init (SF_PRIVATE *psf) ;static sf_count_t dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;/*------------------------------------------------------------------------------** Public function.*/intxi_open (SF_PRIVATE *psf){ XI_PRIVATE *pxi ; int subformat, error = 0 ; if (psf->is_pipe) return SFE_XI_NO_PIPE ; if (psf->fdata) pxi = psf->fdata ; else if ((pxi = calloc (1, sizeof (XI_PRIVATE))) == NULL) return SFE_MALLOC_FAILED ; psf->fdata = pxi ; if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0)) { if ((error = xi_read_header (psf))) return error ; } ; subformat = psf->sf.format & SF_FORMAT_SUBMASK ; if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR) { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI) return SFE_BAD_OPEN_FORMAT ; psf->endian = SF_ENDIAN_LITTLE ; psf->sf.channels = 1 ; /* Always mono */ psf->sf.samplerate = 44100 ; /* Always */ /* Set up default instrument and software name. */ memcpy (pxi->filename, "Default Name ", sizeof (pxi->filename)) ; memcpy (pxi->software, PACKAGE "-" VERSION " ", sizeof (pxi->software)) ; memset (pxi->sample_name, 0, sizeof (pxi->sample_name)) ; LSF_SNPRINTF (pxi->sample_name, sizeof (pxi->sample_name), "%s", "Sample #1") ; pxi->sample_flags = (subformat == SF_FORMAT_DPCM_16) ? 16 : 0 ; if (xi_write_header (psf, SF_FALSE)) return psf->error ; psf->write_header = xi_write_header ; } ; psf->close = xi_close ; psf->seek = dpcm_seek ; psf->sf.seekable = SF_FALSE ; psf->blockwidth = psf->bytewidth * psf->sf.channels ; switch (subformat) { case SF_FORMAT_DPCM_8 : /* 8-bit differential PCM. */ case SF_FORMAT_DPCM_16 : /* 16-bit differential PCM. */ error = dpcm_init (psf) ; break ; default : break ; } ; return error ;} /* xi_open *//*------------------------------------------------------------------------------*/static intxi_close (SF_PRIVATE *psf){ psf = psf ; return 0 ;} /* xi_close *//*==============================================================================*/static sf_count_t dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;static sf_count_t dpcm_write_s2dsc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;static sf_count_t dpcm_write_i2dsc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;static sf_count_t dpcm_write_f2dsc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;static sf_count_t dpcm_write_d2dsc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;static sf_count_t dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;static sf_count_t dpcm_write_s2dles (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;static sf_count_t dpcm_write_i2dles (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;static sf_count_t dpcm_write_f2dles (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;static sf_count_t dpcm_write_d2dles (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;static intdpcm_init (SF_PRIVATE *psf){ if (psf->bytewidth == 0 || psf->sf.channels == 0) return SFE_INTERNAL ; psf->blockwidth = psf->bytewidth * psf->sf.channels ; if (psf->mode == SFM_READ || psf->mode == SFM_RDWR) { switch (psf->bytewidth) { case 1 : psf->read_short = dpcm_read_dsc2s ; psf->read_int = dpcm_read_dsc2i ; psf->read_float = dpcm_read_dsc2f ; psf->read_double = dpcm_read_dsc2d ; break ; case 2 : psf->read_short = dpcm_read_dles2s ; psf->read_int = dpcm_read_dles2i ; psf->read_float = dpcm_read_dles2f ; psf->read_double = dpcm_read_dles2d ; break ; default : psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ; return SFE_UNIMPLEMENTED ; } ; } ; if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR) { switch (psf->bytewidth) { case 1 : psf->write_short = dpcm_write_s2dsc ; psf->write_int = dpcm_write_i2dsc ; psf->write_float = dpcm_write_f2dsc ; psf->write_double = dpcm_write_d2dsc ; break ; case 2 : psf->write_short = dpcm_write_s2dles ; psf->write_int = dpcm_write_i2dles ; psf->write_float = dpcm_write_f2dles ; psf->write_double = dpcm_write_d2dles ; break ; default : psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ; return SFE_UNIMPLEMENTED ; } ; } ; psf->filelength = psf_get_filelen (psf) ; psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ; psf->sf.frames = psf->datalength / psf->blockwidth ; return 0 ;} /* dpcm_init *//*==============================================================================*/static sf_count_tdpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset){ XI_PRIVATE *pxi ; int total, bufferlen, len ; if ((pxi = psf->fdata) == NULL) return SFE_INTERNAL ; if (psf->datalength < 0 || psf->dataoffset < 0) { psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; if (offset == 0) { psf_fseek (psf, psf->dataoffset, SEEK_SET) ; pxi->last_16 = 0 ; return 0 ; } ; if (offset < 0 || offset > psf->sf.frames) { psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; if (mode != SFM_READ) { /* What to do about write??? */ psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; psf_fseek (psf, psf->dataoffset, SEEK_SET) ; if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DPCM_16) { total = offset ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (total > 0) { len = (total > bufferlen) ? bufferlen : total ; total -= dpcm_read_dles2s (psf, psf->u.sbuf, len) ; } ; } else { total = offset ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (total > 0) { len = (total > bufferlen) ? bufferlen : total ; total -= dpcm_read_dsc2s (psf, psf->u.sbuf, len) ; } ; } ; return offset ;} /* dpcm_seek */static intxi_write_header (SF_PRIVATE *psf, int calc_length){ XI_PRIVATE *pxi ; sf_count_t current ; const char *string ; if ((pxi = psf->fdata) == NULL) return SFE_INTERNAL ; calc_length = calc_length ; /* Avoid a compiler warning. */ current = psf_ftell (psf) ; /* Reset the current header length to zero. */ psf->header [0] = 0 ; psf->headindex = 0 ; psf_fseek (psf, 0, SEEK_SET) ; string = "Extended Instrument: " ; psf_binheader_writef (psf, "b", string, strlen (string)) ; psf_binheader_writef (psf, "b1", pxi->filename, sizeof (pxi->filename), 0x1A) ; /* Write software version and two byte XI version. */ psf_binheader_writef (psf, "eb2", pxi->software, sizeof (pxi->software), (1 << 8) + 2) ; /* ** Jump note numbers (96), volume envelope (48), pan envelope (48), ** volume points (1), pan points (1) */ psf_binheader_writef (psf, "z", (size_t) (96 + 48 + 48 + 1 + 1)) ; /* Jump volume loop (3 bytes), pan loop (3), envelope flags (3), vibrato (3) ** fade out (2), 22 unknown bytes, and then write sample_count (2 bytes). */ psf_binheader_writef (psf, "ez2z2", (size_t) (4 * 3), 0x1234, (size_t) 22, 1) ;psf->sf.frames = 12 ;pxi->loop_begin = 0 ;pxi->loop_end = 0 ; psf_binheader_writef (psf, "et844", psf->sf.frames, pxi->loop_begin, pxi->loop_end) ; /* volume, fine tune, flags, pan, note, namelen */ psf_binheader_writef (psf, "111111", 128, 0, pxi->sample_flags, 128, 0, strlen (pxi->sample_name)) ; psf_binheader_writef (psf, "b", pxi->sample_name, sizeof (pxi->sample_name)) ; /* Header construction complete so write it out. */ psf_fwrite (psf->header, psf->headindex, 1, psf) ; if (psf->error) return psf->error ; psf->dataoffset = psf->headindex ; if (current > 0) psf_fseek (psf, current, SEEK_SET) ; return psf->error ;} /* xi_write_header */static intxi_read_header (SF_PRIVATE *psf){ char buffer [64], name [32] ; short version, fade_out, sample_count ; int k, loop_begin, loop_end ; int sample_sizes [MAX_XI_SAMPLES] ; psf_binheader_readf (psf, "pb", 0, buffer, 21) ; memset (sample_sizes, 0, sizeof (sample_sizes)) ; buffer [20] = 0 ; if (strcmp (buffer, "Extended Instrument:") != 0) return SFE_XI_BAD_HEADER ; memset (buffer, 0, sizeof (buffer)) ; psf_binheader_readf (psf, "b", buffer, 23) ; if (buffer [22] != 0x1A) return SFE_XI_BAD_HEADER ; buffer [22] = 0 ; psf_log_printf (psf, "Extended Instrument : %s\n", buffer) ; psf_binheader_readf (psf, "be2", buffer, 20, &version) ; buffer [19] = 0 ; psf_log_printf (psf, "Software : %s\nVersion : %d.%02d\n", buffer, version / 256, version % 256) ; /* Jump note numbers (96), volume envelope (48), pan envelope (48), ** volume points (1), pan points (1) */ psf_binheader_readf (psf, "j", 96 + 48 + 48 + 1 + 1) ; psf_binheader_readf (psf, "b", buffer, 12) ; psf_log_printf (psf, "Volume Loop\n sustain : %u\n begin : %u\n end : %u\n", buffer [0], buffer [1], buffer [2]) ; psf_log_printf (psf, "Pan Loop\n sustain : %u\n begin : %u\n end : %u\n", buffer [3], buffer [4], buffer [5]) ; psf_log_printf (psf, "Envelope Flags\n volume : 0x%X\n pan : 0x%X\n", buffer [6] & 0xFF, buffer [7] & 0xFF) ; psf_log_printf (psf, "Vibrato\n type : %u\n sweep : %u\n depth : %u\n rate : %u\n", buffer [8], buffer [9], buffer [10], buffer [11]) ; /* ** Read fade_out then jump reserved (2 bytes) and ???? (20 bytes) and ** sample_count. */ psf_binheader_readf (psf, "e2j2", &fade_out, 2 + 20, &sample_count) ; psf_log_printf (psf, "Fade out : %d\n", fade_out) ; /* XI file can contain up to 16 samples. */ if (sample_count > MAX_XI_SAMPLES) return SFE_XI_EXCESS_SAMPLES ; /* Log all data for each sample. */ for (k = 0 ; k < sample_count ; k++) { psf_binheader_readf (psf, "e444", &(sample_sizes [k]), &loop_begin, &loop_end) ; /* Read 5 know bytes, 1 unknown byte and 22 name bytes. */ psf_binheader_readf (psf, "bb", buffer, 6, name, 22) ; name [21] = 0 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -