/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / How to set up an audio buffer

Username:     
Password:     
             

Forum

# 1   2009-10-12 00:42:42 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

How to set up an audio buffer

Hi!

How do i set up an audio buffer? Lets say i write the following in 'Application_Ini':

AUDIO_SetMode(AUDIO_MODE, LG_16_BITS, FRQ_44KHZ, MONO);

...Will this set the audio to 44.1khz, signed 16bit mono sound?
or is it unsigned data?

Lets say that I want double buffered sound. So I can fill the next buffer with sound
while the other is playing. How do I accomplish that?

What code do i need in the 'Application_Handler'?
How do I automatically switch buffer when the first is empty?

This is really basic stuff, but I cant find code for this.


Can I use 'AUDIO_PlaybackBuffer_Status' for this and just set
up one buffer?

If the buffer is LOW_EMPTY does it mean that the first half is playing,
and that I can write to the second half?
     
If the buffer is HIGH_EMPTY does it mean that the second half is playing,
and that I can write to the first half?

Does the buffer repeat itself automatically?
If it doesn't repeat itself, how do I switch buffers without glitches?

Is the following the right way to make the buffer loop?
AUDIO_SetMode(AUDIO_CIRCULAR_MODE, LG_16_BITS, FRQ_44KHZ, MONO);

I think that this has to be answered for anyone who is interested in
generating audio, and not just play samples.

When I look in 'audio.c File Reference' than only 2 modes are mentioned...
AUDIO_MODE and VOICE_MOD

This is true for both AUDIO_SetMode and AUDIO_GetMode

But when I look in 'circle_api.h File Reference' it mentions:
AUDIO_MODE, VOICE_MODE, AUDIO_CIRCULAR_MODE, VOICE_CIRCULAR_MODE

Isn't AUDIO_CIRCULAR_MODE and VOICE_CIRCULAR_MODE supported?

Thanks!!!

// Daniel

Offline

 

# 2   2009-10-12 06:46:57 How to set up an audio buffer

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: How to set up an audio buffer

Hi Daniel,

The AUDIO SetMode function is used only to configure the audio codec. You have to declare your own buffer and pass it as parameter to the AUDIO_Play function.
The transfer from the buffer to the codec is made with DMA, and the variable "AUDIO_PlaybackBuffer_Status" (acceded by the AUDIO_PlaybackBuffer_GetStatus function) is used to indicate the status of the buffer (half and full tranfered).
The AUDIO mode is used for playing from buffers, and VOICE mode for recording to buffers.
The MONO mode is special because the samples are copied to a local intermediate buffer in order to copy the sample to left and right chanels before to be sent to the codec.
The Circular modes do the DMA transfer from or to the buffer indefinitely in loop, instead to transfer the buffer contents once. This allows to manage a buffer as a double buffer, and filling the first part during transfer the second part of the buffer.

But the better for you is to take a look at the "PlayWav" and "Dictaphone" projects on the web site that use double buffer for playing and recording.

Yves

Offline

 

# 3   2009-10-12 14:44:01 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Thank you for your effort! But this doesn't help me much.

Is there someone on this forum who is capable enough to answer my questions instead of avoiding them?

Offline

 

# 4   2009-10-12 16:07:42 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

Hello,

Circular mode will play the buffer until you stop it, and normal mode will play the buffer once.

In mono mode you will have only 16bit for 1 sample, but in stereo you will have 2*16bit for one sample, because both Left & Right data is present.

When the LOW_EMPTY is present the low half of the buffer has just been played, and you can fill data in to that area. When HIGH_EMPTY is present it just finished playing the upper half of the buffer and if circular mode is chosen it will start from the beginning of the buffer.

So that means you ½ buffer length to fill up the buffer.

Yes, you will only use 1 buffer in total... (split in 2 halfs)

Kasper

Offline

 

# 5   2009-10-12 16:34:46 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Thanks for your help!

Ok! this starts to make some sense.

So when it's LOW_EMPTY i want to fill the lower part.
But what part is the lower part in C?

When I have programed other ARM platforms, GNU didn't use the same addressing in C as one other commercial alternative (that I don't remember the name of).

Is the data signed of unsigned?

Offline

 

# 6   2009-10-13 16:36:42 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

Hello,

You define you full buffer like:

s16 buffer[500];

then low part of buffer starts at buffer[0] and high part starts at buffer[250]

The size you set in AUDIO_Play, will be 500 (number of samples)


(i think this way of using arrays and pointers are straigh C code, and not compiler specific)

Yes i believe the data is signed values smile

Kasper

Last edited by repzak (2009-10-13 16:40:31)

Offline

 

# 7   2009-10-13 18:42:01 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Thank you very much Kasper!

Always great to have some one who can give strait answers.

But according to my testing that I did yesterday the data is
unsigned. This is somewhat a problem if you want to make some
DSP calculation, as you can't calculate on unsigned data.

Are you from Danmark Kasper?

Offline

 

# 8   2009-10-13 19:21:12 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

Yes, i am from Denmark.

Datasheet:
http://eu.st.com/stonline/products/lite … /11341.pdf
page 16 Register 0 -> 2-complement sign and magnitude

CircleOS :
AUDIO_CODEC_CRs[0]  = 0x20; //0x20 16khz / 0x00 8Khz
AUDIO_CODEC_CRs[0]  = 0x00;
Setup lines for Voice/Audio... it is set to 00 for bit 3 & 2

So i expect it to me signed values.

Kasper

Offline

 

# 9   2009-10-13 20:18:18 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Ok! cool so maybe I can make it use signed data, thats much better.
What file do you use to declare those variables?

I have a synthesizer form Denmark at home, Droid-3 smile

Offline

 

# 10   2009-10-13 20:24:11 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

They are allready declared in circle OS.. so don't worry about this...

Please take a look in some of the other projects on how to play sounds

Kasper

Offline

 

# 11   2009-10-13 21:02:45 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

I get an error message:

C:\Program\Raisonance\Ride\Examples\soundbuffer\Application.c:96: error: 'AUDIO_CODEC_CRs' undeclared (first use in this function)

If so, there is something thats not set up right at my place.

Offline

 

# 12   2009-10-13 21:15:50 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

Let me put it in another way... you can not & have no need to chage these registers!!

They are allready configured in the OS!

Use Play wave as reference.

Offline

 

# 13   2009-10-14 13:06:34 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

So you are telling me that you cant use signed values,
but if I recompile the OS, then I could or what?

Offline

 

# 14   2009-10-14 15:05:03 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

No, It uses stadard signed values...

Offline

 

# 15   2009-10-14 16:55:28 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Kids, try this at home...

Ok! lets go on your word that it plays signed values.
Here are some code to put in a regular project to test this.

This doesn't play a sinus wave at my place anyway.
How ever, if you use an unsigned sinus waveform
then the output will be a sinus layered upon a
giant jitter cloud form the nice hardware.

I my reality this is a hint that it uses unsigned data.
But if primer still uses signed data, can someone correct
the code please. It would be very informative.



// Put this sinus table with your variables

