Article View: borland.public.cppbuilder.nativeapi
Article #16372Re: Beep(freq,dur) MIDI-style
From: Chris Hill
Date: Tue, 20 May 2003 05:18
Date: Tue, 20 May 2003 05:18
62 lines
2368 bytes
2368 bytes
On Sun, 18 May 2003 15:08:04 -0700, "Nicholas Gessler"
<gessler@ucla.edu> wrote:
>I am looking for an extremely simple example of using MIDI.
>Simply the MIDI equivalent of Beep (frequency, duration) which does NOT work
>with Win95.
>Can someone just provide me with a few lines of code to get me started?
>
>Thanks,
>Nick
>
Usually I don't think of MIDI as being a replacement for the Beep
function (I think it would more natural to implement a Beep function
using waveform audio functions).
Here is a crude attempt at a MIDI based beep. I don't know how
correct the FreqToNote function is, I just Googled for it.
#include <windows.h>
#include <mmsystem.h>
#include <math.h>
#define NOTEON(c, n, v) ( 0x90 | (c) | ((n) << 8) | ((v) <<16) )
#define CHANGE_INSTR(c, i) ( 0xC0 | (c) | ((i) << 8) )
DWORD FreqToNote(DWORD frequency)
{
double freq;
int x;
for(x = 0; x < 127; x++)
{
freq = (440.0 / 32) * pow(2,((x - 9) / 12));
if (freq > frequency)
break;
}
return x;
}
void MIDIBeep(DWORD frequency, DWORD duration)
{
HMIDIOUT hMidiOut;
UINT dev = MIDI_MAPPER;
midiOutOpen(&hMidiOut, dev, NULL, 0, CALLBACK_NULL);
midiOutShortMsg(hMidiOut, CHANGE_INSTR(0, 56) /* trumpet */);
midiOutShortMsg(hMidiOut, NOTEON(0, FreqToNote(frequency),
0x7F));
Sleep(duration);
midiOutReset(hMidiOut);
midiOutClose(hMidiOut);
}
int main(int,char*)
{
Beep(600,1000);
MIDIBeep(600,1000);
return 0;
}
Chris Hill
Chris244@aol.com
Technical Details
Message-ID:
<3ec9b925.815056399@forums.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3ec803f7@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,43257d87a9e437b6 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-05-19 22:26:13 PST From: Chris244@aol.com (Chris Hill) Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Beep(freq,dur) MIDI-style Date: Tue, 20 May 2003 05:18:14 GMT Message-ID: <3ec9b925.815056399@forums.borland.com> References: <3ec803f7@newsgroups.borland.com> X-Newsreader: Forte Free Agent 1.21/32.243 NNTP-Posting-Host: 12.221.66.183 X-Trace: newsgroups.borland.com 1053407892 12.221.66.183 (19 May 2003 22:18:12 -0700) Lines: 62 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail