C语言怎么输出mp3的二进制编码

Python09

C语言怎么输出mp3的二进制编码,第1张

使用fopen以二进制只读方式打开mp3文件,用fseek求得文件大小,申请一片相应大小的内存空间用作缓冲区,然后将文件整个读入到缓冲区中。

    //二进制方式打开文件  

    FILE* fp = fopen("example.mp3","rb")  

    if(NULL == fp)

    {  

        printf("Unable to open mp3 file.\n")  

        exit(1)

    }  

  

    //求得文件的大小  

    fseek(fp, 0, SEEK_END)  

    int size = ftell(fp)  

    rewind(fp)  

  

    //申请一块能装下整个文件的空间  

    char* buffer = (char*)malloc(sizeof(char)*size)  

  

    //读文件  

    fread(buffer,size,1,fp)

具体怎样将记录了模拟音频信号的文件转换为mp3格式文件,我也不知道具体,原理还是可以告诉你!

但是,无论什么文件,都可以使用huffman编码来压缩,

huffman编码是一种无损的编码,就是可以还原样的编码

给你一个我大一看的一huffman编码程序,可以编码也可以解码

比楼上的强一点,找qq:504449327要,就说要huffman编码的程序

这个是我同学的哈夫曼编码程序

另外还有解码的程序,要的话再商量

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define TRUE 1

#define ERROR 0

#define OK 1

#define FALSE 0

#define INFEASIBLE -1

#define OVERFLOW -2

#define Status int

#define MAXLENGTH 128

typedef struct HTnode

{

long weight

int parent

int lchild

int rchild

}HTNode, *HuffmanTree

typedef struct CTnode

{

long weight

char *coded_string

}CharacterTable

typedef char * *HuffmanCode

FILE *fp=NULL

void Analyse (CharacterTable * *character_table, long * *w, char * *chara, int &n)//分析所有不同的字符的权值

{

long *tmpw

char ch, *tmpchara

int i

(*character_table)=(CharacterTable *)malloc(128*sizeof(CharacterTable))//定义存放字母的数组

for(i=0i<128i++)

{

(*character_table)[i].weight=0 //初始化

(*character_table)[i].coded_string=NULL

}

ch=fgetc(fp)

while(!feof(fp))//诺到文件末尾,函数值为真

{

//m=ch

if(ch<128 &&ch>=0)

(*character_table)[ch].weight++//获得各个字母在文件中出现的次数

ch=fgetc(fp)

}

for(i=0, n=0i<128i++)

if((*character_table)[i].weight)

n++//统计有多少不同的字符数

(*w)=(long *)malloc(n*sizeof(long))//deliver the character and the weight to main

(*chara)=(char *)malloc(n*sizeof(char))

tmpw=(*w)

tmpchara=(*chara)

for(i=0i<128i++)

if((*character_table)[i].weight)

{//将权值放入*w数组中

*(*w)=(*character_table)[i].weight

*(*chara)=i//这里i是字符

(*w)++

(*chara)++

}

(*w)=tmpw

(*chara)=tmpchara//指针返回数组头

}

void Select (HuffmanTree *HT, int i, int *Min1, int *Min2)

{

int j, n, tmp1=-1, tmp2=-2

for(n=0n<in++)

{

if(!(*HT)[n].parent)

{

if(tmp1 == -1)

{

tmp1=n

continue

}

if(tmp2 == -2)

{

tmp2=n

if((*HT)[tmp1].weight >(*HT)[tmp2].weight)

{

j=tmp1

tmp1=tmp2

tmp2=j

}

continue

}

if((*HT)[n].weight <(*HT)[tmp2].weight) //scan and change

if((*HT)[n].weight <(*HT)[tmp1].weight)

tmp1=n

else

tmp2=n

}

}

*Min1=tmp1

*Min2=tmp2//tmp[Min2].weight >= tmp[Min1].weight

}

Status Huffman(HuffmanTree *HT, HuffmanCode *HC,long *w, int n)

{

int m, i, Min1, Min2, p1, p2, start, *M1, *M2

char *cd

HuffmanTree *HTp

if(n<1) return ERROR

m=2*n-1

(*HT)=(HTNode *)malloc(m*sizeof(HTNode)) //intialise Hc in main

HTp=HT

for(i=0i<ni++, w++)

{

(*HTp)[i].weight=*w

(*HTp)[i].parent=0

(*HTp)[i].lchild=0

(*HTp)[i].rchild=0

}

for(i<mi++)

{

(*HTp)[i].weight=0

(*HTp)[i].parent=0

(*HTp)[i].lchild=0

(*HTp)[i].rchild=0

}

M1=&Min1

M2=&Min2

for(i=ni<mi++)

{

Select(HT, i, M1, M2)

(*HTp)[Min1].parent=i

(*HTp)[Min2].parent=i

(*HTp)[i].lchild=Min1//左孩子要小一些

(*HTp)[i].rchild=Min2

(*HTp)[i].weight=(*HTp)[Min1].weight + (*HTp)[Min2].weight

}

//coded the weight below

(*HC)=(HuffmanCode)malloc(n*sizeof(char *))

cd=(char *)malloc(n*sizeof(char))

cd[n-1]='\0'

for(i=0i<ni++)

{

start=n-1

for(p1=i, p2=(*HTp)[p1].parentp2!=0p1=p2, p2=(*HTp)[p1].parent)

{

if( (*HTp)[p2].lchild ==p1) //编码, 左孩子为0, 右孩子为1

cd[--start]='0'

else

cd[--start]='1'

}

(*HC)[i]=(char *)malloc((n-start)*sizeof(char))

strcpy((*HC)[i],&cd[start])

} //over

return OK

}

