![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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;
}
__________________
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
looks kewl
i'll try it as soon as I'm in my windoze box ![]() |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
That's absolutely brilliant.
![]() |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
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
. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
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 |
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Gonna try this out asap.
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Hmmm, IR, I have no clue. Access issue? I do see a mistake, however:
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|