DSXACMStreams - How To?


Some simple example scenarios to use the DSXACMStreams library.

Index

How to convert a file to MSADPCM?
How to let a user choose an ACM wave format?
How to convert a file to one channel while preserving the original format?
How to compress a file and redirect the RIFF output.
How to get the format of a RIFF wave file.

Warning

The examples shown here don't handle errors correctly. They can crash under some circumstances (files not existing, memory problems). The library calls itsself should handle error conditions correctly. Please refer to the Reference Documentation for error result codes.

Armin Sander, Digital SimpleX GmbH


How to convert a file to MSADPCM?

This example shows how to convert an arbitrary RIFF-WAVE file to the MSADPCM compression format.


implementation
uses
	Classes, Forms,
	UDSStream, UDSWaveStream, UDSWaveFormat, UDSWaveStreamFactory, UDSACMFormat;

procedure ConvertToMSADPCM(const _source : string; const _destination : string);
var _factory : TDSWaveStreamFactory;
var _readstream, _writestream : TDSStream;
begin
	_factory := TDSWaveStreamFactory.Create;

	_readstream := _factory.OpenFileRead(_source);

	_factory.Reset; // Don't need this here!

	_factory.targetformat.format := 2; // WAVE_FORMAT_ADPCM
	_writestream := _factory.OpenFileWrite(_destination, _readstream.format);

	TDSStream.Copy(_readstream, _writestream);

	_readstream.Destroy;
	_writestream.Destroy;

	_factory.Destroy;
end;

How to let a user choose an ACM wave format?

This pops up the system requester to choose an ACM wave format.

Returns the chosen format or nil.


implementation
uses
	Classes, Forms,
	UDSStream, UDSWaveStream, UDSWaveFormat, UDSWaveStreamFactory, UDSACMFormat;

function ChooseAFormat : TDSWaveFormat;
var _choose : TDSACMFormatChoose;
begin
	_choose := TDSACMFormatChoose.Create;
	_choose.Title := 'Choose a format or drink coffee!';
	_choose.OwnerWindow := Application.handle;

	Result := _choose.Choose;

	// formattag and formatattributes contain some information about
	// the chosen format in textual form:

	// Form1.label1.caption := _choose.formattag;
	// Form1.label2.caption := _choose.formatattributes;

	_choose.Destroy;
end;

How to convert a file to one channel while preserving the original format?

Be warned: sometimes this could imply more than one conversion step. Especially if the source-format is a compressed one. Internally the library handles this for you, but the needed memory for buffers increases and performance decreases. Also recompression is a problem concerning the resulting quality.


implementation
uses
	Classes, Forms,
	UDSStream, UDSWaveStream, UDSWaveFormat, UDSWaveStreamFactory, UDSACMFormat;

procedure ToOneChannelPreserve(const _source : string; const _destination : string);
var _factory : TDSWaveStreamFactory;
var _readstream, _writestream : TDSStream;
begin
	_factory := TDSWaveStreamFactory.Create;

	_readstream := _factory.OpenFileRead(_source);

	_factory.Reset; // not needed here

	_factory.targetformat.Assign(_readstream.format);
	_factory.targetformat.channels := 1;

	_writestream := _factory.OpenFileWrite(_destination, _readstream.format);

	TDSStream.Copy(_readstream, _writestream);

	_readstream.Destroy;
	_writestream.Destroy;

	_factory.Destroy;
end;

How to compress a file and redirect the RIFF output.

The following code redirects the file output of a conversion to a TFileStream using the TDSTStream wrapper and the OpenStreamWrite call.


implementation
uses
	Classes, Forms,
	UDSStream, UDSWaveStream, UDSWaveFormat, UDSWaveStreamFactory, UDSACMFormat;

procedure WriteRedirection(const _source : string; const _destination : string);
var _readstream, _writestream : TDSStream;
var _factory : TDSWaveStreamFactory;
var _xstream : TStream;
var _x2stream : TDSStream;
begin
	_factory := TDSWaveStreamFactory.Create;

	_factory.targetformat.format := 2; // WAVE_FORMAT_ADPCM
	_factory.targetformat.channels := 1;

	_readstream := _factory.OpenFileRead(_source);

	_factory.Reset;

	// this example uses TDSTSteam. Of course we could create a
	// TDSFileStream in this case for easier use.

	_xstream := TFileStream.Create(_destination, fmCreate);
	_x2stream := TDSTStream.Create(_xstream);

	_writestream := _factory.OpenStreamWrite(_x2stream, _readstream.format);

	TDSStream.Copy(_readstream, _writestream);

	_readstream.Destroy;
	_writestream.Destroy;

	_x2stream.Destroy;
	_xstream.Destroy;

	_factory.Destroy;

end;

How to get the format of a RIFF wave file.

That's easy stuff. Returns nil if the file was not a RIFF wave file. If you wan't to retrieve the TDSWaveFormat instance just use Result.format.


implementation
uses
	Classes, Forms,
	UDSStream, UDSWaveStream, UDSWaveFormat, UDSWaveStreamFactory, UDSACMFormat;

function GetFormatOfFile(const _fn : string) : TDSWaveStreamFormat;
begin
	Result := HDSWaveStreamFactory.QueryFileFormat(_fn);
end;