Solved

=== Microsoft Synthesizer ===

Posted on 2002-07-03
4
670 Views
Last Modified: 2009-12-16
How to use [b]Microsoft Synthesizer[/b] in Delphi ? It is not a standart MIDI Out Device as far as I understood...
0
Comment
Question by:LukA_YJK
  • 2
4 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 7192511
Is this helps u?

 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.wav');
    // 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}
0
 
LVL 3

Author Comment

by:LukA_YJK
ID: 7193692
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.SetSearchDirectory(GUID_DirectMusicAllTypes,StringToOleStr(Path),True);
DMusicLoader.LoadObjectFromFile(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,StringToOleStr(Name),DMusicSegment);
if ExtractFileExt(Name) = '.mid' then
DMusicSegment.SetParam(GUID_StandardMIDIFile,$FFFFFFFF,DMUS_SEG_ALLTRACKS,0,nil);
DMusicSegment.Download(DMusicPerformance);
DMusicPerformance.PlaySegment(DMusicSegment,0,0,nil);
end;
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 7195236
OK no problem
:-)

0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7264921
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 317
error 1.1 400 Bad request idhttp delphi 18 147
DBGrid or StringGrid ? 6 104
How to insert .png images to TClientDataSet .cds file? 2 31
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question