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

📄 mp3.c

📁 MP3解压程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
{   MYINPUT *mi;
	DWORD dwFormat;
	if (!hInput) return 0;
	mi=(MYINPUT *)GlobalLock(hInput);
		
	dwFormat=(DWORD)mi->dwFormat;

    GlobalUnlock(hInput);
    return dwFormat;
}


__declspec(dllexport) DWORD FAR PASCAL FilterOptionsString(HANDLE hInput, LPSTR szString)
{   MYINPUT *mi;
	DWORD dwFormat;
	
	if (!hInput) return 0;
	mi=(MYINPUT *)GlobalLock(hInput);
    dwFormat=(DWORD)mi->dwFormat;

	wsprintf(szString,"%s Layer %d\r\n(%s)",mi->mpi.szVersion,mi->mpi.iLayer,mi->mpi.szBitrate);
	
    GlobalUnlock(hInput);
    return dwFormat;
}


__declspec(dllexport) DWORD FAR PASCAL FilterCanWriteSpecial(HANDLE hOutput, DWORD idType)
{
 return 0L;
}

__declspec(dllexport) DWORD FAR PASCAL FilterCanReadSpecial(HANDLE hInput, DWORD idType)
{
 return 0L;
}
					 
__declspec(dllexport) DWORD FAR PASCAL FilterWriteSpecial(HANDLE hOutput, DWORD idType, unsigned char far *buf, DWORD dwSize)
{
 return 0L;
}

__declspec(dllexport) DWORD FAR PASCAL FilterReadSpecial(HANDLE hOutput, DWORD idType, unsigned char far *buf, DWORD dwSize)
{
 return 0L;
}



__declspec(dllexport) HANDLE FAR PASCAL OpenFilterOutput(LPSTR lpstrFilename,long lSamprate,WORD wBitsPerSample,WORD wChannels,long lSize,long far *lpChunkSize,DWORD dwOptions)
{   HANDLE hOutput;
	short nFile;
	nFile=_lcreat(lpstrFilename,0);
    if (nFile==-1) return NULL;
    hOutput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,sizeof(MYOUTPUT));
    if (hOutput)
    {   MYOUTPUT *mo;
		mo=(MYOUTPUT *)GlobalLock(hOutput);
		mo->nFile=nFile;
		mo->lSize=lSize;
		GlobalUnlock(hOutput);
    }
    *lpChunkSize=16384;
    return hOutput;
}

__declspec(dllexport) void FAR PASCAL CloseFilterOutput(HANDLE hOutput)
{   if (hOutput)
	{       MYOUTPUT *mo;
		mo=(MYOUTPUT *)GlobalLock(hOutput);
		if (mo->nFile!=-1)
		{       _lclose(mo->nFile);
			mo->nFile=-1;
		}

		GlobalUnlock(hOutput);
		GlobalFree(hOutput);
	}
}

__declspec(dllexport) DWORD FAR PASCAL WriteFilterOutput(HANDLE hOutput, unsigned char far *buf, long lBytes)
{       DWORD written=0L;
	if (hOutput)
	{       MYOUTPUT *mo;
		mo=(MYOUTPUT *)GlobalLock(hOutput);
		written=(DWORD)_lwrite(mo->nFile,buf,(UINT)lBytes);
		GlobalUnlock(hOutput);
	}
	return written; 
}

DWORD valJ=524309;			
DWORD valI=12345; // reset to time at startup

double fRandom(void) // 0 to 1
{	DWORDLONG dwl;	
	dwl=UInt32x32To64(valJ,valI);
	dwl++;
	valI=(DWORD)(dwl&0x1FFFFF);	
	return (double)valI/2097152.0;
}

// This is the 32-bit to 16-bit converter from Cool Edit, but with only the code
// for the straight triangular dithering.  For higher quality, read in the file
// as 32-bit, then use Cool Edit Pro's dithering options for going to 16-bit.

