LukA_YJK
asked on
=== Microsoft Synthesizer ===
How to use [b]Microsoft Synthesizer[/b] in Delphi ? It is not a standart MIDI Out Device as far as I understood...
ASKER
Sorry, I asked about MIDI, not WAVE audio. Anyway thanks... I got answer from www.programmersheaven.com. Seems I need DirectMusic to use MS Synthesizer, so need DirectX headers:
//plays normal midi or SGT/SGP(DirectMusic Producer) files
procedure PlayFile(FileName: string);
var
Name: string;
Path: string;
begin
Name := ExtractFileName(FileName);
Path := ExtractFilePath(FileName);
DMusicLoader.SetSearchDire ctory(GUID _DirectMus icAllTypes ,StringToO leStr(Path ),True);
DMusicLoader.LoadObjectFro mFile(CLSI D_DirectMu sicSegment ,IID_IDire ctMusicSeg ment8,Stri ngToOleStr (Name),DMu sicSegment );
if ExtractFileExt(Name) = '.mid' then
DMusicSegment.SetParam(GUI D_Standard MIDIFile,$ FFFFFFFF,D MUS_SEG_AL LTRACKS,0, nil);
DMusicSegment.Download(DMu sicPerform ance);
DMusicPerformance.PlaySegm ent(DMusic Segment,0, 0,nil);
end;
//plays normal midi or SGT/SGP(DirectMusic Producer) files
procedure PlayFile(FileName: string);
var
Name: string;
Path: string;
begin
Name := ExtractFileName(FileName);
Path := ExtractFilePath(FileName);
DMusicLoader.SetSearchDire
DMusicLoader.LoadObjectFro
if ExtractFileExt(Name) = '.mid' then
DMusicSegment.SetParam(GUI
DMusicSegment.Download(DMu
DMusicPerformance.PlaySegm
end;
OK no problem
:-)
:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
from http://www.delphi-fundgrube.de/faq13.htm
uses
MMSystem;
procedure MakeSound(Frequency, Duration : integer);
{writes tone to memory and plays it}
var
WaveFormatEx : TWaveFormatEx;
MS : TMemoryStream;
i, TempInt,
DataCount,
RiffCount : integer;
SoundValue : byte;
w : double; // omega ( 2 * pi * frequency)
const
Mono : Word = $0001;
SampleRate : integer = 11025; // 8000, 11025, 22050, or 44100
RiffId : string = 'RIFF';
WaveId : string = 'WAVE';
FmtId : string = 'fmt ';
DataId : string = 'data';
begin
with WaveFormatEx do begin
wFormatTag := WAVE_FORMAT_PCM;
nChannels := Mono;
nSamplesPerSec := SampleRate;
wBitsPerSample := $0008;
nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
nBlockAlign := (nChannels * wBitsPerSample) div 8;
cbSize := 0;
end;
MS := TMemoryStream.Create;
with MS do begin
{Calculate length of sound data and of file data}
DataCount := (Duration * SampleRate) div 1000; // sound data
RiffCount := Length(WaveId)
+ Length(FmtId) + SizeOf(DWord)
+ SizeOf(TWaveFormatEx)
+ Length(DataId) + SizeOf(DWord)
+ DataCount; // file data
{write out the wave header}
Write(RiffId[1], 4); // 'RIFF'
Write(RiffCount, SizeOf(DWord)); // file data size
Write(WaveId[1], Length(WaveId)); // 'WAVE'
Write(FmtId[1], Length(FmtId)); // 'fmt '
TempInt := SizeOf(TWaveFormatEx);
Write(TempInt, SizeOf(DWord)); // TWaveFormat data size
Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // WaveFormatEx record
Write(DataId[1], Length(DataId)); // 'data'
Write(DataCount, SizeOf(DWord)); // sound data size
{calculate and write out the tone signal} // now the data values
w := 2 * Pi * Frequency; // omega
for i := 0 to DataCount - 1 do begin
// wt = w *i /SampleRate
SoundValue := 127 + trunc(127 * sin(i * w / SampleRate));
Write(SoundValue, SizeOf(Byte));
end;
// you could save the wave tone to file with :
// MS.Seek(0, soFromBeginning);
// MS.SaveToFile('C:\MyFile.w
// then reload and play them without having to
// construct them each time.
{now play the sound}
sndPlaySound(MS.Memory, SND_MEMORY or SND_SYNC);
MS.Free;
end;
end; {Alan Lloyd}