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

📄 wavep.c

📁 希望能帮助大家学习C语言.
💻 C
字号:
/*____________________________________________________________________________
	
	MP3 Decoding Libraries
	Lanuguages used: C, C++, ASM

	As much as I would love to, I cannot take
	full credit for writing this code.
	Xing Technology	made MPEG-Layer 3 for us
	to decode. =)
	- ULTiMaTuM -

	MPEG-Layer 3 (MP3)
	Copyright (C) 1995-1997 Xing Technology

  	NOTE: I only edited the bare-minimum. 90%
		of this is original decoding code...
		Hmm... that sounds strange =)

____________________________________________________________________________*/

/*---- wavep.c --------------------------------------------

WAVE FILE HEADER ROUTINES
with conditional pcm conversion to MS wave format
portable version

-----------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include "port.h"

typedef struct
{
   unsigned char riff[4];
   unsigned char size[4];
   unsigned char wave[4];
   unsigned char fmt[4];
   unsigned char fmtsize[4];
   unsigned char tag[2];
   unsigned char nChannels[2];
   unsigned char nSamplesPerSec[4];
   unsigned char nAvgBytesPerSec[4];
   unsigned char nBlockAlign[2];
   unsigned char nBitsPerSample[2];
   unsigned char data[4];
   unsigned char pcm_bytes[4];
}
BYTE_WAVE;

static BYTE_WAVE wave =
{
   "RIFF",
   {(sizeof(BYTE_WAVE) - 8), 0, 0, 0},
   "WAVE",
   "fmt ",
   {16, 0, 0, 0},
   {1, 0},
   {1, 0},
   {34, 86, 0, 0},		/* 86 * 256 + 34 = 22050 */
   {172, 68, 0, 0},		/* 172 * 256 + 68 = 44100 */
   {2, 0},
   {16, 0},
   "data",
   {0, 0, 0, 0}
};

/*---------------------------------------------------------*/
static void set_wave(unsigned char w[], int n, long x)
{
   int i;

   for (i = 0; i < n; i++)
   {
      w[i] = (unsigned char) (x & 0xff);
      x >>= 8;
   }
}
/*---------------------------------------------------------*/
int write_pcm_header_wave(int handout,
			  long samprate, int channels, int bits, int type)
{
   int nwrite;

   if (type == 0)
      set_wave(wave.tag, sizeof(wave.tag), 1);
   else if (type == 10)
      set_wave(wave.tag, sizeof(wave.tag), 7);
   else
      return 0;

   set_wave(wave.size, sizeof(wave.size), sizeof(wave) - 8);
   set_wave(wave.nChannels, sizeof(wave.nChannels), channels);
   set_wave(wave.nSamplesPerSec, sizeof(wave.nSamplesPerSec), samprate);
   set_wave(wave.nAvgBytesPerSec, sizeof(wave.nAvgBytesPerSec),
	    (channels * samprate * bits + 7) / 8);
   set_wave(wave.nBlockAlign, sizeof(wave.nBlockAlign), (channels * bits + 7) / 8);
   set_wave(wave.nBitsPerSample, sizeof(wave.nBitsPerSample), bits);
   set_wave(wave.pcm_bytes, sizeof(wave.pcm_bytes), 0);

   nwrite = write(handout, &wave, sizeof(wave));
   if (nwrite != sizeof(wave))
      return 0;

   return 1;
}
/*-----------------------------------------------*/
int write_pcm_tailer_wave(int handout, unsigned long pcm_bytes)
{
   unsigned long pos;
   int nwrite;


   set_wave(wave.size, sizeof(wave.size), sizeof(wave) - 8 + pcm_bytes);
   set_wave(wave.pcm_bytes, sizeof(wave.pcm_bytes), pcm_bytes);


   pos = lseek(handout, 0L, 2);
/*-- save current position */
   lseek(handout, 0L, 0);
/*-- pos to header --*/
   nwrite = write(handout, &wave, sizeof(wave));
   lseek(handout, pos, 0);
/*-- restore pos --*/

   if (nwrite != sizeof(wave))
      return 0;
   return 1;
}
/*-----------------------------------------------*/
/*----------------------------------------------------------------
  pcm conversion to wave format

  This conversion code required for big endian machine, or,
  if sizeof(short) != 16 bits.
  Conversion routines may be used on any machine, but if
  not required, the do nothing macros in port.h can be used instead
  to reduce overhead.

-----------------------------------------------------------------*/
#ifndef LITTLE_SHORT16
#include "wcvt.c"
#endif
/*-----------------------------------------------*/
int cvt_to_wave_test()
{
/*-- test for valid compile ---*/

   return sizeof(short) - 2;


}
/*-----------------------------------------------*/

⌨️ 快捷键说明

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