Need help to convert C to PB [Resolved]

Just starting out? Need help? Post your questions and find answers here.
ThorstenD
New User
New User
Posts: 8
Joined: Fri Mar 27, 2009 8:48 am

Need help to convert C to PB [Resolved]

Post by ThorstenD »

Hello people!
Am new here and needs help with convert from following C code:

Code: Select all

#define amptarget 30000 // target level
#define ampquiet 800 // quiet level
#define amprate 0.02f // amp adjustment rate

typedef struct {
      float gain; // amplification level
      int delay; // delay before increasing level
      int count; // count of sequential samples below target level
      int high; // the highest in that period
      int quiet; // count of sequential samples below quiet level
} AUTOAMPSTUFF;

void CALLBACK autoamp(HDSP handle, DWORD channel, void *buffer, DWORD length, AUTOAMPSTUFF *amp)
{
      short *data=(short*)buffer;
      DWORD c;
      for (c=0;c<length/2;c++) {
            int s=(int)(data[c]*amp->gain); // amplify sample
            int sa=abs(s);
            if (abs(data[c])<ampquiet)
                  amp->quiet++; // sample is below quiet level
            else
                  amp->quiet=0;
            if (sa<amptarget) { // amplified level is below target
                  if (sa>amp->high) amp->high=sa;
                  amp->count++;
                  if (amp->count==amp->delay) { // been below target for a while
                        if (amp->quiet>amp->delay)
                              // it's quiet, go back towards normal level
                              amp->gain+=10*amprate*(1-amp->gain);
                        else
                              amp->gain+=amprate*amptarget/amp->high; // increase amp
                        amp->high=amp->count=0; // reset counts
                  }
            } else { // amplified level is above target
                  if (s<-32768) s=-32768;
                  else if (s>32767) s=32767;
                  amp->gain-=2*amprate*sa/amptarget; // decrease amp
                  amp->high=amp->count=0;
            }
            data[c]=(short)s; // replace original sample with amplified version
      }
}
I hopes somebody can help me. Thank you :D
Last edited by ThorstenD on Fri Mar 27, 2009 1:55 pm, edited 1 time in total.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

With all the -> I had to do a double-take at this part :).

Code: Select all

s<-32768
I would try and convert it but without a test-case I might be way off. The only thing that might be misleading is the '->'. If you pass a structure pointer you can access structure members using the '\' character instead of C's '.'.

Did you try and convert it?
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Hi,

Have fun.

Code: Select all

#amptarget = 30000 ; target level
#ampquiet  = 800 ; quiet level
#amprate   = 0.02 ; amp adjustment rate

Structure AUTOAMPSTUFF
      gain.f ; amplification level
      delay.f ; delay before increasing level
      count.f ; count of sequential samples below target level
      high.f ; the highest in that period
      quiet.f ; count of sequential samples below quiet level
EndStructure

Structure tWordArray
   w.w [ 0 ]
EndStructure

Procedure autoamp ( handle.i , channel.i, *buffer, length.i , *amp.AUTOAMPSTUFF )
 
      Protected *ptrData.tWordArray= *buffer
      Protected c       .i
      Protected s       .i
      Protected sa      .i
      
      For c=0 To length/2 
            s=(*ptrData\w[c]**amp\gain) ; amplify sample
            sa=Abs(s)
            If (Abs(*ptrData\w[c])<#ampquiet)
                *amp\quiet+1 ; sample is below quiet level
            Else
               *amp\quiet=0
               If (sa<#amptarget)  ; amplified level is below target
                     If (sa>*amp\high) : *amp\high=sa : EndIf
                     *amp\count+1
                     If (*amp\count=*amp\delay)  ; been below target For a While
                           If (*amp\quiet>*amp\delay)
                                 ; it's quiet, go back towards normal level
                                 *amp\gain+10*#amprate*(1-*amp\gain)
                           Else
                                 *amp\gain+#amprate*#amptarget/*amp\high ; increase amp
                                 *amp\high=*amp\count=0 ; reset counts
                           EndIf
                     EndIf
                Else   ; amplified level is above target
                     If (s<-32768) 
                        s=-32768
                     Else 
                        If (s>32767) : s=32767 : EndIf
                        *amp\gain-2*#amprate*sa/#amptarget ; decrease amp
                        *amp\high=*amp\count=0
                     EndIf
               EndIf
               *ptrData\w[c]=s ; replace original sample With amplified version
            EndIf
      Next 
      
      ProcedureReturn #Null
EndProcedure
Regards

Wolf
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@Hroudtwolf: Your conversion looks good! Did you ever think about writing
a C-to-PureBasic converter? Or is just easier to do it by hand?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

PB wrote:@Hroudtwolf: Your conversion looks good! Did you ever think about writing
a C-to-PureBasic converter? Or is just easier to do it by hand?
That would be a pretty big undertaking. But in reality, would it actually be useful?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> would it actually be useful?

Hell yeah. The example above taught me a thing or two about C conversions.
It's like looking at the ASM output of a PureBasic source; it teaches you a bit
about ASM when reading it. But I know it's a big ask, I was just wondering if
it's an easy thing to do.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
ThorstenD
New User
New User
Posts: 8
Joined: Fri Mar 27, 2009 8:48 am

Post by ThorstenD »

@Hroudtwolf
Many thanks for the fast help. Functions like it has to do. :D Thank you also to the other. Have a nice day and up to next... Greetings Thorsten
Post Reply