C++ help needed - beginner and so confused

This is a discussion on C++ help needed - beginner and so confused within the C++ Programming forums, part of the General Programming Boards category; Ok so i have a skeleton code in which i need to add my own coding. You must write a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    C++ help needed - beginner and so confused

    Ok so i have a skeleton code in which i need to add my own coding.

    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.

    1. Amplify the sound by an integer value, where the user enters the amplification value
    2. Normalise the level of the sound track


    this is what we have been asked to do.

    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");
    }
    is the coding, it needs to allow the user to input a value in which to ampify by and work this out. and say error if its above the max number of smaples and also allow them to normalise the sound.

    I am new to this and dont know where to begin any help would be appreciated thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to write code that will amplify the sound by an integer value, and write code that will normalize the sound level. The part you posted hints that you were told how to do these things later in your assignment, so theoretically you should know what those words mean, specifically for this program. Then you add the code to the process_sound function, after printing out some sort of menu and letting the user pick. The data you need will be found in the sound_data array.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Perhaps start with an amplification value of 1, which should merely result in the output being identical to the input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    // Reads 44100 samples from a 16 bit raw PCM audio file (not a wave file)
    A .wav is the same thing as a raw PCM file, just it has a header tacked on. And in fact, "a raw pcm file" is not a standardized format, since there are a number of variables:
    -number of channels (eg, mono = 1, stereo = 2)
    -sampling rate ("assuming samples are 16 bit")
    -sample size
    which is why .wavs have headers (they contain that information).

    Anyway, there are libraries to do this, but I imagine that you are not suppose to do that (if you are, it may be somewhat easier).

    The FIRST thing you need to do is read up on the pcm format and get an understanding of exactly how the data is organized. I can't remember where I learnt all this stuff*, but here is a decent page to start with:

    Tutorial - Basics - Part 1 - Digital Audio

    Generally it is in interlaced segments. Interlacing is where you have two separate streams of data, eg, AAAAAAA and BBBBBB, that need to be processed synchronously (such as sound channels), and to help facilitate that, the data will be packed:
    ABABABABAB

    * probably from library documentation, but this may not be so good if you can't use a library because it means learning the API as well
    Last edited by MK27; 05-03-2010 at 10:25 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Australia
    Posts
    10
    A quick note about the math - to Amplify you just multiply values, though why you would use an integer for this is beyond me - and to normalize, well the max value in 16 bits is 65535, assuming 0 is at 32768 you can go through the whole file looking for the largest Abs(DataValue-32768) and then using signed math multiply all values by the factor that will expand them to the range of 0-65535.
    Hope this helps, and I don't mind telling, since your course sounds program orientated, and not based in sound science.
    EDIT: When doing the amplify multiply, you might want to check for values outside of the 16 bit range, and do some adjustment to them to keep them in-line, anymore help and I'd be practically doing this for you...
    And don't forget to comment - you might get more marks for well commented code that doesn't work than for uncommented code that does
    Last edited by feeder74; 05-03-2010 at 11:37 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21