How to resolve the algorithm Record sound step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Record sound step by step in the C++ programming language

Table of Contents

Problem Statement

Record a monophonic 16-bit PCM sound into either memory space, a file or array. (This task neglects to specify the sample rate, and whether to use signed samples. The programs in this page might use signed 16-bit or unsigned 16-bit samples, at 8000 Hz, 44100 Hz, or any other sample rate. Therefore, these programs might not record sound in the same format.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Record sound step by step in the C++ programming language

The provided C++ code is a simple command-line audio recorder and playback program that utilizes the Windows MCI (Media Control Interface) library. The program allows users to record, play, pause, and stop audio input using the keyboard.

A brief overview of the code:

  1. MCI Execution: The mciExecute function is used throughout the program to send commands to the MCI device driver. It takes a string containing the MCI command and executes it. If the command is successful, it returns true; otherwise, it returns false.

  2. Recorder Class: The recorder class encapsulates the functionality of the audio recorder and playback.

    • start: This function starts the main loop of the program, which displays a menu of options and allows the user to select an action.
    • record: Starts recording audio input to a temporary file named "my_sound" using the MCI command record.
    • play: Plays the recorded audio from the temporary file. If the audio is currently paused, it resumes playback.
    • pause: Pauses the playback of the audio.
    • stop: Stops recording or playback and closes the MCI device. If recording was active, it saves the recorded audio to the temporary file.
  3. main Function: The main function creates an instance of the recorder class and calls its start function to start the program.

The program operates in a loop, displaying a menu of options (record, play, pause, stop, quit). The user can enter a number corresponding to the desired action.

Based on the user's input, the program performs the corresponding action by calling the appropriate methods in the recorder class. The program continues running until the user selects the "quit" option (5).

In summary, this program provides a basic interface for recording and playing audio on Windows using the MCI library. It demonstrates how to use MCI commands to manipulate audio devices and files.

Source code in the cpp programming language

#include <iostream>
#include <string>
#include <windows.h>
#include <mmsystem.h>

#pragma comment ( lib, "winmm.lib" )
using namespace std;

class recorder
{
public:
    void start()
    {
	paused = rec = false; action = "IDLE";
	while( true )
	{
	    cout << endl << "==" << action << "==" << endl << endl;
	    cout << "1) Record" << endl << "2) Play" << endl << "3) Pause" << endl << "4) Stop" << endl << "5) Quit" << endl;
	    char c; cin >> c;
	    if( c > '0' && c < '6' )
	    {
		switch( c )
		{
		    case '1': record(); break;
		    case '2': play();   break;
		    case '3': pause();  break;
		    case '4': stop();   break;
		    case '5': stop();   return;
		}
	    }
	}
    }
private:
    void record()
    {
	if( mciExecute( "open new type waveaudio alias my_sound") )
	{ 
	    mciExecute( "record my_sound" ); 
	    action = "RECORDING"; rec = true; 
	}
    }
    void play()
    {
	if( paused )
	    mciExecute( "play my_sound" );
	else
	    if( mciExecute( "open tmp.wav alias my_sound" ) )
		mciExecute( "play my_sound" );

	action = "PLAYING";
	paused = false;
    }
    void pause()
    {
	if( rec ) return;
	mciExecute( "pause my_sound" );
	paused = true; action = "PAUSED";
    }
    void stop()
    {
	if( rec )
	{
	    mciExecute( "stop my_sound" );
	    mciExecute( "save my_sound tmp.wav" );
	    mciExecute( "close my_sound" );
	    action = "IDLE"; rec = false;
	}
	else
	{
	    mciExecute( "stop my_sound" );
	    mciExecute( "close my_sound" );
	    action = "IDLE";
	}
    }
    bool mciExecute( string cmd )
    {
	if( mciSendString( cmd.c_str(), NULL, 0, NULL ) )
	{
	    cout << "Can't do this: " << cmd << endl;
	    return false;
	}
	return true;
    }

    bool paused, rec;
    string action;
};

int main( int argc, char* argv[] )
{
    recorder r; r.start();
    return 0;
}


  

You may also check:How to resolve the algorithm Substring step by step in the Logo programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Perl programming language
You may also check:How to resolve the algorithm Function definition step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the C programming language
You may also check:How to resolve the algorithm Animate a pendulum step by step in the Delphi programming language