Solved

Convert Wave Sample Rate

Posted on 2007-12-03
4
862 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 155
Firemonkey android show image from resource ? 1 71
scroll down TListBox component in Delphi 1 42
FMX TEdit KeyUp handler detecting  "enter" key 4 62
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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