static s8 sinus[] = 
    {
    0 , 3 , 6 , 9 , 12 , 15 , 18 , 21  ,
    24  , 27  , 30  , 34  , 37  , 39  , 42  , 45  ,
    48  , 51  , 54  , 57  , 60  , 62  , 65  , 68  ,
    70  , 73  , 75  , 78  , 80  , 83  , 85  , 87  ,
    90  , 92  , 94  , 96  , 98  , 100  , 102  , 104  ,
    106  , 107  , 109  , 110  , 112  , 113  , 115  , 116  ,
    117  , 118  , 120  , 121  , 122  , 122  , 123  , 124  ,
    125  , 125  , 126  , 126  , 126  , 127  , 127  , 127  ,
    127  , 127  , 127  , 127  , 126  , 126  , 126  , 125  ,
    125  , 124  , 123  , 122  , 122  , 121  , 120  , 118  ,
    117  , 116  , 115  , 113  , 112  , 110  , 109  , 107  ,
    106  , 104  , 102 , 100 , 98 , 96 , 94 , 92 ,
    90  , 87  , 85  , 83  , 80  , 78  , 75  , 73  ,
    70  , 68  , 65  , 62  , 60  , 57  , 54  , 51  ,
    48  , 45  , 42  , 39  , 37  , 34  , 30  , 27  ,
    24  , 21  , 18  , 15  , 12  , 9  , 6  , 3  ,
    0  , -3  , -6  , -9  , -12  , -15  , -18  , -21  ,
    -24  , -27  , -30  , -34  , -37  , -39  , -42  , -45  ,
    -48  , -51  , -54  , -57  , -60  , -62  , -65  , -68  ,
    -70  , -73  , -75  , -78  , -80  , -83  , -85  , -87  ,
    -90  , -92  , -94  , -96  , -98  , -100  , -102  , -104  ,
    -106  , -107  , -109  , -110  , -112  , -113  , -115  , -116  ,
    -117  , -118  , -120  , -121  , -122  , -122  , -123  , -124  ,
    -125  , -125  , -126  , -126  , -126  , -127  , -127  , -127  ,
    -127  , -127  , -127  , -127  , -126  , -126  , -126  , -125  ,
    -125  , -124  , -123  , -122  , -122  , -121  , -120  , -118  ,
    -117  , -116  , -115  , -113  , -112  , -110  , -109  , -107  ,
    -106  , -104  , -102  , -100  , -98  , -96  , -94  , -92  ,
    -90  , -87  , -85  , -83  , -80  , -78  , -75  , -73  ,
    -70  , -68  , -65  , -62  , -60  , -57  , -54  , -51  ,
    -48  , -45  ,  -42  , -39  , -37  , -34  , -30  , -27  ,
    -24  , -21  , -18  , -15  , -12  , -9  , -6  , -3
    } ;


// This is the initsiation

enum MENU_code Application_Ini(void)
    {
    // Ensure that the current OS version is recent enough
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
        {
        return MsgVersion();
        }


    // Play signed sinus wave
    AUDIO_SetMode(AUDIO_CIRCULAR_MODE, LG_8_BITS, FRQ_44KHZ, MONO);
    AUDIO_Play(sinus,256);
   

    return MENU_CONTINUE_COMMAND;
    }

Offline

 

# 16   2009-10-14 18:36:46 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

You may be correct this is not working... i have not been working so much with 8bit...

Please try 16bit stereo, then later 16bit mono...

for 16bit stereo:
static s16 sinus[] = 
    {
    0,0,3,3,6,6, etc...

for 16bit mono:static s8 sinus[] = 
    {
    0 , 3 , 6 , 9 , 12 , 15 , 18 , 21  ,


I think there can be some problems in 8bit, but i am not sure

Kasper

Offline

 

# 17   2009-10-14 18:59:18 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Ok! I have made some tests here,
And this is my conclusion.

16bit mono is signed.
8bit mono is unsigned.

But ignorance is bliss as usual.
We don't need documentation for this smile

Offline

 

# 18   2009-10-14 19:10:08 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

A question:

How do you tell the OS, that you have filled
the audio buffer with data?

Some rows of code to clarify this would be nice...

Offline

 

# 19   2009-10-14 19:33:56 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

BufferStatus = AUDIO_PlaybackBuffer_GetStatus(LOW_EMPTY); = Low data has been added
BufferStatus = AUDIO_PlaybackBuffer_GetStatus(HIGH_EMPTY); = High data has been added

BufferStatus = AUDIO_PlaybackBuffer_GetStatus(0); = Returns the buffer status

I have been looking in the code and at the moment i can't remember how 8bit stereo is working sad

8bit mono i think i have found 1 / the bug...

16 bit mono / stereo i believe is working wink

Kasper

Offline

 

# 20   2009-10-15 04:18:59 How to set up an audio buffer

fad_electric
Member
Registered: 2009-10-07
Posts: 56

Re: How to set up an audio buffer

Is it better to use stereo mode?
I would like more free memory and CPU.

How big difference is it between stereo mode and mono mode
in memory and CPU usage?

I get much clicking in the sound. Maybe I will come back on this
with some code to test run, if I don't manage to get it click free.

Offline

 

# 21   2009-10-15 06:14:29 How to set up an audio buffer

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: How to set up an audio buffer

When i get some time i will have a look, maybe in the weekend...

Offline

 

Board footer