What's a segmentation fault? Help with array of sound files?

Totally new to this.
Have written an app that loads sound files into an array and plays them all back simultaneously. I feel confident that this is the worst way to do this. Here’s my code:

void testApp::setup(){  
    for(int i = 100; i<295; i++)  
        char songNumber[195];  
        sprintf(songNumber, "%03d.ogg", i);  

When I run this, the files load and play for a minute or so and then suddenly stop. I’m getting the message:

Segmentation fault.
Process returned 139 (0x8B) execution time 116.806 s

I have no idea what this means. Any guidance will be greatly appreciated.

a segmentation fault is something that happens when you try to put your hands in a memory area you are not supposed to poke your fingers into; it often happens when you make mistakes with pointers and/or arrays and makes you program close in your face.

in your code you’re creating an array 195 char long, for 195 times, which i suppose was not the way you wanted it.

something like this:

ofSoundPlayer songArray[195];  
void testApp::setup(){  
   for(int i=100;i<295;i++){  
      string songNumber;  

will try to load 195 sounds (which are quite a lot) starting from file 000100.ogg and ending on file 000294.ogg

Thanks for the reply. I get segmentation fault now. How would I go about achieving the same ends? I want to load 195 files (more, actually), and play them simultaneously.

I tried running your code and I get this error:

/src/testApp.cpp|13|error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘int sprintf(char*, const char*, ...)’|  

in reference to this line:


Now I’m really out of my league.

Also, how would I go about doing the math to figure if I can load and play all of the files?

Many many thanks.

ops, sorry… try to declare songNumber as a char* then cast it to a string after the sprintf() call.

for the hardware part: first thing i would do would be to sum up the size of all the audio files and check if you have enough free ram before loading the files.

I used char* and it runs again, but I still get a segmentation fault. Without doing the real math, I don’t think I have enough memory for this as I ultimately want to play something closer to 400 audio files simultaneously. I don’t think this is the best way to achieve this. Is this at all possible?


What if I were to modify my approach. The program randomly select two audio files, merge them and output a new file, then grab another file, merge it and output that, and so on and so forth until I’d run through the entire array.

How would I go about doing this?

you could create a single buffer, maybe in setup(), before starting the program loop; give a look here for a few hints: http://forum.openframeworks.cc/t/sound-recording/3183/0

anyway, I don’t know what you’re trying to do, but are you sure you really need to mix all those files in your program? if they’ll play together all the time it would be probably easier o mix them with some sound editing software and importing just 1 file (or just a few) in your program…

This post is really helpful for newbies like me. Thanks for the help and guide!