void HfCvt32To16InMem(short *piDest, float *pfSource, long dwSourceSamples, BOOL bStereo, double fDitherAmount)
{	long t;
	double fFilteredError=0;
	double fSourceBeforeNoise;
	double qq=1.0;
	double fMultiplier=0.5;
	double fNoise;
	double fSource;
	double fSqrtDitherAmount;
	long quant;
	static double fError=0;
	static double fError1=0;
	static double fError2=0;
	static double fError3=0;
	static double fError4=0;
	static double fError5=0;
	static double fErrorR=0;
	static double fErrorR1=0;
	static double fErrorR2=0;
	static double fErrorR3=0;
	static double fErrorR4=0;
	static double fErrorR5=0;
		  		 
	{   
		if (fDitherAmount>0)
			fSqrtDitherAmount=sqrt(fDitherAmount);
		else
			fSqrtDitherAmount=0;
		
		if (bStereo)
	 	{	
			for (t=0;t<dwSourceSamples;t+=2)			
			{	
				fFilteredError=0;
				
				fError4=fError3;
				fError3=fError2;
				fError2=fError1;
				fError1=fFilteredError;
				
				fSource=*pfSource;
				pfSource++;
				
				// Don't Shift out 8 bits
				fSource=fSource-fFilteredError; //*fDitherMultiplier;					
				
				fNoise=fDitherAmount*(fRandom()+fRandom()-1.0);				
				
				fSourceBeforeNoise=fSource;

				// Add Noise (v)
				fSource+=fNoise; //*fDitherMultiplier; //*2.0;
				
				// Quantization
				quant=(long)(fSource+500000+0.5)-500000; // floor function, values -1000 to +1000 OK

				// Error for input to H(z)
				fError=(double)quant-fSourceBeforeNoise;

				if (quant>=-32768)
				{	if (quant<=32767)
						*piDest=(short)quant;
					else
						*piDest=32767;
				}
				else
					*piDest=-32768;
				piDest++;

				fFilteredError=0;
				
				fErrorR4=fErrorR3;
				fErrorR3=fErrorR2;
				fErrorR2=fErrorR1;
				fErrorR1=fFilteredError;
				
				fSource=*pfSource;
				pfSource++;

				// Don't Shift out 8 bits
				fSource=fSource-fFilteredError; //*fDitherMultiplier;					
				
				fNoise=fDitherAmount*(fRandom()+fRandom()-1.0);
				
				fSourceBeforeNoise=fSource;

				// Add Noise (v)
				fSource+=fNoise; //*fDitherMultiplier; //*2.0;
				
				// Quantization
				quant=(long)(fSource+500000+0.5)-500000; // floor function, values -1000 to +1000 OK

				// Error for input to H(z)
				fErrorR=(double)quant-fSourceBeforeNoise;

				if (quant>=-32768)
				{	if (quant<=32767)
						*piDest=(short)quant;
					else
						*piDest=32767;
				}
				else
					*piDest=-32768;
				piDest++;
			}		
	 	}       
	 	else
	 	{   
			for (t=0;t<dwSourceSamples;t++)
			{	fFilteredError=0;
							
				fError5=fError4;
				fError4=fError3;
				fError3=fError2;
				fError2=fError1;
				fError1=fFilteredError;

				fSource=*pfSource;
				pfSource++;
				
				// Don't Shift out 8 bits
				fSource=fSource-fFilteredError; //*fDitherMultiplier;					
				// Triangular Dither Noise
				
				fNoise=fDitherAmount*(fRandom()+fRandom()-1.0);					
				
				fSourceBeforeNoise=fSource;

				// Add Noise (v)
				fSource+=fNoise; //*fDitherMultiplier; //*2.0;
				
				// Quantization
				quant=(long)(fSource+500000+0.5)-500000; // floor function, values -1000 to +1000 OK

				// Error for input to H(z)
				fError=(double)quant-fSourceBeforeNoise;

				if (quant>=-32768)
				{	if (quant<=32767)
						*piDest=(short)quant;
					else
						*piDest=32767;
				}
				else
					*piDest=-32768;
				piDest++;

			}	
		} 
	}
}


__declspec(dllexport) void FAR PASCAL GetSuggestedSampleType(long far *lSugSamprate,WORD far *wSugBitsPerSample, WORD far *wSugChannels)
{   // return 0 for don't care
 	*lSugSamprate=0;
	{	int n=GetPrivateProfileInt("MPEG Filter","InputFormat",0,"coolrw.ini");
		*wSugBitsPerSample=HIWORD(n)&0x00FF;
		if ((*wSugBitsPerSample!=16) && (*wSugBitsPerSample!=32))
			*wSugBitsPerSample=16;
	}
 	*wSugChannels=0;
}