void Weinumber_to_stringnumber(char * *stringnumber, long *w, int leaves)

{//将权值以字符数组形式存放在上米的数组中

char tmp[30]

long i, j, k

int start

for(i=0i<leavesi++)

{

start=29

tmp[start--]='\0'

for(k=w[i], j=k%10k!=0k=k/10, j=k%10)

tmp[start--]=j+'0'

stringnumber[i]=(char *)malloc((29-start)*sizeof(char))

strcpy(stringnumber[i], &tmp[start+1])

}

}

void Save_huffman_weight_dictionary(long *w, char *character, int leaves, HuffmanCode *HC)

{

char * *stringnumber

int i

FILE *fp1

fp1=fopen("weight.txt", "w")

stringnumber=(char * *)malloc(leaves * sizeof(char *))

Weinumber_to_stringnumber(stringnumber, w, leaves)

for(i=0i<leavesi++)

{

fputc(' ', fp1) // for unhuffman add '

fputc(character[i], fp1)

fputc('\t', fp1)

fputs(stringnumber[i], fp1)

fputc('\t', fp1)

fputc('\'', fp1)

fputs((*HC)[i], fp1)

fputc('\'', fp1)

fputc('\n', fp1)

}

fclose(fp1)

}

void Huffman_file_convert(HuffmanCode *HC, CharacterTable *character_table) //fp had opened

{

int i

char ch

FILE *fp2=fopen("coded.txt","w")

for( i=0i<128i++)

if(character_table[i].weight)

{

character_table[i].coded_string=*(*HC)

(*HC)++

}

ch=fgetc(fp)

while(!feof(fp))

{

if( (ch>=0 &&ch<128) &&(character_table[ch].weight) )//it is very importan to add (ch>=0 &&ch<128)

fputs(character_table[ch].coded_string,fp2)

ch=fgetc(fp)

}

fclose(fp2)

}

void fileopen1() //通过指针fp传递信息

{

char filename[100]

do{

printf("\n\n\t请输入要编码的文件:")

scanf("%s", filename)

if ((fp=fopen(filename,"r"))==NULL)

printf("\n\t不能打开此文件! 请重新输入!\n")

}while(!fp)

}

void main()

{

HuffmanTree Ht, *ht//three level pointer

HuffmanCode Hc, *hc

CharacterTable *CT, * *character_table

long *weight, * *w

char * character, * *chara

int leave //the all leaves number

ht=&Ht

hc=&Hc

w=&weight

chara=&character

character_table=&CT

fileopen1()

Analyse(character_table, w, chara, leave)

fseek(fp, 0, 0)//将文件指针还原

Huffman(ht, hc, weight, leave)//构建哈弗曼树!

Save_huffman_weight_dictionary(weight, character, leave, hc)

Huffman_file_convert(hc, CT)

fclose(fp)

}

可以使用PlaySound()函数播放mp3声音,该函数原型位于windows.h中,

函数原型为:

BOOL PlaySound(LPCSTR pszSound, HMODULE hmod,DWORD fdwSound)

参数pszSound是指定了要播放声音的字符串,该参数可以是MP3文件的名字,或是MP3资源的名字,或是内存中声音数据的指针,或是在系统注册表WIN.INI中定义的系统事件声音。如果该参数为NULL则停止正在播放的声音。

参数hmod是应用程序的实例句柄,当播放MP3资源时要用到该参数,否则它必须为NULL。

参数fdwSound是标志的组合,各种可选的标志及意义如下所示。若成功则函数返回TRUE,否则返回FALSE。

播放标志以及含义:

SND_APPLICATION

用应用程序指定的关联来播放声音。

SND_ALIAS

pszSound参数指定了注册表或WIN.INI中的系统事件的别名。

SND_ALIAS_ID

pszSound参数指定了预定义的声音标识符。

SND_ASYNC

用异步方式播放声音,PlaySound函数在开始播放后立即返回。

SND_FILENAME

pszSound参数指定了MP3文件名。

SND_LOOP

重复播放声音,必须与SND_ASYNC标志一块使用。

SND_MEMORY

播放载入到内存中的声音,此时pszSound是指向声音数据的指针。

SND_NODEFAULT

不播放缺省声音,若无此标志,则PlaySound在没找到声音时会播放缺省声音。

SND_NOSTOP

PlaySound不打断原来的声音播出并立即返回FALSE。

SND_NOWAIT

如果驱动程序正忙则函数就不播放声音并立即返回。

SND_PURGE

停止所有与调用任务有关的声音。若参数pszSound为NULL,就停止所有的声音,否则,停止pszSound指定的声音。

SND_RESOURCE

pszSound参数是WAVE资源的标识符,这时要用到hmod参数。

SND_SYNC

同步播放声音,在播放完后PlaySound函数才返回。

************************************************************

例如我想播放在C:\WINDOWS\Media目录中的 Windows XP 启动.MP3文件

程序如下:

#include <windows.h>

#include <stdlib.h>

int main(int argc, char* argv[])

{

PlaySound("C:\\WINDOWS\\Media\\Windows XP 启动.MP3", NULL, SND_FILENAME | SND_ASYNC)

system("pause")

return 0

}

*/:)))))))))))))))))))))))))))))))