📄 wavex01.cu
字号:
/*
* Copyright 1993-2007 NVIDIA Corporation. All rights reserved.
*/
/* WaveX01 project which demonstrates the basics on how to setup a project
* example application.
* HOST CODE.
*/
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
// includes, project
#include <cutil.h>
#include <cuda.h>
// includes, kernels
#include <WaveX01_kernel.cu>
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void ReadFIRCoeff(void); //Read desired Frequency
void ReadConfig(char * ConfigFile); //Read Configuration
int ProcessWav(char *infilename, char *outfilename); //Prototype Routine
char InputFile[256];
char ConfigFile[256];
char OutputFile[256];
//parameters
int DivideCount; //4 = 4Way FIR Divider
int OutputFormat_Bit; //16 or 24
int SampleRate; //44100,
bool StreamOut; //true:output to Xylo
bool FileOut; //true:output to File
#define DEFTAPS 8192
struct FIRParams{
int DelayLength; //Channel Delay Sample Length
int chPointer[2]; //Channel Processing Pointer for Wave Data (initialized negative for delay)
bool Enabled; //Channel Used
int Offset; //Offset in dB
double dOffset; //Offset in double, to calculate
char CoeffFile[256]; //Coeff File Name
float FIRCoeff[DEFTAPS]; //Coeff Value
};
struct FIRParams FIR[4];
const unsigned int CUDABLOCKS = 16;
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
//Check Arguments Count
if (argc != 4)
{
printf("requires 3 arguments.\n");
printf("WaveX01 inWaveFile ConfigFile OutWaveXFile\n");
printf("WaveX01 \"C:\\\\Temp\\\\MusicFile.wav\" \"C:\\\\Temp\\\\WaveX.cfg\" \"C:\\\\Temp\\\\MusicFileOut.wav\" \n");
return -1;
}
strncpy_s(InputFile,256, argv[1],strlen(argv[1]));
strncpy_s(ConfigFile,256, argv[2],strlen(argv[2]));
strncpy_s(OutputFile,256, argv[3],strlen(argv[3]));
ReadConfig(ConfigFile);
ReadFIRCoeff();
ProcessWav(InputFile,OutputFile);
}
void ReadConfig(char* FileName)
{
char RetStr[256];
for (int i = 0; i < 4; i++)
{
FIR[i].Enabled = false;
strcpy(FIR[i].CoeffFile, "");
for (int j = 0; j < DEFTAPS; j++)
{
FIR[i].FIRCoeff[j] = 0.0;
}
}
GetPrivateProfileString("Global_Params", "WAYS", "1", RetStr, 64, FileName);
DivideCount = atoi(RetStr);
GetPrivateProfileString("Global_Params", "FORMAT", "16", RetStr, 64, FileName);
OutputFormat_Bit = atoi(RetStr);
GetPrivateProfileString("Global_Params", "StreamOut", "0", RetStr, 64, FileName);
if (strcmp("1",RetStr)==0)
StreamOut = true;
else
StreamOut = false;
GetPrivateProfileString("Global_Params", "FileOut", "0", RetStr, 64, FileName);
if (strcmp("1",RetStr)==0)
FileOut = true;
else
FileOut = false;
GetPrivateProfileString("Global_Params", "SampleRate", "0", RetStr, 64, FileName);
SampleRate = atoi(RetStr);
GetPrivateProfileString("CH12", "COEFF", "", FIR[0].CoeffFile, 256, FileName);
GetPrivateProfileString("CH34", "COEFF", "", FIR[1].CoeffFile, 256, FileName);
GetPrivateProfileString("CH56", "COEFF", "", FIR[2].CoeffFile, 256, FileName);
GetPrivateProfileString("CH78", "COEFF", "", FIR[3].CoeffFile, 256, FileName);
GetPrivateProfileString("CH12", "Delay", "0", RetStr, 64, FileName);
FIR[0].DelayLength = atoi(RetStr);
GetPrivateProfileString("CH34", "Delay", "0", RetStr, 64, FileName);
FIR[1].DelayLength = atoi(RetStr);
GetPrivateProfileString("CH56", "Delay", "0", RetStr, 64, FileName);
FIR[2].DelayLength = atoi(RetStr);
GetPrivateProfileString("CH78", "Delay", "0", RetStr, 64, FileName);
FIR[3].DelayLength = atoi(RetStr);
GetPrivateProfileString("CH12", "Offset", "0", RetStr, 64, FileName);
FIR[0].Offset = atoi(RetStr);
GetPrivateProfileString("CH34", "Offset", "0", RetStr, 64, FileName);
FIR[1].Offset = atoi(RetStr);
GetPrivateProfileString("CH56", "Offset", "0", RetStr, 64, FileName);
FIR[2].Offset = atoi(RetStr);
GetPrivateProfileString("CH78", "Offset", "0", RetStr, 64, FileName);
FIR[3].Offset = atoi(RetStr);
for (int i = 0; i < 4; i++)
{
FIR[i].chPointer[0] -= FIR[i].DelayLength;
FIR[i].chPointer[1] -= FIR[i].DelayLength;
FIR[i].dOffset = pow(10.0, FIR[i].Offset/10.0);
}
for (int i = 0; i < DivideCount-1; i++)
{
FIR[i].Enabled = true;
}
}
void ReadFIRCoeff(void)
{
FILE * f;
char buf[64];
for (int j = 0; j < 4; j++)
{
if ((f = fopen(FIR[j].CoeffFile,"r")) != NULL)
{
int i = 0;
double tmp = 0.0;
while((!feof(f)) && (i < DEFTAPS))
{
fgets(buf, sizeof(buf), f);
sscanf(buf, "%lf", &tmp);
FIR[j].FIRCoeff[i] = (float)tmp;
i++;
}
fclose(f);
}
}
}
unsigned read2bytes(FILE *f)
{
unsigned char buf[2];
if (fread(buf, 2, 1, f) != 1) {
fprintf(stderr, "Read error\n");
return(1);
}
return 256U * buf[1] + buf[0] ;
}
unsigned long read4bytes(FILE *f)
{
unsigned char buf[4];
if (fread(buf, 4, 1, f) != 1) {
fprintf(stderr, "Read error\n");
return(1);
}
return ((256LU * buf[3] + buf[2]) * 256LU + buf[1] ) * 256LU + buf[0] ;
}
int CheckInFile(FILE * f)
{
unsigned long len;
unsigned char s[10];
int channels, bits;
if (f == NULL) {
printf("Can not open File\n");
return -1;
}
if (fread(s, 4, 1, f) != 1) {
printf("Read error\n");
fclose(f);
return -1;
}
if (memcmp(s, "RIFF", 4) != 0) {
printf("Not a RIFF format\n");
fclose(f);
return -1;
}
printf("[RIFF] (%lu bytes)\n", read4bytes(f));
if (fread(s, 8, 1, f) != 1) {
printf("Read error\n");
fclose(f);
return -1;
}
if (memcmp(s, "WAVEfmt ", 8) != 0) {
printf("Not a WAVEfmt format\n");
fclose(f);
return -1;
}
len = read4bytes(f);
printf("[WAVEfmt ] (%lu bytes)\n", len);
if (len != 16) {
printf("Length of WAVEfmt must be 16\n");
return -1;
}
printf(" Data type = %u (1 = PCM)\n", read2bytes(f));
channels = read2bytes(f);
printf(" Number of channels = %u (1 = mono, 2 = stereo)\n", channels);
printf(" Sampling rate = %luHz\n", read4bytes(f));
printf(" Bytes per second = %lu\n", read4bytes(f));
printf(" Bytes per sample = %u\n", read2bytes(f));
bits = read2bytes(f);
printf(" Bits per sample = %u\n", bits);
return 0;
}
void PrepareOutFile(FILE * fo, unsigned long len, int channels, int Sample_sec, int BitFormat)
{
unsigned long temp_l;
unsigned short temp_s;
unsigned char s[20];
// Prepare Output Wave File
// RIFF
s[0] = 'R';
s[1] = 'I';
s[2] = 'F';
s[3] = 'F';
fwrite(s, 1, 4, fo);
//filesize
if (channels == 2)
{
temp_l = len + 36;
}
else //extended
{
temp_l = len + 120;
}
fwrite(&(temp_l), sizeof(long), 1, fo);
// WAVE
s[0] = 'W';
s[1] = 'A';
s[2] = 'V';
s[3] = 'E';
fwrite(s, 1, 4, fo);
// fmt chunk
s[0] = 'f';
s[1] = 'm';
s[2] = 't';
s[3] = ' ';
fwrite(s, 1, 4, fo);
// chunk size
if (channels == 2)
{
temp_l = 16;
}
else //extended
{
temp_l = 40;
}
fwrite(&(temp_l), sizeof(long), 1, fo);
// format PCM = 1
if (channels == 2)
{
temp_s = 1;
}
else //extended, 0xFFFE
{
temp_s = 0xFFFE;
}
fwrite(&(temp_s), sizeof(short), 1, fo);
// channel stereo = 2, or Extended
temp_s = channels;
fwrite(&(temp_s), sizeof(short), 1, fo);
// sample 44100, 48000, 88200, 96000 etc
temp_l = Sample_sec;
fwrite(&(temp_l), sizeof(long), 1, fo);
// Bytes per sec
temp_l = channels * BitFormat/8 * Sample_sec;
fwrite(&(temp_l), sizeof(long), 1, fo);
// Block (Bytes per sample&channel)
temp_s = channels * BitFormat/8 ;
fwrite(&(temp_s), sizeof(short), 1, fo);
// Bits per sample
temp_s = BitFormat ;
fwrite(&(temp_s), sizeof(short), 1, fo);
//Extended Format area
if (channels > 2)
{
//22 bytes extended area
temp_s = 22;
fwrite(&(temp_s), sizeof(short), 1, fo);
//bit width
temp_s = BitFormat ;
fwrite(&(temp_s), sizeof(short), 1, fo);
//channel Mask. 2=0x03, 4=0x0F, 6=0x3F, 8=0xFF
temp_l = (1<<channels) -1;
fwrite(&(temp_l), sizeof(long), 1, fo);
//Extended PCM GUID
s[ 0] = 0x01;
s[ 1] = 0x00;
s[ 2] = 0x00;
s[ 3] = 0x00;
s[ 4] = 0x00;
s[ 5] = 0x00;
s[ 6] = 0x10;
s[ 7] = 0x00;
s[ 8] = 0x80;
s[ 9] = 0x00;
s[10] = 0x00;
s[11] = 0xAA;
s[12] = 0x00;
s[13] = 0x38;
s[14] = 0x9B;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -