Display the memory buffer in a chart (oscilloscope)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Display the memory buffer in a chart (oscilloscope)

Loic
Dear all,

I am trying to use UOS library.
The objective is to record the sound from my microphone.
To store the sound in a buffer.
Then to display the signal in a chart (like an oscilloscope).
Theres is a problem in the code below.
The values in the buffer are for example -2.03E27 or 2.33E-37 and a display in a chart does not show wave forms.
I have exactly the same issue when I store in a buffer a *wav file (which was properly recorded).
If anyone can help me?

uos_CreatePlayer(SoundPlayerIndex);

SampleRate := 44100;
SampleFormat := 2; // Int16
Channels := 1; // Mono
FramesCount := 65536;

// saving in a Memory-Buffer:
SetLength(SoundBuffer, 0);
uos_AddIntoMemoryBuffer(SoundPlayerIndex, @SoundBuffer, SampleRate, SampleFormat, Channels, FramesCount);

uos_AddFromDevIn(SoundPlayerIndex);
uos_Play(SoundPlayerIndex);

sleep(3000); // Duration of recording (I don't know?)
uos_Stop(SoundPlayerIndex);
ShowMessage('Recording done');

// Diplaying in a chart
ChartSignalLine.Clear;
for p := Low(SoundBuffer) to High(SoundBuffer) do
  ChartSignalLine.AddXY(p/SampleRate*1e3, SoundBuffer[p]);

Other question, how can I do that if the recording has 2 channels?
Many thanks in advance for your reply
Reply | Threaded
Open this post in threaded view
|

Re: Display the memory buffer in a chart (oscilloscope)

fredvs
Administrator
Hello Loic and welcome to uos forum.

I will test your code asap (I am too busy this week end, sorry).
But did you try the demo waveform?
It shows how to do to show the wave-form of a array of samples.

If you only use the data of the array of samples, this will not show the wave-form but the value of the electric-current.

> Other question, how can I do that if the recording has 2 channels?
Using uos_AddFromDevIn() set the number of channels according the device used, it depends of your sound-card.

You can check how many channels in the input device (see the example DeviceInfos).
So for the buffer check first how many channels has the input device and adapt it.

Fred
Reply | Threaded
Open this post in threaded view
|

Re: Display the memory buffer in a chart (oscilloscope)

fredvs
Administrator
To get the number of channels of a input, you may also use, after uos_AddFromDevIn():

function uos_InputGetChannels(PlayerIndex: cint32; InputIndex: cint32): cint32;
Reply | Threaded
Open this post in threaded view
|

Re: Display the memory buffer in a chart (oscilloscope)

fredvs
Administrator
This post was updated on .
In reply to this post by Loic
Hello and sorry for the long delay.

Could you try this:

uos_CreatePlayer(SoundPlayerIndex);

SampleRate := 44100;
SampleFormat := 0; // float 32
Channels := -1; // Use the default of sound-card and if mono will force to stereo
FramesCount := 65536;

// saving in a Memory-Buffer:
SetLength(SoundBuffer, 0);
uos_AddIntoMemoryBuffer(SoundPlayerIndex, @SoundBuffer, SampleRate, SampleFormat, Channels, FramesCount);

----------------

And when you get the array SoundBuffer, if you need you can split each channel with something like this:

procedure splitchannels;
var
  arrayleft, arrayright : array of single;
  x, y : integer;
 begin
   setlength(arrayleft, Length(SoundBuffer) div 2);
   setlength(arrayright, Length(arrayleft));
 
   x := 0;
   y := 0;
 
  while x < length(arrayleft) do
  begin
   arrayleft[x]   := SoundBuffer[y];
   arrayright[x] := SoundBuffer[y+1];
   x := x + 1 ;
   y := y + 2;
  end;
end;