# Character/String to Integer Conversion Algorithm; Voice/mp3 input?

• 04-12-2006
sjm96
Character/String to Integer Conversion Algorithm; Voice/mp3 input?
It took my awhile but I finally got a brainwave to solve a simple yet annoying problem. You know when you have a string or character array and you want to get numbers inside it to be real numbers, like 'string num="10101"' to be 'int num=10101'? Well I finally wrote an algorithm that does that. I would post it if other people are interested. Alternatively, tell me how you got around it or if there is some function that does it. I have spent years asking on many forums and no one has told me one that works. The algorithm is a function that uses the following syntax:
int number=convert(string);
It takes a string as an argument and returns the numerical value in that string. Make sure that the string actually contains numbers though, and only numbers...
BTW I am in Bloodshed DEV-C++ on Windows XP.

Also, I need to know how to get input from mp3's and voice (microphone). Preferably I would like to get this information as a number of frequency every x milliseconds. So that an mp3 might get a list of frequencies that show how the song changes. I don't know much about audio files or voice input but I am primarily looking to identify the deepest parts of the song (the beat). I have an algorithm that, if provided a list of frequencies, can organize it into a universal beat scheme that I need for my application.

Thank you!
• 04-12-2006
Sebastiani
>> Alternatively, tell me how you got around it or if there is some function that does it.

C has the atoi function and C++ has stringstreams.

>> Also, I need to know how to get input from mp3's and voice (microphone).

I would recommend a using search engine to find out about the mp3 format and digital signal processing.
• 04-12-2006
sjm96
Basically, I converted the string to a char array of the same length. I took each character and got the int value of the ascii character, and subtracted 48, so the ascii code-48 equals what the ascii code actually represents. Then I multiplied each one by ten to the power of the length of the string minus the place of that character, so if its 203 its 200+00+3. Then I added and returned the value. It's kind of complex so I might post code. I have tried many functions that should work but many generated errors.

I am researching the mp3 structure currently. I would just like some input from veteran coders who may be able to point me in the right directions. Functions that accept microphone/mp3's would be appreciated.
• 04-12-2006
Daved
The built-in features that perform string to int conversions do a better job than your algorithm would for non-simple inputs (like -20), you just have to figure out how to use them properly, which usually isn't that hard. Righting the conversion yourself is a good exercise, but I wouldn't actually use it in real code unless there was some abnormal feature you needed to implement.

If you'd like to find out why you had problems with stringstreams, atoi, or strtol, feel free to post the code and errors.
• 04-12-2006
DougDbug
For the MP3 stuff, first you need to decode the MP3 into an array or a vector (i.e. using the codec that comes with Windows Media Player). Just for reference, a wave file is basically an array... it's a series of individual "samples" that indicate the height of the wave at any point in time. When you "connect the dots" you can "draw" the waveform.

I have a bit of informaton somewhere on how to open an audio file. It's not easy.... Well.. It's probably less than a page of code, it's just not easy to dig-up information and sample code you need. :(

I think I found the info in the Platform SDK, but it might have been in the DirectX SDK.

One of those should also tell you how to "capture" the microphone input and stick it into an array, vector, or file.

All of the functions in the platform SDK and the DirectX SDK are for Windows only... It's not ANSI/ISO standard-portable C++!

If you want to learn about the details of MP3 encoding... i.e. write your own codec or something, check-out the open-source LAME project. The actual MP3 format is covered by patents and maybe trade secrets, so the LAME stuff is for learning purposes only.

The WAV format is much simpler. You don't really need a codec, because there is no tricky compression or encoding. You can get details of the WAV format at Wotsit.org.

For your frequency analysis, you need to perform an FFT (Fast Fourier Fransform). That's standard stuff in the DSP (Digital Signal Processing) world. You can probably find an open-source FFT library, so you shouldn't have to write the code yourself. Or, in your application, you may be able to get-away with using a low-pass filter. Again you can find sample DSP code, or maybe a DSP library.
• 04-12-2006
sjm96
I have tried for many years with the functions and they never worked. I'm happy with what I have now, for my purposes anyway :).

Thanks for all the mp3 info, too! For my program, I can simply use a command-line mp3-to-wav converter from the program and then delete the temporary wave when done. Any number, at all, regardless of frequency, will work. In fact, the wav idea sounds even better. I will basically be converting the numbers from the array into a universal scale of -2 to 2. It will obviously become more complex inward as I work on it but that's the general idea. I just need to know how to access the info within the wav and how to get it into my application. For my purposes, it is safe to assume that I have the path to the file on hand. Once I get this working I will begin working on the microphone aspect of the code.

Thanks for all the help!