Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC

streamRipper for Winamp

Rate me:
Please Sign up or sign in to vote.
2.50/5 (5 votes)
26 Sep 2006CPOL 39.7K   454   35  
streamRipper for winamp
/*
** Copyright (C) 2006 Kannan Krishnamoorthy.
**
** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held 
** liable for any damages arising from the use of this software. 
**
** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to 
** alter it and redistribute it freely, subject to the following restrictions:
**
**   1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 
**      If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
**
**   2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
**
**   3. This notice may not be removed or altered from any source distribution.
**
*/
///////////////////////////////////////////////////////////////////////////////
/// @file     Wave.cpp
/// 
/// @author   Kannan K
/// 
/// @brief    implementation of CWave class
/// 
/// @note     
/// @verbatim
/// ___________________________________________________________________________
/// Revision History
/// Kannan K       05-Dec-2005     Created.
/// @endverbatim
///////////////////////////////////////////////////////////////////////////////

#include "Wave.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWave::CWave()
{
	//initialize the wave header buffer
    ZeroMemory((void*)&m_pcmWF, sizeof(m_pcmWF));
	m_pcmWF.wFormatTag = 1;
}

CWave::~CWave()
{
	//close file handle
    m_file.close();
}
/////////////////////////////////////////////////////////////////////////////////
/// @b             setFilename
/// 
/// @fn            void CWave::setFilename(TCHAR * strFilename)
/// 
/// @brief         initialize new wav file
/// 
/// @param         TCHAR * strFilename (wav file name)                                                       
/// 
/// @return        Nil                                                       
/// 
/// @author        Kannan K                                                    
/////////////////////////////////////////////////////////////////////////////////
void CWave::setFilename(TCHAR * strFilename)
{
	// create the wav file
    m_file.open(strFilename,ios::in|ios::out|ios::binary|ios::trunc);
	//add wav file signature
    m_file.write("RIFF", 4);
    DWORD dwFileSize = 36;
	m_file.write((char*)&dwFileSize, sizeof(dwFileSize)) ;
	m_file.write("WAVEfmt ", 8) ;
	DWORD dwFmtSize = 16L;
	//add wav data info
	m_file.write((char*)&dwFmtSize, sizeof(dwFmtSize)) ;
	m_file.write((char*)&m_pcmWF.wFormatTag, sizeof(m_pcmWF.wFormatTag)) ;
	m_file.write((char*)&m_pcmWF.nChannels, sizeof(m_pcmWF.nChannels)) ;
	m_file.write((char*)&m_pcmWF.nSamplesPerSec, sizeof(m_pcmWF.nSamplesPerSec)) ;
	m_file.write((char*)&m_pcmWF.nAvgBytesPerSec, sizeof(m_pcmWF.nAvgBytesPerSec)) ;
	m_file.write((char*)&m_pcmWF.nBlockAlign, sizeof(m_pcmWF.nBlockAlign)) ;
	m_file.write((char*)&m_pcmWF.wBitsPerSample, sizeof(m_pcmWF.wBitsPerSample)) ;
	m_file.write("data", 4) ;
	DWORD dwNum = 0;
	m_file.write((char*)&dwNum, sizeof(dwNum)) ;
}
/////////////////////////////////////////////////////////////////////////////////
/// @b             buildFormat
/// 
/// @fn            void CWave::buildFormat(WORD nChannels, DWORD nFrequency, WORD nBits)
/// 
/// @brief         builds the wav file format based on the given three value
/// 
/// @param         WORD nChannels (audio number of channels),
///                     DWORD nFrequency (samples per second), WORD nBits  (bits per sample)                                                     
/// 
/// @return        Nil                                                       
/// 
/// @author        Kannan K                                                    
/////////////////////////////////////////////////////////////////////////////////
void CWave::buildFormat(WORD nChannels, DWORD nFrequency, WORD nBits)
{
    m_pcmWF.wFormatTag = WAVE_FORMAT_PCM;
	m_pcmWF.nChannels = nChannels;
	m_pcmWF.nSamplesPerSec = nFrequency;
	m_pcmWF.nAvgBytesPerSec = nFrequency * nChannels * nBits / 8;
	m_pcmWF.nBlockAlign = nChannels * nBits / 8;
	m_pcmWF.wBitsPerSample = nBits;
}
/////////////////////////////////////////////////////////////////////////////////
/// @b             addSamples
/// 
/// @fn            void CWave::addSamples(void * pSamples,WORD nSamples)
/// 
/// @brief         add sub-sequent samples to the wav file
/// 
/// @param         void * pSamples (samples pointer),WORD nSamples  (number of samples)                                                     
/// 
/// @return        Nil                                                       
/// 
/// @author        Kannan K                                                    
/////////////////////////////////////////////////////////////////////////////////
void CWave::addSamples(void * pSamples,WORD nSamples)
{
    DWORD nSamplesSize = nSamples * m_pcmWF.nBlockAlign;
    DWORD nTemp;

// 4-RIFF
// 4-filesize 36 + data size
// 8-WAVEfmt
// 4-dwFmtSize
// 2-wFormatTag
// 2-nChannels
// 4-nSamplesPerSec
// 4-nAvgBytesPerSec
// 2-nBlockAlign
// 2-wBitsPerSample
// 4-data
// 4-data size = numSamples + nBlockAlign

	//read the existing data length
    m_file.seekg(4,ios::beg);
    m_file.read((char*)&nTemp,sizeof(nTemp));
    m_file.seekg(4,ios::beg);
	//calculate new length
    nTemp += nSamplesSize;
	//write new data length to file
    m_file.write((char*)&nTemp,sizeof(nTemp));

	//read the existing samplesize
    m_file.seekg(40,ios::beg);
    m_file.read((char*)&nTemp,sizeof(nTemp));
    m_file.seekg(40,ios::beg);
	//calculate new samplesize 
    nTemp += nSamplesSize;
	//write to file
    m_file.write((char*)&nTemp,sizeof(nTemp));
	//seek file to end of file
    m_file.seekg(0,ios::end);
    //write current samples at the end of file
    m_file.write((char*)pSamples,nSamplesSize);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Kannan K living in india, he likes cricket as like all indians,loves to code in C#,VC++ languages.

Comments and Discussions