ミキサーアルゴリズムの研究

5543 ワード

転載はオリジナルの住所を明記してください. http://blog.csdn.net/iflychenyang/article/details/8694147
上記の記事を合わせて、reearchを作りましたが、理想的な効果が得られませんでした.(プログラムを作りながら、音楽を聞きました.混ざった音が耳を潰すところでした.)、プログラムを貼ってください.明日の夜に研究を続けます.
//
//  Mixer.cpp
//        (iphone appstore        )
//
//  Created by chen yang on 13-3-19.
//  Copyright 2013   . All rights reserved.
//
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>

/********   wave            ******/
/*------------------------------------------------
 |             RIFF WAVE Chunk                  |
 |             ID = 'RIFF'                     |
 |             RiffType = 'WAVE'                |
 ------------------------------------------------
 |             Format Chunk                     |
 |             ID = 'fmt '                      |
 ------------------------------------------------
 |             Fact Chunk(optional)             |
 |             ID = 'fact'                      |
 ------------------------------------------------
 |             Data Chunk                       |
 |             ID = 'data'                      |
 ------------------------------------------------*/
/**********   wave         ***********/
/*wave        Chunk  ,     Chunk    ,  Chunk   (ID),
   (size,   Chunk       ),       */
struct TWavehead
{
	/****RIFF WAVE CHUNK*/
	unsigned char a[4];//      'R','I','F','F'
	long int b;        //       -8;  Chunk size  ,       Chunk ID SIZE      ;
	unsigned char c[4];//      'W','A','V','E'
	/****RIFF WAVE CHUNK*/
	/****Format CHUNK*/
	unsigned char d[4];//      'f','m','t',''
	long int e;       //16       ,18      ;   16,         18
	short int f;       //    ,   0x0001;
	short int g;       //    ,1   ,2   ;
	long int h;        //    ;
	long int i;        //       ;
	short int j;       //          ,     ,       ;
	short int k;       //     
	/****Format CHUNK*/
	/***Data Chunk**/
	unsigned char p[4];//      'd','a','t','a'
	long int q;        //        ,           
};//  WAVE         

typedef struct TWavehead TWavehead;

void writeHead(FILE *fp, long int filesize)
{
	if (fp == NULL)
	{
		return;
	}
	const int CHANNELS = 1;
	const int RATE = 16000;
	const int SIZE = 16;
	struct TWavehead wavehead;
	
	wavehead.a[0] = 'R';
	wavehead.a[1] = 'I';
	wavehead.a[2] = 'F';
	wavehead.a[3] = 'F';
	wavehead.b = filesize - 8;
	wavehead.c[0] = 'W';
	wavehead.c[1] = 'A';
	wavehead.c[2] = 'V';
	wavehead.c[3] = 'E';
	wavehead.d[0] = 'f';
	wavehead.d[1] = 'm';
	wavehead.d[2] = 't';
	wavehead.d[3] = ' ';
	wavehead.e = 16;
	wavehead.f = 1;
	wavehead.g = CHANNELS;
	wavehead.h = RATE;
	wavehead.i = RATE*CHANNELS*SIZE/8;
	wavehead.j = CHANNELS*SIZE/8;
	wavehead.k = SIZE;
	wavehead.p[0] = 'd';
	wavehead.p[1] ='a';
	wavehead.p[2] ='t';
	wavehead.p[3] ='a';
	wavehead.q = filesize;
	
	fseek(fp, 0, SEEK_SET);
	fwrite(&wavehead, 1, sizeof(wavehead), fp);
}


void mixWav(char *backgroundFilePath, char *foregroundFilePath, char *dstFilePath)
{
	FILE *backgroundFile = fopen(backgroundFilePath, "rb");
    if(NULL == backgroundFile)
	{
		printf("Can Not find backgroundFile\r
"); return ; } FILE *foregroundFile = fopen(foregroundFilePath, "rb"); if(NULL == foregroundFile) { printf("Can Not find foregroundFilePath\r
"); return ; } FILE *dstFile = fopen(dstFilePath, "wb"); if(NULL == dstFile) { printf("Can Not find dstFilePath\r
"); return ; } int waveHeadSize = sizeof(struct TWavehead); // fseek(backgroundFile, 0, SEEK_SET); void *backgroundHead = malloc(waveHeadSize); int success = fread(backgroundHead, 1, waveHeadSize, backgroundFile); struct TWavehead *backgroundWaveHead = (struct TWavehead *)backgroundHead; printf("headSize:%d,isSuccess:%d",waveHeadSize,success); // //fseek(secondFile, 0, SEEK_SET); void *foregroundHead = malloc(waveHeadSize); success = fread(foregroundHead, 1, waveHeadSize, foregroundFile); struct TWavehead *foregroundWaveHead = (struct TWavehead *)foregroundHead; // int fileSize = foregroundWaveHead->q; // writeHead(dstFile,fileSize); fseek(backgroundFile, waveHeadSize, SEEK_SET); fseek(foregroundFile, waveHeadSize, SEEK_SET); char sample1, sample2; int value; while(!feof(foregroundFile)) { if(feof(foregroundFile)) { fseek(backgroundFile, waveHeadSize, SEEK_SET); } // , sample1 = fgetc(foregroundFile); sample2 = fgetc(backgroundFile); if ((sample1 < 0) && (sample2 < 0)) { //value = sample1 + sample2 - (sample1 * sample2 / (-1 *(pow(2,16-1)-1))); double i = 2, j = 15; value = sample1 + sample2 - (sample1 * sample2 / (-1 *(pow(i,j)-1))); } else { //value = sample1 + sample2 - (sample1 * sample2 / (pow(2,16-1)-1)); double i = 2, j = 15; value = sample1 + sample2 - (sample1 * sample2 / (pow(i,j)-1)); } fputc(value, dstFile); } fclose(backgroundFile); fclose(foregroundFile); fclose(dstFile); } int main(int argc,char *argv[]) { mixWav("d:\\game.wav", "d:\\background.wav", "d:\\temp.wav"); return 0; }