Solved

Convert Wave Sample Rate

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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