Sound by C language
Senji Niban
Posts: 4
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;
}
////////////////////////////////////////////////////////////////
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;
}
////////////////////////////////////////////////////////////////
c
620B
Comments
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?
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;
}
Thanks very much!
I learned a lot from your code!!!
and I'll study PWM and delta-sigma more.
Cheers!!!!