Some light hearted fun :)

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Code:
#include <windows.h>
#include <cmath>
#include <cstdio>
void Message(LPCSTR msg);

#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651

BOOL PlayFreq(long FREQUENCY, long DurationMillisecs)
{
// taken from http://www.tmsoft.com/tutorial-sound.html
    HWAVEOUT hWaveOut; // Handle to sound card output
    WAVEFORMATEX WaveFormat; // The sound format
    WAVEHDR WaveHeader; // WAVE header for our sound data

    HANDLE Done; // Event Handle that tells us the sound has finished being played.
// This is a real efficient way to put the program to sleep
// while the sound card is processing the sound buffer
    double x;
    int i;

// ** Initialize the sound format we will request from sound card **
    WaveFormat.wFormatTag = WAVE_FORMAT_PCM; // Uncompressed sound format
    WaveFormat.nChannels = 1; // 1=Mono 2=Stereo
    WaveFormat.wBitsPerSample = 8; // Bits per sample per channel
    WaveFormat.nSamplesPerSec = 44100; //1000*(DurationMillisecs/1000); // Sample Per Second
    WaveFormat.nBlockAlign = WaveFormat.wBitsPerSample / 8 * WaveFormat.nChannels;
    WaveFormat.nAvgBytesPerSec = (WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign);

    WaveFormat.cbSize = 0;
    int BUFFERSIZE = (DurationMillisecs * WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign) / 1000; // 4k sound buffer
    char* Data = new char[BUFFERSIZE]; // Sound data buffer

// ** Create our "Sound is Done" event **
    Done = CreateEvent (0, FALSE, FALSE, 0);

// ** Open the audio device **
    if (waveOutOpen(&hWaveOut,0,&WaveFormat,(DWORD) Done,0,CALLBACK_EVENT) != MMSYSERR_NOERROR)
    {
        MessageBox(0,"Sound card cannot be opened.", "error", 0);
        return TRUE;
    }

// ** Make the sound buffer **
    for (i=0; i < BUFFERSIZE; i++)
    {
// ** Generate the sound wave based on FREQUENCY define
// ** x will have a range of -1 to +1

        x = sin(i * 2.0 * PI * FREQUENCY / WaveFormat.nSamplesPerSec) / 2;
        long long val = ((1 << (WaveFormat.wBitsPerSample - 1)) - 1)*x+(1 << (WaveFormat.wBitsPerSample - 1));
        //printf("%d\n", val);
        //printf("%d\n", val);
// ** scale x to a range of 0-255 (signed char) for 8 bit sound reproduction **
        for (int j = 0; j < WaveFormat.nBlockAlign; ++j)
        {
            Data[i + j] = ((unsigned char*)&val)[j];
        }
        i += WaveFormat.nBlockAlign - 1;
    }


// ** Create the wave header for our sound buffer **
    WaveHeader.lpData=Data;
    WaveHeader.dwBufferLength=BUFFERSIZE;
    WaveHeader.dwFlags=0;
    WaveHeader.dwLoops=0;

// ** Prepare the header for playback on sound card **
    if (waveOutPrepareHeader(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
        Message("Error preparing Header!");
        return TRUE;
    }

// ** Play the sound! **
    ResetEvent(Done); // Reset our Event so it is non-signaled, it will be signaled again with buffer finished

    if (waveOutWrite(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
        Message("Error writing to sound card!");
        return TRUE;
    }

// ** Wait until sound finishes playing
    if (WaitForSingleObject(Done,INFINITE) != WAIT_OBJECT_0)
    {
        Message("Error waiting for sound to finish");
        return TRUE;
    }


// ** Unprepare our wav header **
    if (waveOutUnprepareHeader(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
        Message("Error unpreparing header!");
        return TRUE;
    }

// ** close the wav device **
    if (waveOutClose(hWaveOut) != MMSYSERR_NOERROR)
    {
        Message("Sound card cannot be closed!");
        return TRUE;
    }

// ** Release our event handle **
    CloseHandle(Done);
    delete[] Data;
    return FALSE;

}

void Message(LPCSTR msg)
{
    MessageBox(0, msg, " ",0);

}

void sing()
{
    double aCoff[] = {310.26875,-39.47032537521871,9.798126630798038,
                      13.27051785371783,-16.96040764008566,-8.906423734050552,-
                      37.73534821290271,7.801101044011428,-7.55374999999998,-
                      21.58412966106785,-26.72665337680714,12.13025974357324,-
                      4.939592359914349,-24.97963523747909,-22.5211250410882,
                      61.73863536651369,58.0
                     };
    double bCoff[] = {0,38.24090914589485,50.57997447236831,14.18197189328155,
                      35.3284701257369,9.032153654031216,24.38626527586978,1.899775329574208
                      ,15.44625000000001,9.682803946630628,14.05242300568981,
                      18.2559896635539,-1.256529874263107,19.8910892770428,73.63113220218833
                      ,66.50921913718983,0
                     };

    for (int j = 0; j < 24; ++j)
    {
        double sum = 0;
        for (int i = 0; i < 17; ++i)
        {
            sum += aCoff[i] * cos (2 * i * j * PI / 32) + bCoff[i] * sin (2 * i * j * PI / 32);
        }
        if (j % 4 == 3)
            PlayFreq(sum, 1000);
        else
        {
            PlayFreq(sum, 500);
            PlayFreq(sum, 500);
        }
    }
}

int main()
{
    while(true)
    {
        sing();
        Sleep(1000);
    }
}

This hunk of code does something pretty clever. Can you figure out what the exact output is without compiling and running it? (If you do compile it, you'll need to link against the winmm library. Barring that, you need to replace PlayFreq() with your favorite Beep() alternative.)

The code is in C++, though, I don't think I did anything C++y in it. so a good C compiler should be able to handle it.

Lets see how clever ya all are :awe: (this is what happens when you have too much spare time)
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Code:
    #define TEMPO   2
    if (j % 4 == 3)
        PlayFreq(sum, 1000 / TEMPO);
    else
    {
        PlayFreq(sum, 700 / TEMPO);
        PlayFreq(sum, 300 / TEMPO);
    }

Now that's more my style!

P.S. No I didn't figure that out without compiling... cool function though
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
I actually considered throwing in a tempo, but decided this would be slightly simpler to get.

The first couple of versions used a tempo.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
My guess, twinkle twinkle little star?

Edit: In middle-C, 4/4 time

Explanation:
1. j &#37; 4 == 3
6 normal duration notes, followed by 1 double duration.
2. Doing the math for the first iteration of SUM produces around 261, which is around the frequency of middle C
3. 24 change of notes
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
My guess, twinkle twinkle little star?

Edit: In middle-C, 4/4 time

Explanation:
1. j % 4 == 3
6 normal duration notes, followed by 1 double duration.
2. Doing the math for the first iteration of SUM produces around 261, which is around the frequency of middle C
3. 24 change of notes

Bravo!

The coefficients were calculated using a FFT. It is the first time I've ever dealt with FFT so I thought this would be as fun a place as any to put it in (One of the first iterations of this program used a polynomial curve fitting).
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |