// try1.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include <stdio.h> //for printf,fgets...
#include <stdlib.h> //for exit
#include <vector> //for vector
#include <string.h> //for strlen
#include <string> //for string
#include <algorithm> //for sort
#include<io.h>
#include<fstream>
#define MAXLINE 256
using namespace std;
typedef struct Number{
int time;//歌词时间
int line;//所在行
};
static int counter=0,counter2=0,n=0;
static int LINE = 0;//记录歌词所在的行
int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime);
int strtoint(char *str);
int operator<(Number x,Number y);
int it3=0,it4,wordnum;
int main(int argc, char *argv[]){
char buf[MAXLINE];
vector<string> sentences, finalSentence;
vector<Number> songTime;
FILE *fd;
char data[50],t1[50];
strcpy(data,".\\LRC\\");
strcpy(t1,".\\LRC\\周杰伦 - 麦芽糖.lrc");
struct _finddata_t fa;
long fHandle;
if( (fHandle=_findfirst( ".\\LRC\\*.lrc", &fa ))==-1L )//这里可以改成需要的目录
{
printf( "当前目录下没有txt文件\n");
return 0;
}
else
do
{
strcat(data,fa.name);
printf( "找到文件:%s\n", fa.name );
fd=fopen(data,"r");
strcpy(data,".\\LRC\\");
if(fd == NULL){
perror("open file");
exit(1);
}
//处理歌词
while(fgets(buf, sizeof(buf), fd) != NULL){
LRCPrase(buf, sentences, songTime);
}
sort(songTime.begin(), songTime.end());//按照时间排序
//printf("%d\n", sentences.size());
/*vector<string>::iterator it = sentences.begin();
for(; it != sentences.end(); it++){
//printf("%d\t,%d\t", (*it).time, (*it).line);
printf("%s", (*it).c_str());
}*/
//按时间顺序排序歌词
vector<Number>::iterator it1 = songTime.begin();
for(; it1 != songTime.end(); it1++){
//printf("%d\t,%d\n", (*it1).time, (*it1).line);
finalSentence.push_back(sentences[(*it1).line]);
}
int st=0;
it1 = songTime.begin();
st=(*it1).time;
vector<string>::iterator it = finalSentence.begin();
for(; it1 != songTime.end() && it != finalSentence.end(); it1++, it++){
//printf("%d\t %d\t%s", (*it1).time, (*it1).line, (*it).c_str());
}
it1--;
int fi;
fi=(*it1).time;
//printf("歌词滚动部分总时间:%d\n",fi-st); //没用了
//printf("总字数:%d\n",counter);
printf("平均速度:%d\n",(counter2/n));
counter=0;
n=0;
counter2=0;
it3=0;
it4=0;
wordnum=0;
fclose(fd);
}while( _findnext(fHandle,&fa)==0 );
_findclose( fHandle );
//fd = fopen("李慧珍 - 爱死了昨天.lrc", "r");
//fd = fopen("周杰伦 - 麦芽糖.lrc", "r");
//fd = fopen("周杰伦 - 七里香.lrc", "r");
//fd = fopen(t1, "r");
return 0;
}
int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime){
if(strlen(str) == 1){//空行
return 0;
}else{
char *p, *q, *temp;
q = str;
//处理时间的
while((p = strchr(q, '[')) != NULL && (temp = strchr(q, ']')) != NULL){
//printf("\n");
q = p + 1;
q[temp - q] = '\0';
//printf("%s ", q); //打印这行时间
struct Number number;
if((number.time = strtoint(q)) < 0){
return 0;
}
it4=number.time-it3;
//换算成微妙:
//printf("微:%d ",number.time);
it3=number.time;
//printf("差值:%d ",it4);
number.line = LINE;
songTime.push_back(number);
q = temp + 1;
}
//printf("%s", temp + 1);
//截取歌词
p = ++temp;
while(*temp != NULL){
temp++;
}
p[temp - p] = '\0';
counter+=(temp-p-1)/2;
if(wordnum!=0){
//printf("速度:%d ",(it4/wordnum));
if((it4/wordnum)<1000){ //大于1000很有可能是非歌词部分导致,可不进行统计
counter2+=(it4/wordnum);
n++;
}
}
wordnum=(temp-p-1)/2;
//printf("字数:%d ",wordnum);
//printf("%s", p);
string s(p);
sentences.push_back(s);
LINE++;
return 1;
}
}
//把char转换为int
int chartoint(char ch){
return ch - '0';
}
int strtoint(char *str){//计算时间,微秒
if(isdigit(str[0]) && isdigit(str[1])
&& isdigit(str[0]) && isdigit(str[0])
&& isdigit(str[0]) && isdigit(str[0])){
int mintue = chartoint(str[0]) * 10 + chartoint(str[1]);
int second = chartoint(str[3]) * 10 + chartoint(str[4]);
int microsecond = chartoint(str[6]) * 10 + chartoint(str[7]);
return (mintue * 60 + second) * 1000 + microsecond * 10;
}
return -1;
}
//重载<操作符,用在sort函数比较中
int operator<(Number x,Number y){
return x.time < y.time;
}
通过LRC文件分析出一首歌曲的速度
最新推荐文章于 2024-11-01 12:24:48 发布