📄 speexdec.c
字号:
/* Copyright (C) 2002-2003 Jean-Marc Valin File: speexdec.c Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <stdio.h>#if !defined WIN32 && !defined _WIN32#include <unistd.h>#include <getopt.h>#endif#ifndef HAVE_GETOPT_LONG#include "getopt_win.h"#endif#include <stdlib.h>#include <string.h>#include "speex.h"#include "ogg/ogg.h"#if defined WIN32 || defined _WIN32#include <windows.h>#include "getopt_win.h"#include "wave_out.h"/* We need the following two to set stdout to binary */#include <io.h>#include <fcntl.h>#endif#include <math.h>#ifdef __MINGW32__#include "wave_out.c"#endif#ifdef HAVE_SYS_SOUNDCARD_H#include <sys/soundcard.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#elif defined HAVE_SYS_AUDIOIO_H#include <sys/types.h>#include <fcntl.h>#include <sys/ioctl.h>#include <sys/audioio.h>#ifndef AUDIO_ENCODING_SLINEAR#define AUDIO_ENCODING_SLINEAR AUDIO_ENCODING_LINEAR /* Solaris */#endif#endif#include <string.h>#include "wav_io.h"#include "speex_header.h"#include "speex_stereo.h"#include "speex_callbacks.h"#include "misc.h"#define MAX_FRAME_SIZE 2000#define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \ ((buf[base+2]<<16)&0xff0000)| \ ((buf[base+1]<<8)&0xff00)| \ (buf[base]&0xff))static void print_comments(char *comments, int length){ char *c=comments; int len, i, nb_fields; char *end; if (length<8) { fprintf (stderr, "Invalid/corrupted comments\n"); return; } end = c+length; len=readint(c, 0); c+=4; if (c+len>end) { fprintf (stderr, "Invalid/corrupted comments\n"); return; } fwrite(c, 1, len, stderr); c+=len; fprintf (stderr, "\n"); if (c+4>end) { fprintf (stderr, "Invalid/corrupted comments\n"); return; } nb_fields=readint(c, 0); c+=4; for (i=0;i<nb_fields;i++) { if (c+4>end) { fprintf (stderr, "Invalid/corrupted comments\n"); return; } len=readint(c, 0); c+=4; if (c+len>end) { fprintf (stderr, "Invalid/corrupted comments\n"); return; } fwrite(c, 1, len, stderr); c+=len; fprintf (stderr, "\n"); }}FILE *out_file_open(char *outFile, int rate, int *channels){ FILE *fout=NULL; /*Open output file*/ if (strlen(outFile)==0) {#if defined HAVE_SYS_SOUNDCARD_H int audio_fd, format, stereo; audio_fd=open("/dev/dsp", O_WRONLY); if (audio_fd<0) { perror("Cannot open /dev/dsp"); exit(1); } format=AFMT_S16_NE; if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1) { perror("SNDCTL_DSP_SETFMT"); close(audio_fd); exit(1); } stereo=0; if (*channels==2) stereo=1; if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo)==-1) { perror("SNDCTL_DSP_STEREO"); close(audio_fd); exit(1); } if (stereo!=0) { if (*channels==1) fprintf (stderr, "Cannot set mono mode, will decode in stereo\n"); *channels=2; } if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate)==-1) { perror("SNDCTL_DSP_SPEED"); close(audio_fd); exit(1); } fout = fdopen(audio_fd, "w");#elif defined HAVE_SYS_AUDIOIO_H audio_info_t info; int audio_fd; audio_fd = open("/dev/audio", O_WRONLY); if (audio_fd<0) { perror("Cannot open /dev/audio"); exit(1); } AUDIO_INITINFO(&info);#ifdef AUMODE_PLAY /* NetBSD/OpenBSD */ info.mode = AUMODE_PLAY;#endif info.play.encoding = AUDIO_ENCODING_SLINEAR; info.play.precision = 16; info.play.sample_rate = rate; info.play.channels = *channels; if (ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) { perror ("AUDIO_SETINFO"); exit(1); } fout = fdopen(audio_fd, "w");#elif defined WIN32 || defined _WIN32 { unsigned int speex_channels = *channels; if (Set_WIN_Params (INVALID_FILEDESC, rate, SAMPLE_SIZE, speex_channels)) { fprintf (stderr, "Can't access %s\n", "WAVE OUT"); exit(1); } }#else fprintf (stderr, "No soundcard support\n"); exit(1);#endif } else { if (strcmp(outFile,"-")==0) {#if defined WIN32 || defined _WIN32 _setmode(_fileno(stdout), _O_BINARY);#endif fout=stdout; } else { fout = fopen(outFile, "wb"); if (!fout) { perror(outFile); exit(1); } if (strcmp(outFile+strlen(outFile)-4,".wav")==0 || strcmp(outFile+strlen(outFile)-4,".WAV")==0) write_wav_header(fout, rate, *channels, 0, 0); } } return fout;}void usage(){ printf ("Usage: speexdec [options] input_file.spx [output_file]\n"); printf ("\n"); printf ("Decodes a Speex file and produce a WAV file or raw file\n"); printf ("\n"); printf ("input_file can be:\n"); printf (" filename.spx regular Speex file\n"); printf (" - stdin\n"); printf ("\n"); printf ("output_file can be:\n"); printf (" filename.wav Wav file\n"); printf (" filename.* Raw PCM file (any extension other that .wav)\n"); printf (" - stdout\n"); printf (" (nothing) Will be played to soundcard\n"); printf ("\n"); printf ("Options:\n"); printf (" --enh Enable perceptual enhancement (default)\n"); printf (" --no-enh Disable perceptual enhancement\n"); printf (" --force-nb Force decoding in narrowband\n"); printf (" --force-wb Force decoding in wideband\n"); printf (" --force-uwb Force decoding in ultra-wideband\n"); printf (" --mono Force decoding in mono\n"); printf (" --stereo Force decoding in stereo\n"); printf (" --rate n Force decoding at sampling rate n Hz\n"); printf (" --packet-loss n Simulate n %% random packet loss\n"); printf (" -V Verbose mode (show bit-rate)\n"); printf (" -h, --help This help\n"); printf (" -v, --version Version information\n"); printf (" --pf Deprecated, use --enh instead\n"); printf (" --no-pf Deprecated, use --no-enh instead\n"); printf ("\n"); printf ("More information is available from the Speex site: http://www.speex.org\n"); printf ("\n"); printf ("Please report bugs to the mailing list `speex-dev@xiph.org'.\n");}void version(){ printf ("speexdec (Speex decoder) version " VERSION " (compiled " __DATE__ ")\n"); printf ("Copyright (C) 2002-2003 Jean-Marc Valin\n");}void version_short(){ printf ("speexdec version " VERSION "\n"); printf ("Copyright (C) 2002-2003 Jean-Marc Valin\n");}static void *process_header(ogg_packet *op, int enh_enabled, int *frame_size, int *rate, int *nframes, int forceMode, int *channels, SpeexStereoState *stereo, int *extra_headers, int quiet){ void *st; const SpeexMode *mode; SpeexHeader *header; int modeID; SpeexCallback callback; header = speex_packet_to_header((char*)op->packet, op->bytes); if (!header) { fprintf (stderr, "Cannot read header\n"); return NULL; } if (header->mode >= SPEEX_NB_MODES) { fprintf (stderr, "Mode number %d does not (yet/any longer) exist in this version\n", header->mode); return NULL; } modeID = header->mode; if (forceMode!=-1) modeID = forceMode; mode = speex_mode_list[modeID]; if (header->speex_version_id > 1) { fprintf (stderr, "This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id); return NULL; } if (mode->bitstream_version < header->mode_bitstream_version) { fprintf (stderr, "The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n"); return NULL; } if (mode->bitstream_version > header->mode_bitstream_version) { fprintf (stderr, "The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n"); return NULL; } st = speex_decoder_init(mode); if (!st) { fprintf (stderr, "Decoder initialization failed.\n"); return NULL; } speex_decoder_ctl(st, SPEEX_SET_ENH, &enh_enabled); speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, frame_size); if (!(*channels==1)) { callback.callback_id = SPEEX_INBAND_STEREO; callback.func = speex_std_stereo_request_handler; callback.data = stereo; speex_decoder_ctl(st, SPEEX_SET_HANDLER, &callback); } if (!*rate) *rate = header->rate; /* Adjust rate if --force-* options are used */ if (forceMode!=-1) { if (header->mode < forceMode) *rate <<= (forceMode - header->mode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -