Thread: C++ MP3 Cutter and other I/O Qs

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    14

    C++ MP3 Cutter and other I/O Qs

    hi,

    i just started to learn c++
    so ill start with the big question-

    i want to make a mp3 cutter which will get an mp3 file, starting time,ending time and new filename - and save a new mp3 .

    so..how do i start? i searched google for libraries ...but didnt know what to look for or where...

    i know the syntex, and a lil bit from std (like fstream, and strings, and arrays and loops and all)

    the 2nd project im working on (one in the office and one at home)

    is a simple reading data from one file and displaying it
    what i need to display is a part of the first line of the file, a part of the last line
    and the number of lines.

    i made a code like this (which i suppose should output the first and last lines and line number):
    Code:
    ifstream fin;
    fin.open("c:\\somefile.txt")
    string line;
    getline(fin,line);
    cout<<"The first line is :\n"<<line;
    int counter = 1;
    while (!fin.eof())
    {
            getline(fin,line);
            counter += 1;
    }
    cout<<"The last line is :\n"<<line<<"\nThe number of lines :"<<counter<<endl;
    the problem with this code was the last line was empty (when it wasnt - and i tried saving the last 3 lines each time, and i found out the last line was the one before the last getline...
    when in fact there was no empty line at the end...)

    so 2 questions - the first: how do i get the last line without saving 2 lines at a time?
    the second question : how do i get only a defined part of a string? (like from the 4th character to the 14th character)

    Thank you
    Roy

    Edit - how can i delete one post? it posted it twice for some reason
    Last edited by Rainbowinblack; 11-13-2007 at 01:43 PM. Reason: Multi Post.... :(

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Usually, you want the read function to be used as the control of the while loop:
    Code:
    while (getline(fin,line))
    {
            counter += 1;
    }
    Using eof() to control the loop often doesn't work, because it only returns true if you try to read past the of the file. When using getline, that means that unless the last line does not end with a newline, it will get an empty value for the last read.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    Thank You,

    that answered my first question

    what about - how do i get a part of a string?
    and the mp3 thing?

    Roy

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To get part of the string, use the substr member function. It takes the first character you want to get, followed by a count of how many characters afterward to include.
    Code:
    std::string line = "abcdefg";
    line = line.substr(2, 4);
    After that code is run, line is "cdef" since the character at index 2 is 'c' and then it includes 4 total characters in the substring.

    I don't know anything about mp3 programming, although I'd imagine you'd have to learn a bit more before you are ready for that type of project in C++.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    yeah, i thought so ...

    thank you for your time

    if you have any advice on how to get better, i'd be more than happy to hear it

    like - what programs to try and make and stuff like that

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For your MP3 part, I recommend DirectShow.
    That is, if you're using Windows. Otherwise, I don't know.
    Last edited by Elysia; 11-13-2007 at 02:51 PM.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    i am using Windows,
    where can i find more information about using DirectShow?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, DirectShow is part of the Platform SDK from Microsoft. You'll need it to build apps with DirectShow. The platform sdk doc also contain DirectShow docs, on how to use it.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    MP3 files are rather complicated. Well... Except for plain-text files, most real-world file formats are rather complicated. A good place to start learning about file formats is wotsit.org. Their information on MP3 looks a little "light"... I didn't see a link to an actual spec.

    codeproject.com has an example program that uses DirectShow (Microsoft) to open and convert an MP3. They also have an article/example about the MP3 frame header.

    MP3 files are divided into frames (MP3 was originally designed to be used with compressed audio/video files). Some data can be stored in "extra space" in preceeding or following frames as part of the compression process. So, if you simply cut the file at the frame boundry, you may loose some data. You can learn a bit more about this at mp3-converter.com.

    You can avoid this issue by decoding the file (into an array), cutting it, and then re-compressing it. But, since MP3 is lossy compression, you will degrade the audio quality if you do this. Most audio editors do exactly that, but there are a few special-purpose MP3 programs that can cut & join MP3s without re-coding. Windows comes with an MP3 decoder, but you'd will need an encoder (i.e. LAME).

    You might want to start-out experimenting with "regular" uncompressed WAV files. A WAV file is simpler. It's easier to cut/paste and you can fairly-simply re-create or reformat the file-header.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    Wow, that was very detailed, and extremely helpfull

    I'll start learning about DirectShow and how to use it as well as about the Mp3 file format

    and i think ill start by doing it with dec/enc (simpler cut) and after i'll get that right ill start tryin cutting mp3 without losing quality

    i think after this program i'll know a bit more about c++

    thanks

Popular pages Recent additions subscribe to a feed