Solved

Convert Wave Sample Rate

Posted on 2007-12-03
4
852 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 78
Thread safe  opinion 7 126
LAN or WAN ? 11 81
How to create virtual printer while installing my application 5 20
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 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

12 Experts available now in Live!

Get 1:1 Help Now