Shop OBEX P1 Docs P2 Docs Learn Events
Sound by C language — Parallax Forums

Sound by C language

Senji NibanSenji Niban Posts: 4
edited 2015-06-12 23:42 in Learn with BlocklyProp
Hello, everyone.

I'm beginner for propeller chip and C language, and interested in sound out put .
so I got propeller demo board and sound by spin language.
next, I tried C language and checked "libwavplayer".
I customized these files and tried to sound from 10 & 11 pins, but get no sound.
What's bad? please help me.

Senji Niban

////////////////////////////////////////////////////////////////

#include "simpletools.h"

int sampleRate;
unsigned int dacVal;
unsigned int dtSample;
//
int pulse(int fq);
int freq = 44000;
//
int main()
{
CTRA = 0x18000000 + 27;
CTRB = 0x18000000 + 26;
DIRA |= (1<<10);
DIRA |= (1<<11);
sampleRate = 20000;
dtSample = CLKFREQ/sampleRate;
int t = CNT;
while(1)
{
dacVal = pulse(freq);
FRQA = dacVal;
FRQB = dacVal;
waitcnt(t += dtSample);
}
}

int pulse(int fq)
{
unsigned int output;
static int i;
output = 65536;
if( i >= fq / 2)
{
output = 0;
}
//
i = i++;
if( i >= fq )
{
i = 0;
}
return output;
}

////////////////////////////////////////////////////////////////

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2015-06-06 07:18
    You set outputs correctly in dira (10/11) but your counters are connected to pins 26/27. This has to match.
  • Senji NibanSenji Niban Posts: 4
    edited 2015-06-06 08:14
    Dear kuroneko!!

    Thanks for your reply!

    What's a careless mistake...
    Now, I get sound, but a new problem too.

    I change the value of "freq", and nothing changes.
    but I change the value of "output", pitch of tone changes...

    I think that,
    1) waitcnt = interval that next sound is out.
    2) dacVal = voltage of output sound, unsigned 16bit.

    so, I expect "pulse(freq)" generate pulse wave
    and hands value of voltage to "dacVal".

    but "dacVal" seems "frequency" to me.
    What do I get a wrong?
  • kuronekokuroneko Posts: 3,623
    edited 2015-06-08 01:25
    waitcnt is linked to the sample rate your data is sent. In your case (assuming 80MHz) this happens at 20kHz (4k cycles per loop). I'm not sure yet what you intend to do with pulse(). Can you elaborate?
  • Senji NibanSenji Niban Posts: 4
    edited 2015-06-10 20:44
    Dear kuroneko,

    Thank you very much for answering beginner's question.
    I think I don't understand about "waitcnt" yet...

    Then, I hope to work for "pulse()" as "wav_reader()" of "wavplayer.c".
    I expect that
    1) "pulse()" generate pulse waves in ”freq” interval,
    2) "while(1)" is repeated by the same timing as sampling rate,
    3) and "dacVal = pulse(freq)" renew value of FRQA at same timing.

    but values of FRQA don't change at the timing I hope, it's very long interval.
    For changing values at I hope, I must not send value to FRQA, must send to other?
    Buffer, for example.

    This is my second bad code, but it became a terrible little better.
    changing the "freq", pitch is changed.

    //////////////////////////////////////////////////////////////////////

    #include "simpletools.h"

    int sampleRate;
    unsigned int dacVal;
    unsigned int dtSample;
    static volatile int pulse_i;
    int pulse(int fq);
    int freq = 50;
    int main()
    {
    CTRA = 0x18000000 + 10;
    CTRB = 0x18000000 + 11;
    DIRA |= (1<<10);
    DIRA |= (1<<11);
    sampleRate = 44100;
    dtSample = CLKFREQ/sampleRate;
    int t = CNT;
    while(1)
    {
    dacVal = pulse(freq);
    FRQA = dacVal;
    FRQB = dacVal;
    //waitcnt(t += dtSample);
    }
    }

    int pulse(int fq)
    {
    unsigned int output;
    unsigned int max = 65536;
    unsigned int min = 0;
    if( pulse_i <= (fq / 2))
    {
    output = max;
    }
    else
    {
    output = min;
    }
    if( pulse_i >= fq )
    {
    pulse_i = 0;
    }
    else
    {
    pulse_i = pulse_i++;
    }
    return output;
    }
  • kuronekokuroneko Posts: 3,623
    edited 2015-06-12 13:40
    Try this (25Hz square wave on pin 16 with 11.025kHz sample rate) , it should give you an idea about the relationship between the wave you generate and the sample rate. Note, depending on the time spent in pulse() you have to lower your sample rate. The higher the latter the less time you have for operations in each loop run.
    #include "simpletools.h"
    
    #define SAMPLE_RATE (11025)
    #define PIN (16)
    
    static const int freq = 25;
    
    int pulse(int fq) {
      static int acc;
      acc += fq;
      if (acc <= (SAMPLE_RATE / 2))
        return -1;      // max voltage
    
      if (acc > SAMPLE_RATE) {
        acc -= SAMPLE_RATE;
        return -1;      // max voltage
      }
    
      return 0x10000;   // min voltage (avoid 0)
    }
    
    int main() {
      CTRA = 0x18000000 + PIN;
      DIRA |= (1 << PIN);
    
      unsigned int dtSample = CLKFREQ/SAMPLE_RATE;
      int t = CNT;
      while(1) {
          FRQA = pulse(freq);
          waitcnt(t += dtSample);
      }
    }
    
  • Senji NibanSenji Niban Posts: 4
    edited 2015-06-12 23:42
    Dear Kuroneko,

    Thanks very much!
    I learned a lot from your code!!!
    and I'll study PWM and delta-sigma more.

    Cheers!!!!
Sign In or Register to comment.