DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Merging wave files (Wave Audio Package)

 
   Reply to topic    DELPHI AREA Forum Index -> DELPHI AREA's Products
View previous topic :: View next topic  
Author Message
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 420
Location: Tehran, Iran

PostPosted: 07/09/03 18:12    Post subject: Merging wave files (Wave Audio Package) Reply with quote

Here is the implementation of a function to make a wave audio stream by merging several wave audio streams. The function expects all input waves have identical format, otherwise it fails and returns False.

Code:
uses
  WaveUtils, MMSystem;

function MergeWaves(DstStream: TStream; SrcStreams: array of TStream): Boolean;

  function AppendWave(mmIO: HMMIO; Stream: TStream;
    ExpectedFormat: PWaveFormatEx): Boolean;
  var
    Buffer: array[0..1023] of Char;
    DataSize, DataOffset: Cardinal;
    ReadCount: Cardinal;
    WaveFormat: PWaveFormatEx;
  begin
    Result := False;
    GetStreamWaveAudioInfo(Stream, WaveFormat, DataSize, DataOffset);
    try
      if (ExpectedFormat^.cbSize = WaveFormat^.cbSize) and
         CompareMem(ExpectedFormat, WaveFormat, ExpectedFormat^.cbSize) then
      begin
        Stream.Seek(DataOffset, soFromBeginning);
        while DataSize <> 0 do
        begin
          if DataSize > SizeOf(Buffer) then
            ReadCount := SizeOf(Buffer)
          else
            ReadCount := DataSize;
          Stream.ReadBuffer(Buffer, ReadCount);
          mmioWrite(mmIO, @Buffer[0], ReadCount);
          Dec(DataSize, ReadCount);
        end;
        Result := True;
      end;
    finally
      FreeMem(WaveFormat);
    end;
  end;

var
  mmIO: HMMIO;
  ckRIFF, ckData: TMMCKInfo;
  WaveFormat: PWaveFormatEx;
  Dummy: Cardinal;
  I: Integer;
begin
 Result := False;
 if Length(SrcStreams) > 0 then
 begin
   GetStreamWaveAudioInfo(SrcStreams[0], WaveFormat, Dummy, Dummy);
   try
     mmIO := CreateStreamWaveAudioInfo(DstStream, WaveFormat, ckRIFF, ckData);
     try
       for I := 0 to Length(SrcStreams) - 1 do
       begin
         if not AppendWave(mmIO, SrcStreams[I], WaveFormat) then
           Break; // Error: Wave format is not identical
       end;
       Result := True;
     finally
       mmioAscend(mmIO, @ckData, 0);
       mmioAscend(mmIO, @ckRIFF, 0);
       mmioClose(mmIO, 0);
     end;
   finally
     FreeMem(WaveFormat);
   end;
 end;
end;


Here is an example of usage:

Code:
MergeWaves(AudioPlayer3.Wave, [AudioPlayer1.Wave, AudioPlayer2.Wave]);


Cheers,
Kambiz
Back to top
View user's profile Send e-mail Visit poster's website
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> DELPHI AREA's Products All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group