Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 30th, 2005, 1:15 PM   #1
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Not Beethoven, but...

...I hadda do it: the sound of one .exe clapping.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sys\stat.h>

using namespace std;

class formatChunk
{
public:
	string formatTag;			// "fmt "
	int formatSize;				// Size of chunk following this value
	short audioFormat;			// Two bytes here; value 1 for PCM
	short nChannels;			// Two bytes here, stereo here
	int sampleRate;				// 44100 here
	int byteRate;				// sampleRate * channels * bitsPerSample/8
	short bytesPerSample;		// channels * bitsPerSample/8, two bytes
	short bitsPerSample;		// Two bytes here
	formatChunk ()
	{
		formatTag = "fmt ";
		formatSize = 16;		// PCM default
		audioFormat = 1;		// PCM default
		nChannels = 2;			// Stereo default
		sampleRate = 44100;		// Default
		bitsPerSample = 16;		// Default
		bytesPerSample = nChannels * 
			bitsPerSample / 8;
		byteRate = bytesPerSample * 
			sampleRate;
	}
};
std::ostream& operator << (std::ostream& os, const formatChunk& chunk) 
{
	os << chunk.formatTag.c_str ();
	os.write ((const char *) &chunk.formatSize, 4);
	os.write ((const char *) &chunk.audioFormat, 2);
	os.write ((const char *) &chunk.nChannels, 2);
	os.write ((const char *) &chunk.sampleRate, 4);
	os.write ((const char *) &chunk.byteRate, 4);
	os.write ((const char *) &chunk.bitsPerSample, 2);
	os.write ((const char *) &chunk.bytesPerSample, 2);
	return os;
}
class dataChunk
{
public:
	string dataTag;				// "data"
	int dataSize;				// bytes following, which is 
	dataChunk ()
	{
		dataTag = "data";
	}
};

std::ostream& operator << (std::ostream& os, const dataChunk& chunk) 
{
	os << chunk.dataTag.c_str ();
	os.write ((const char *) &chunk.dataSize, 4);
	return os;
}

class waveHeader
{
public:
	string chunkID;
	int chunkSize;				// Size of file following this value
	string formatTag;			// Specific format
	formatChunk fmt;
	dataChunk sound;
	waveHeader ()
	{
		chunkID = "RIFF";
		formatTag = "WAVE";
	}

};
std::ostream& operator << (std::ostream& os, const waveHeader& header) 
{
	os << header.chunkID.c_str ();
	os.write ((const char *) &header.chunkSize, 4);
	os << header.formatTag.c_str ();
	os << header.fmt;
	os << header.sound;
	return os;
}

int uhOh (string trouble)
{
	cerr << trouble << endl;
	return 1;
}
int main (int argc, char *argv [])
{
	ifstream inFile; 
	ofstream outFile; 
	waveHeader wDesc;
	struct stat fileStats;
	string sourceFile = "C:\\windows\\explorer.bak";
	unsigned padding;
	char *pad = NULL;

	// Get the size of the file to treat as sound
	stat (sourceFile.c_str (), &fileStats);
	// Prepare to adjust to an integral number of samples, if necessary
	padding = fileStats.st_size % wDesc.fmt.bytesPerSample;
	if (padding) char *pad = new char (padding);

	// Fill in the appropriate members of the header
	wDesc.sound.dataSize = fileStats.st_size + padding;
	wDesc.chunkSize = wDesc.formatTag.size () + wDesc.fmt.formatTag.size () +
		wDesc.fmt.formatSize + sizeof (wDesc.fmt.formatSize) +
		wDesc.sound.dataTag.size () + 
		wDesc.sound.dataSize + sizeof (wDesc.sound.dataSize);
	
	// Open the source file and a destination .wav file
	inFile.open (sourceFile.c_str (), ios::in | ios::binary);
	outFile.open ("explorer.wav", ios::out | ios::binary);
	if (!inFile.good () || !outFile.good ()) return uhOh ("File(s) didn't open");

	// Write out the header
	outFile << wDesc;
	// Tack on the "sound"
	outFile << inFile.rdbuf ();
	// Tack on padding, if necessary
	if (padding) outFile << pad;

	// Close the files
	inFile.close ();
	outFile.close ();

	// Play the sound
	system ("explorer.wav");

	return 0;
}
I apologize for the tab expansion -- something's gone kerplookety with my editor and it isn't subbing in 4 spaces per tab.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 30th, 2005, 1:20 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
looks kewl i'll try it as soon as I'm in my windoze box
Polyphemus_ is offline   Reply With Quote
Old Aug 30th, 2005, 1:52 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
That's absolutely brilliant.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 30th, 2005, 2:04 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I'll try it out when I have power at home...
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 30th, 2005, 4:00 PM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
i've used it on my zipped homework from last year (110 mb). Believe me, it's boring as hell, just like my homework, and it is 20 min long .
Polyphemus_ is offline   Reply With Quote
Old Aug 30th, 2005, 4:29 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
For those of you as weird as I, this is an analysis out to 20k or so. The number in the upper right corner refers to that large negative peak. Top is 0db. This is your Windows executive at work. The output is essentially saturated, by the way, clipped like hell. Must be my reproducer.
Attached Images
File Type: jpg FFTanalysis.jpg (20.8 KB, 315 views)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 30th, 2005, 9:58 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Gonna try this out asap.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Aug 30th, 2005, 10:59 PM   #8
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
lol

I remember cat-ing some exe into /dev/something which sent it straight to the speakers

this was back when I used Linux, it's a shame I can't do a similar thing on Windows.

But now I can :-P
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Aug 30th, 2005, 11:01 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
it died on me... can't open file
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 31st, 2005, 6:22 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Hmmm, IR, I have no clue. Access issue? I do see a mistake, however:
Quote:
char *pad = NULL;
if (padding) char *pad = new char (padding);
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC