Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ help needed!! (http://www.go4expert.com/forums/cpp-help-needed-t21909/)

staceyktaylor 27Apr2010 16:20

C++ help needed!!
 
Ok, so i need some serious help, i really do no no where to start, i have been give this skeleton coding, in which i have to enter my own coding

Code:

//
// Skeleton program for assignment 2
//
// Reads 44100 samples from a 16 bit raw PCM audio file (not a wave file)
// The file must be called 'input.pcm'
// into an array called 'sound_data'. You can use any sample rate.
// You can process the data by writing code in the process_sound function
// The program then writes the sound_data array to a destination file, 'output.pcm', in raw PCM format
//
#include <iostream>
#include <fstream> //header to access files
using namespace std;

// define constants
const int max_number_samples = 44100; //max number samples in array
const int max_number_bytes = 2 * max_number_samples; //max number of bytes in file assuming samples are 16 bits
const char in_file[] = "input.pcm";
const char out_file[] = "output.pcm";
int n;

fstream f_in;    // filestream for source file
fstream f_out;    // filesteam for destination file

short signed int sound_data[max_number_samples]; // array of sound samples, 16 bit integer
bool file_found;

void open_source()
{
    // Opens source file as binary (NOT text) stream
    do
    {
        file_found = false;
        cout << "\nOpening file: " << in_file << "\n\n";
        f_in.open (in_file, ios::in | ios::binary);
        if (!f_in)
        {
            cout << "Cannot open file: " << in_file << "\n";
        }
        else
        {
            file_found = true;
        }
    }while (file_found == false);
}

void read_source()
{
    // Reads source file into the array

    cout << "Reading " << in_file << " into array\n\n";

    f_in.read ((char*)sound_data, max_number_bytes);
}


void close_source()
{
    // Closes source file

    cout << "Closing " << in_file << "\n\n";

    f_in.close();
}
void open_destination()
{
    // Opens destination file for writing

   
    do
    {
        file_found = false;
        cout << "\nOpening file: " << out_file << "\n\n";
        f_out.open (out_file, ios::out | ios::binary);
        if (!f_out)
        {
            cout << "Cannot open file: " << out_file << "\n\n";
        }
        else
        {
            file_found = true;
        }
    }while (file_found == false);
}
void write_destination()
{
    // Write sound array to destination file

    cout << "Writing array to " << out_file << "\n\n";

    f_out.write ((char*)sound_data, max_number_bytes);
}

void close_destination()
{
    // Close destination file

    cout << "Closing " << out_file << "\n\n";

    f_out.close();
}

void process_sound()
{
    cout << "Beginning processing of sound data\n\n";
    // Put your code to process the sound here
    // The original sound is in the array sound_data
    // The number of samples in the array is max_number_samples


    cout << "Processing complete\n\n";
}

void main()
{
    open_source();
    read_source();
    close_source();
    process_sound();
    open_destination();
    write_destination();
    close_destination();
    cout << "Program complete.\n";
    system("pause");
}

The coding ive got to add must; 1. Amplify the sound by an integer value, where the user enters the amplification value
2. Normalise the level of the sound track


i dont no where to begin, i have a soundfile im soo stuck, please help!

xpi0t0s 28Apr2010 14:00

Re: C++ help needed!!
 
Do you know how to loop over an array, and modify the values within that array?

What does an individual entry in sound_data[] represent? For example what is 32768 (0x8000)? Is it the zero-line above and below which a sound wave is drawn? Or does it use signed numbers so that silence is a string of zeros, and a square wave might alternate between +100 and -100?

Suppose you're currently looking at the value 100. What would it mean to amplify that by 2? Would the result be 200, or something else?

Normalising requires two passes of the array: first, you determine the maximum absolute* sound value across the whole file, then you can determine the amplification factor necessary to bring that up to the maximum volume. The second part is identical to task 1 except that you use the calculated amplification factor instead of a number entered by the user.

* so you must take into account that -100 would be considered louder than +50, for example.

staceyktaylor 28Apr2010 16:18

Re: C++ help needed!!
 
Quote:

Originally Posted by xpi0t0s (Post 67541)
Do you know how to loop over an array, and modify the values within that array?

What does an individual entry in sound_data[] represent? For example what is 32768 (0x8000)? Is it the zero-line above and below which a sound wave is drawn? Or does it use signed numbers so that silence is a string of zeros, and a square wave might alternate between +100 and -100?

Suppose you're currently looking at the value 100. What would it mean to amplify that by 2? Would the result be 200, or something else?

Normalising requires two passes of the array: first, you determine the maximum absolute* sound value across the whole file, then you can determine the amplification factor necessary to bring that up to the maximum volume. The second part is identical to task 1 except that you use the calculated amplification factor instead of a number entered by the user.

* so you must take into account that -100 would be considered louder than +50, for example.



Ok so you have actually helped me a little more probably without realising because i had no idea where to begin, i have some examples of array loops in a booklet ive been given so i should me able to understand that. Ill try explain better what it is; we have been given some sound files in which we must use to find the minimum and maximum values, we need to allow the user to input what they would like to amplify to, with it saying error or something if it goes over the max volume. And we need to also allow them to normalise the value, the max is 32k and min is -32k. It also includes the soundwave being above and below the zero line.

The only information been give reallys is " You must write a program that performs audio processing on a sound wave stored in a file on the PC. You are provided with a skeleton program that will read and write a sound file to/from an array of samples. You must write code that will manipulate the sound wave once it is in the array. Your program should allow the user to determine what processing is performed on the sound wave and provide appropriate information to the user through the PC monitor. Additional technical details are provided at the end of this brief."



thanks.

xpi0t0s 28Apr2010 22:02

Re: C++ help needed!!
 
Sounds like you get a chance to be fairly creative. Download a trial copy of Goldwave, an audio editor, and see what it allows you to do with tracks. Some of the more complicated functions might be beyond you at this stage but others will be quite simple, e.g. reverse sound would just reverse everything in the array.

Probably what you need to do is display some sort of menu to allow the user to select what they want to do. Maybe you need to provide additional feedback on the process of each operation, e.g. displaying 10%...20%...30%...etc as an operation proceeds.


All times are GMT +5.5. The time now is 11:25.