Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone,
i have done lots of research on mp3 file structure. i have understood all about mp3 file including how does it work, which type of data it consists, how to read mp3 file data ,how to write mp3 file data ect.now i'm trying to make my own mp3 file just like i'm adding my own audio data in file.but after writing file with my own audio data it doesn't work don't know why?. i'm working in c language.
and please don't suggest me any library because i want to do this with myown.

What I have tried:

below is my code.
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
   FILE *fp,*pt;
   static unsigned char file_data[10000000],a[3];
   fp=fopen("expmp3file.mp3","wb");
   pt=fopen("tt.mp3","rb");
   int t=0,u=0,v=0,x=0;
   unsigned long i=0,j=0,k=0,curr_fr_siz=0,size_of_id3v2=0;
   unsigned char b=150;
   a[0]=255;
   a[1]=255;
   a[2]=251;
   a[3]=250;
   while(fread(&file_data[i],sizeof(char),1,pt)==1)
   {
       i++;
   }
   i=0;
   t=file_data[6];
   u=file_data[7];
   v=file_data[8];
   x=file_data[9];
   size_of_id3v2=t*2097152+u*16384+v*128+x+10;// Synchronization Safe Integer formula last 10 is for id3v2 tag header
   //printf("%d\n",size_of_id3v2);
   while(i<size_of_id3v2)
   {
      fwrite(&file_data[i],sizeof(file_data[i]),1,fp);// writing id3v tag from a another file
      i++;
   }
   i=0;
 curr_fr_siz=(144*(320*1000)/44100+1);
 //printf("%d",curr_fr_siz);
   while(i<4000)
   {
       while(k<4)
       {
        fwrite(&a[k],sizeof(char),1,fp);//writing frame header
        k++;
       }
       k=0;
       j=0;
       while(j<curr_fr_siz)
       {
         fwrite(&b,sizeof(b),1,fp);//writing audio data
         j++;
       }
       i++;
   }
   fclose(fp);
   fclose(pt);
   getch();

}


first of all i added id3v tag data from another file at the beginning of file. after that i have added one by one 4000 mp3 frames with my own data.
Posted
Updated 27-May-18 22:42pm
Comments
jeron1 16-May-18 10:04am    
Sorry for beating a dead horse, but stepping through this with a debugger would have shown you the error pretty quickly.

C++
while(j<curr_fr_siz)
{
  fwrite(&b,sizeof(b),1,fp);//writing audio data
  j++;
}

That code is writing a single value multiple times, what do you expect it to do?
 
Share this answer
 
Comments
Raj Swami 16-May-18 10:10am    
my expectation is that with my own audio data, file should play perfectly in any audio player doesn't matter what sound does it give.
Richard MacCutchan 16-May-18 10:21am    
Reality rarely meets expectation in this life.
Raj Swami 16-May-18 10:28am    
so what is solution of my question
Richard MacCutchan 16-May-18 10:31am    
I don't know. You need to check that each byte that you are writing to the file actually represents some part of a sound.
Raj Swami 16-May-18 10:41am    
ok thank you but a thing every value of byte represent some part of sound no matter we could understood or not that sound.
i am answering my own question.
the code is below.
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
   FILE *fp,*pt,*up;
   static unsigned char file_data[10000000],a[3];
   fp=fopen("mysound.mp3","wb");
   pt=fopen("tt.mp3","rb");
   int t=0,u=0,v=0,x=0;
   unsigned long i=0,j=0,k=0,z=0,y=0,curr_fr_siz=0,size_of_id3v2=0;
   unsigned char c[10],b=50;
   a[0]=255;
   a[1]=251;
   a[2]=226;
   a[3]=68;
   while(fread(&file_data[i],sizeof(char),1,pt)==1)
   {
       i++;
   }
   i=0;
   t=file_data[6];
   u=file_data[7];
   v=file_data[8];
   x=file_data[9];
   size_of_id3v2=t*2097152+u*16384+v*128+x+10;// Synchronization Safe Integer formula last 10 is for id3v2 tag header
   //printf("%d\n",size_of_id3v2);
   while(i<size_of_id3v2)
   {
      fwrite(&file_data[i],sizeof(file_data[i]),1,fp);
      i++;
   }
   i=0;

 curr_fr_siz=(144*(320*1000)/44100)+1;

   while(i<1000)
   {
       while(k<4)
       {
        fwrite(&a[k],sizeof(char),1,fp);//writing header of frame 
        k++;
       }
       k=0;
       j=0;
       while(j<curr_fr_siz-4)
       {
         srand(time(NULL));
         b=z+rand()%(256-z);//random number,z helps to produce different numbers

         fwrite(&b,sizeof(b),1,fp);//writing my own audio data 
       
         j++;
         z++;
         if(z==200)
         {
             z=1;
         }

       }
       i++;
   }
   fclose(fp);
   fclose(pt);
   getch();

}
 
Share this answer
 
Comments
RedDk 23-Aug-18 15:34pm    
There's an infinte loop in here somewhere ... just so you know. I'd have stepped through the code to find out where exactly, but I'm late for a very important date.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900