1 Star 4 Fork 0

开源应用 / linux-utau

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
wavread.h 14.00 KB
一键复制 编辑 原始数据 按行查看 历史
xxxx 提交于 2020-09-10 16:13 . 使用sox获取音调信息
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>
#include <cmath>
#include <stdlib.h>
#include <bitset>
#include <iomanip>
//#include "dr_wav.h"
#include "smbPitchShift.h"
#define DR_WAVE_FORMAT_IEEE_FLOAT 0x3
//要在int main()的前面加上函数的声明,因为你的函数写在main函数的后面
int hex_char_value(char ss);
int hex_to_decimal(const char* s);
//string hex_to_binary(char* szHex);
using namespace std;
class infomation//如你所见,定义了一个infomation的class。。。我还不能提取音调,只能以提高多少音调作为信息
{
public:
string name;
float sound;
float tone;
float split;
float starttone;
};
struct wav_struct
{
unsigned long file_size; //文件大小
unsigned short channel; //通道数
unsigned long frequency; //采样频率
unsigned long Bps; //Byte率
unsigned short sample_num_bit; //一个样本的位数
unsigned int data_size; //数据大小
//unsigned int empty;
unsigned char* data; //音频数据 ,这里要定义什么就看样本位数了,我这里只是单纯的复制数据
};
wav_struct get_wav(string filename)//获取wav信息
{
fstream fs;//实例化一个流
wav_struct WAV;
fs.open(filename, ios::binary | ios::in);
fs.seekg(0, ios::end); //用c++常用方法获得文件大小
WAV.file_size = fs.tellg();
//cout<<WAV.channel<<endl;
fs.seekg(0x16);
fs.read((char*)&WAV.channel, sizeof(WAV.channel));
cout<<WAV.channel<<endl;
fs.seekg(0x18);
fs.read((char*)&WAV.frequency, sizeof(WAV.frequency));
fs.seekg(0x1c);
fs.read((char*)&WAV.Bps, sizeof(WAV.Bps));
fs.seekg(0x22);
fs.read((char*)&WAV.sample_num_bit, sizeof(WAV.sample_num_bit));
fs.seekg(0x28);
cout<<sizeof(WAV.data_size)<<endl;
fs.read((char*)&WAV.data_size, sizeof(WAV.data_size));
WAV.data = new unsigned char[WAV.data_size];
fs.seekg(0x2c);
fs.read((char *)WAV.data, sizeof(char)*WAV.data_size);
cout << "文件大小为 :" << WAV.file_size << endl;
cout << "音频通道数 :" << WAV.channel << endl;
cout << "采样频率 :" << WAV.frequency << endl;
cout << "Byte率 :" << WAV.Bps << endl;
cout << "样本位数 :" << WAV.sample_num_bit << endl;
cout << "音频数据大小:" << WAV.data_size << endl;
//cout << "最后10个数据:" << endl;
//cout<<hex[10];
fs.close();
return WAV;
}
void make(infomation* sounds,int x)
{
unsigned int totalSampleCount[x];//这个地方收集samplecount,我这里提供了三个样本,所以定义为2
unsigned long frequency = get_wav(sounds[0].name).frequency;//先获取frequency
for(unsigned long i=0; i<x;i++)//循环,获取wav的基本信息
{
wav_struct WAV2;
WAV2=get_wav(sounds[i].name);//获取该名字的信息
cout<<sounds[i].name<<endl;
cout<<WAV2.data_size<<endl;//debug用23333
totalSampleCount[i]=(int)(WAV2.data_size/2)*sounds[i].split;
delete[] WAV2.data;//删除数组里所有内容
}
FILE* fp = fopen("z.wav", "wb");//新建一个叫z的wav文件,设置为可写
if (fp == NULL ) {
printf("文件打开失败.\n");
return;
}
for(int i=0;i<x;i++)
{
totalSampleCount[i] *= sizeof(float);//格式化大小信息
}
int nbit = 32;
int FORMAT_PCM = DR_WAVE_FORMAT_IEEE_FLOAT;
int nbyte = nbit / 8;
char text[4] = { 'R', 'I', 'F', 'F' };//输入基本信息
uint32_t count=0;
for(int i=0;i<x;i++)
{
//cout<<totalSampleCount[i]<<endl;
count+=totalSampleCount[i];//把内存加起来
//cout<<count<<endl;
}
cout<<"test"<<endl;
uint32_t long_number = 36 + count;
fwrite(text, 1, 4, fp);
fwrite(&long_number, 4, 1, fp);
cout<<"test"<<endl;
text[0] = 'W';
text[1] = 'A';
text[2] = 'V';
text[3] = 'E';
fwrite(text, 1, 4, fp);
text[0] = 'f';
text[1] = 'm';
text[2] = 't';
text[3] = ' ';
fwrite(text, 1, 4, fp);
long_number = 16;
fwrite(&long_number, 4, 1, fp);
int16_t short_number = FORMAT_PCM;//默认音频格式
fwrite(&short_number, 2, 1, fp);
short_number = 1; // 音频通道数
fwrite(&short_number, 2, 1, fp);
long_number = frequency; // 采样率
fwrite(&long_number, 4, 1, fp);
long_number = frequency * nbyte; // 比特率
fwrite(&long_number, 4, 1, fp);
short_number = nbyte; // 块对齐
fwrite(&short_number, 2, 1, fp);
short_number = nbit; // 采样精度
cout<<"ggg"<<endl;
fwrite(&short_number, 2, 1, fp);
char data[4] = { 'd', 'a', 't', 'a' };
fwrite(data, 1, 4, fp);
cout<<1<<endl;
long_number = count;
cout<<"test"<<endl;
fwrite(&long_number, 4, 1, fp);
cout<<"sizeof"<<sizeof(totalSampleCount)<<endl;
for(int i=0;i<x;i++)//3是你音频个数
{
//cout<<i<<endl;
wav_struct WAV2;
WAV2=get_wav(sounds[i].name);
//cout<<WAV2.data_size<<endl;
unsigned long size = WAV2.data_size/2 ;
float hex2[WAV2.data_size/2];
for (unsigned long k =0; k<(int)WAV2.data_size*sounds[i].split; k = k + 2)
{
//右边为大端
unsigned long data_low = WAV2.data[k];
unsigned long data_high = WAV2.data[k + 1];
double data_true = data_high * 256 + data_low;
long data_complement = 0;
//取大端的最高位(符号位)
int my_sign = (int)(data_high / 128);
//printf("%d ", my_sign);
if (my_sign == 1)
{
data_complement = data_true - 65536;
}
else
{
data_complement = data_true;
}
//printf("%d ", data_complement);
setprecision(4);
double float_data = (double)(data_complement/(double)32768);
//cout<<k/2<<endl;
hex2[k/2]=float_data*sounds[i].sound;//响度设置,如你所见
//cout<<hex2[k/4]<<endl;
}
if (hex2 != NULL && sounds[i].tone !=0)
{
float semitones = sounds[i].tone-sounds[i].starttone; // 向上移动8个半音
float pitchShift = pow(2.0f, semitones / 12.0f); //将半音转换为因子
//printf("pitchShift:%f", pitchShift);
smbPitchShift(pitchShift, (WAV2.data_size/2)*sounds[i].split, 2048, 4, WAV2.frequency, hex2, hex2);
//std::cout << " 处理耗时: " << int(nProcessTime * 1000) << " 毫秒" << std::endl;
}
delete[] WAV2.data;
fwrite(hex2, totalSampleCount[i], 1, fp);
}
fclose(fp);
}
/*void make(infomation* sounds,int x)
{
unsigned int totalSampleCount[x];//这个地方收集samplecount,我这里提供了三个样本,所以定义为2
unsigned long frequency = get_wav(sounds[0].name).frequency;//先获取frequency
wav_struct WAV=get_wav("empty.wav");
for(unsigned long i=0; i<x;i++)//循环,获取wav的基本信息
{
wav_struct WAV2;
WAV2=get_wav(sounds[i].name);//获取该名字的信息
cout<<WAV2.data_size<<endl;//debug用23333
totalSampleCount[i]=(int)(WAV2.data_size/2)*sounds[i].split;
delete[] WAV2.data;//删除数组里所有内容
}
FILE* fp = fopen("z.wav", "wb");//新建一个叫z的wav文件,设置为可写
if (fp == NULL ) {
printf("文件打开失败.\n");
return;
}
for(int i=0;i<x;i++)
{
totalSampleCount[i] *= sizeof(float);//格式化大小信息
}
int nbit = 32;
int FORMAT_PCM = DR_WAVE_FORMAT_IEEE_FLOAT;
int nbyte = nbit / 8;
char text[4] = { 'R', 'I', 'F', 'F' };//输入基本信息
uint32_t count=0;
for(int i=0;i<x;i++)
{
if(i==0)
{
if(sounds[0].start>20)
{
int x =(int)(((float)sounds[0].start/200.0)*448858);
x=sizeof(float);
count+=x+totalSampleCount[0];
}
else {
count+=totalSampleCount[0];
}
}
else {
if(sounds[i].start-sounds[i-1].finish>20)
{
int x = (int)(((float)(sounds[i].start-sounds[i-1].finish)/200.0)*448858);
x=sizeof(float);
cout<<"the empty is"<<(int)(((float)(sounds[i].start-sounds[i-1].finish)/200.0)*448858)<<endl;
count+=(int)(((float)(sounds[i].start-sounds[i-1].finish)/200.0)*448858)+totalSampleCount[i];
}
//cout<<totalSampleCount[i]<<endl;
else {
count+=totalSampleCount[i];//把内存加起来
//cout<<count<<endl;
}
}
}
cout<<"test"<<endl;
uint32_t long_number = 36 + count;
fwrite(text, 1, 4, fp);
fwrite(&long_number, 4, 1, fp);
cout<<"test"<<endl;
text[0] = 'W';
text[1] = 'A';
text[2] = 'V';
text[3] = 'E';
fwrite(text, 1, 4, fp);
text[0] = 'f';
text[1] = 'm';
text[2] = 't';
text[3] = ' ';
fwrite(text, 1, 4, fp);
long_number = 16;
fwrite(&long_number, 4, 1, fp);
int16_t short_number = FORMAT_PCM;//默认音频格式
fwrite(&short_number, 2, 1, fp);
short_number = 1; // 音频通道数
fwrite(&short_number, 2, 1, fp);
long_number = frequency; // 采样率
fwrite(&long_number, 4, 1, fp);
long_number = frequency * nbyte; // 比特率
fwrite(&long_number, 4, 1, fp);
short_number = nbyte; // 块对齐
fwrite(&short_number, 2, 1, fp);
short_number = nbit; // 采样精度
cout<<"ggg"<<endl;
fwrite(&short_number, 2, 1, fp);
char data[4] = { 'd', 'a', 't', 'a' };
fwrite(data, 1, 4, fp);
cout<<1<<endl;
long_number = count;
cout<<"test"<<endl;
fwrite(&long_number, 4, 1, fp);
cout<<"sizeof"<<sizeof(totalSampleCount)<<endl;
for(int i=0;i<x;i++)//x是你音频个数
{
//cout<<i<<endl;
wav_struct WAV2;
if(i==0)
{
cout<<"yes I did"<<endl;
if(sounds[0].start>20)
{
unsigned long sizetest=(int)(((double)sounds[0].start/400.0)*448858);
cout<<sizetest<<endl;
float hex[sizetest/2];
for (unsigned long k =0; k<sizetest; k = k + 2)
{
unsigned long data_low = -0;
unsigned long data_high = -0;
double data_true = data_high * 256 + data_low;
long data_complement = 0;
//取大端的最高位(符号位)
int my_sign = (int)(data_high / 128);
//printf("%d ", my_sign);
if (my_sign == 1)
{
data_complement = data_true - 65536;
}
else
{
data_complement = data_true;
}
//printf("%d ", data_complement);
setprecision(4);
double float_data = (double)(data_complement/(double)32768);
hex[k/2]=float_data;//响度设置,如你所见
}
sizetest*=sizeof(float);
fwrite(hex, sizetest, 1, fp);
}
}
else
{
cout<<"it the second"<<endl;
cout<<sounds[i].start-sounds[i-1].finish<<endl;
if(sounds[i].start-sounds[i-1].finish>20)
{
cout<<"test it is"<<(int)((((float)(sounds[i].start-sounds[i-1].finish))/400.0)*448858)<<endl;
unsigned long sizetest=(int)((((double)(sounds[i].start-sounds[i-1].finish))/400.0)*448858);
float hex[sizetest/2];
for (unsigned long k =0; k<sizetest; k = k + 2)
{
unsigned long data_low = -0.4;
unsigned long data_high = -0.4;
double data_true = data_high * 256 + data_low;
long data_complement = 0;
//取大端的最高位(符号位)
int my_sign = (int)(data_high / 128);
//printf("%d ", my_sign);
if (my_sign == 1)
{
data_complement = data_true - 65536;
}
else
{
data_complement = data_true;
}
//printf("%d ", data_complement);
setprecision(4);
double float_data = (double)(data_complement/(double)32768);
hex[k/2]=float_data;//响度设置,如你所见
//cout<<hex2[k/4]<<endl;
}
sizetest*=sizeof(float);
fwrite(hex, sizetest, 1, fp);
}
}
WAV2=get_wav(sounds[i].name);
//cout<<WAV2.data_size<<endl;
unsigned long size = WAV2.data_size/2 ;
float hex2[WAV2.data_size/2];
for (unsigned long k =0; k<(int)WAV2.data_size*sounds[i].split; k = k + 2)
{
//右边为大端
unsigned long data_low = WAV2.data[k];
unsigned long data_high = WAV2.data[k + 1];
double data_true = data_high * 256 + data_low;
long data_complement = 0;
//取大端的最高位(符号位)
int my_sign = (int)(data_high / 128);
//printf("%d ", my_sign);
if (my_sign == 1)
{
data_complement = data_true - 65536;
}
else
{
data_complement = data_true;
}
//printf("%d ", data_complement);
setprecision(4);
double float_data = (double)(data_complement/(double)32768);
//cout<<k/2<<endl;
hex2[k/2]=float_data*sounds[i].sound;//响度设置,如你所见
//cout<<hex2[k/4]<<endl;
}
if (hex2 != NULL && sounds[i].tone !=0)
{
float semitones = sounds[i].tone; // 向上移动8个半音
float pitchShift = pow(2.0f, semitones / 12.0f); //将半音转换为因子
//printf("pitchShift:%f", pitchShift);
smbPitchShift(pitchShift, (WAV2.data_size/2)*sounds[i].split, 2048, 4, WAV2.frequency, hex2, hex2);
//std::cout << " 处理耗时: " << int(nProcessTime * 1000) << " 毫秒" << std::endl;
}
delete[] WAV2.data;
fwrite(hex2, totalSampleCount[i], 1, fp);
}
fclose(fp);
}
*/
C++
1
https://gitee.com/deepin-opensource/linux-utau.git
git@gitee.com:deepin-opensource/linux-utau.git
deepin-opensource
linux-utau
linux-utau
master

搜索帮助