// return handle that will be passed in to close, and write routines
__declspec(dllexport) HANDLE FAR PASCAL OpenFilterInput( LPSTR lpstrFilename,
											long *lSamprate,
											WORD *wBitsPerSample,
											WORD *wChannels,
											HWND hWnd,
											long *lChunkSize)
{	HANDLE hInput;
	short source; 
	long fullsize;
	DWORD dwOptions;
	
	dwOptions=0;
	valI=GetTickCount()%50000;

	source=_lopen(lpstrFilename,OF_READ);
    if (source==-1) return NULL;     
    
    fullsize=_llseek(source,0L,2);
	_lclose(source);
	
    hInput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE|GMEM_ZEROINIT,sizeof(MYINPUT));
    if (hInput)
    {   MYINPUT *mi;
		
		mi=(MYINPUT *)GlobalLock(hInput);

		mi->wBitsPerSample=32; 
		mi->fAttenuation=0; 
		mi->dwFormat=0;			// Force asking of format, and default to 32-bit No attenuation
		mi->fScaleBy=32768.0f;
		mi->bDither=FALSE;
	
		// return 0 for cool to inquire user for sample rates (we aren't doing that though)
		*lSamprate=44100;
		*wBitsPerSample=32;
		*wChannels=2;
		*lChunkSize=16384;

		mi->bOpenedMPEG=MPEG_Open(lpstrFilename);
		if (!mi->bOpenedMPEG)
		{	GlobalUnlock(hInput);
     		GlobalFree(hInput);
     		return NULL;
		}
		
		MPEG_GetInfo(&mi->mpi);

		*lSamprate=mi->mpi.iSamplerate;
		*wChannels=mi->mpi.iChannels;

		{	int n=GetPrivateProfileInt("MPEG Filter","InputFormat",0,"coolrw.ini");
			*wBitsPerSample=HIWORD(n)&0x00FF;
			if ((*wBitsPerSample!=16) && (*wBitsPerSample!=32))
				*wBitsPerSample=16;
		}

		mi->wBitsPerSample=*wBitsPerSample;
		
		mi->lSize=(int)(mi->mpi.fLengthInMilliseconds*(*lSamprate)/1000.0)*(*wChannels)*(*wBitsPerSample/8);

		GlobalUnlock(hInput);
    }
    
    return hInput;
}

__declspec(dllexport) void FAR PASCAL CloseFilterInput(HANDLE hInput)
{   if (hInput)  
	{       MYINPUT *mi;
		mi=(MYINPUT *)GlobalLock(hInput);
		
		if (mi->bOpenedMPEG)
			MPEG_Close();
    	
		if (mi->h32bit)
		{	GlobalUnlock(mi->h32bit);
			GlobalFree(mi->h32bit);
		}

		GlobalUnlock(hInput);
		GlobalFree(hInput);
	}
}



__declspec(dllexport) DWORD FAR PASCAL ReadFilterInput(HANDLE hInput, unsigned char far *bufout, long lBytes)
{       DWORD read=0L;
	if (hInput)
	{   MYINPUT *mi;
		float fScaleBy;

		mi=(MYINPUT *)GlobalLock(hInput);
		fScaleBy=mi->fScaleBy;

		if (mi->wBitsPerSample==32)
		{	// just scale
			read=(DWORD)MPEG_Read(bufout);
		
			{	int iNum=read/4;
				float * pBuf=(float *)bufout;
				while (iNum--)
				{	*pBuf++*=fScaleBy;					
				}
			}
			
		}
		else if (mi->wBitsPerSample==16)
		{	// Convert to 16-bit with scaling and dithering
			int iNum;
			float * pfBuf;
			short * piBuf;

			pfBuf=(float *)mi->pi32bit;
			piBuf=(short *)bufout;

			read=(DWORD)MPEG_Read((BYTE *)mi->pi32bit);						
			iNum=read/4;
			// Dither it to 16-bit

			if (mi->bDither)
			{	// Scale it first
				{	int iNum=read/4;
					float * pBuf=pfBuf;
					while (iNum--)
					{	*pBuf++*=fScaleBy;					
					}
				}

				// Dither and Clip
				HfCvt32To16InMem(piBuf, pfBuf, iNum,
					(mi->mpi.iChannels==2)?TRUE:FALSE, 1.0);
			}
			else 
			{	// Scale, Truncate, and Clip
				float f;
				while (iNum--)
				{	f=*pfBuf++*fScaleBy;
					if (f>=(float)(-32767.0))
					{	if (f<=(float)32767.0)
							*piBuf++=(short)f;
						else
							*piBuf++=32767;
					}
					else
						*piBuf++=-32767;
				}
			}
			read/=2;
		}
		GlobalUnlock(hInput);
	}
	return read;
}

⌨️ 快捷键说明

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