Solved

Convert Wave Sample Rate

Posted on 2007-12-03
4
849 Views
Last Modified: 2008-02-01
Hi all

I am sampling audio from a number of sources mainly at 44100 16bit stereo but some sources are 48000 16bit stereo and I need to convert these sources to 44100.

I have the actual raw audio data available to me in memory so what I am looking for is some code (a function or procedure) which I can pass the audio data pointer and a target sample rate and have it returned in the correct format on-the-fly.

I can do this through the ACM but its not ideal and relies on opening, closing and managing the ACM. I would much prefere it in code.

I have searched the internet on some way to do this or even an explanation of how it could be done but I can't find anything. It's either really easy to do and I am overthinking it or it's just not very well documented.

Any help is appreciated.

Thanks.
0
Comment
Question by:nacker2000
  • 3
4 Comments
 
LVL 16

Expert Comment

by:CodedK
ID: 20404123
Hi nacker2000.

You can use "ACS Audio Component Suite"  :  http://acs.ullihome.de/acs/index.php/Main_Page

It has a sample rate converter and its free.
Direct download link : http://acs.ullihome.de/acs/downloads/libs.zip

Hope this helps.
0
 
LVL 16

Accepted Solution

by:
CodedK earned 500 total points
ID: 20404173
Also take a look at this PAQ:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20572504.html
~-~-~-~-~-~-~-~-~-~-~-~-~-
To convert from 22.05 KHz to 11.025: Create another buffer that is half the size, and loop through the original buffer copying a sample and droping the next and so on until the end of buffer.

A single sample size is MS * SR
Where MS is 1 for mono, 2 for stereo
and SR is 1 for 8 bit and 2 for 16 bit.
~-~-~-~-~-~-~-~-~-~-~-~-~-
0
 
LVL 5

Author Comment

by:nacker2000
ID: 20412731
Hi CodedK

Thanks for your posts. I have been working away on this problem and actually came across this example http://www.delphipraxis.net/topic104889_upsampling+algorithmus.html&highlight=upsampling on the Delphi Praxis site. Its in German but the code is English.

I modified the code so I could convert from any format to any format as in the code snippet.

The example I found and the converted code works the same as the idea you posted.

Both your posts were helpful and the link I posted and the code will hopefully come in handy for someone else.
type

  TWaveSample = record

    l: smallint;

    r: smallint;

  end;
 

procedure ConvertAudio(_in: TStream; _out: TStream; _inrate: integer = 48000; _outrate: integer = 44100);

var

  smpl1,smpl2,smpl3: TWaveSample;

  i,i2: int64;

  alength: int64;

begin

  _in.Position := 0;
 

  alength := round((_in.Size / sizeof(TWaveSample))*(_outrate / _inrate))+1;

  i := 0;

  if not assigned(_out) then

    _out := TMemoryStream.Create;

  _out.Position := 0;

  while i < alength do

    begin

      i2 := round(i*(_inrate/_outrate)) * sizeof(TWaveSample);

      _in.Position := i2;

      _in.Read(smpl1,sizeof(TWaveSample));

      smpl2.l := smpl1.l;

      smpl2.r := smpl1.r;

      _out.Write(smpl2,sizeof(TWaveSample));

      i := i + 1;

    end;

end;

Open in new window

0
 
LVL 16

Expert Comment

by:CodedK
ID: 20412776
Thank you my friend.
I'm glad i could help a little :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now