Sampled sound function


i created function play low quality sampled sound out speaker attached arduino.
it plays 4000 1 bit per sample sound through digital pin.
maximum 50 seconds in atmega 328 memory.
it stores data bit packed in array.
it enough play short sounds @ low quality.
here function:
code: [select]

#include <avr/pgmspace.h>
void playsound(prog_uchar input[], long len, int soundpin){
   long arrayc=0;
   while(arrayc < len){
       unsigned char s = pgm_read_byte_near(array + arrayc);
       for(unsigned char bs = 1;bs < 128;bs *= 2){
           digitalwrite(soundpin, s & bs);
           delaymicroseconds(250);
       }
   arrayc++;
   }
}

syntax:
code: [select]

void playsound(prog_uchar input, long len, int sound)
where
input bit packed program space array of samples played.
len length of array in bytes
soundpin pin play sound out

the following program converts 8000hz 8 bit mono files bit packed arrays function play:
code: [select]

#include <stdio.h>
#include <stdlib.h>
#define n(x) ((x) > 130)? 1:0
int main(int argc, char *argv[]){
printf("note: input file must in 8000 8 bit samples per second mono\n");
if(argc != 3){printf("usage: %s <input.wav> <output.c>\n", argv[0]);}
file *f = fopen(argv[1], "rb");
file *o = fopen(argv[2], "w");
unsigned char a,b,c,d,e,byte;
fprintf(o, "#include <avr/pgmspace.h>\nprog_uchar array[] progmem = {");
unsigned int cnt=0,_cnt=0;
while(!feof(f)){
fread(&a, 1, 1, f);
fread(&e, 1, 1, f);
fread(&b, 1, 1, f);
fread(&e, 1, 1, f);
fread(&c, 1, 1, f);
fread(&e, 1, 1, f);
fread(&d, 1, 1, f);
a = n(a);
b = n(b);
c = n(c);
d = n(d);
byte = (a | b << 1 | c << 2 | d << 3);
fread(&a, 1, 1, f);
fread(&e, 1, 1, f);
fread(&b, 1, 1, f);
fread(&e, 1, 1, f);
fread(&c, 1, 1, f);
fread(&e, 1, 1, f);
fread(&d, 1, 1, f);
a = n(a);
b = n(b);
c = n(c);
d = n(d);
byte |= (a << 4 | b << 5 | c << 6 | d << 7);
fprintf(o, "%d,", byte);
cnt++;
_cnt++;
if(cnt > 60){fprintf(o, "\n"); cnt = 0;}
}
fprintf(o, "0};");
fclose(o);
fclose(f);
printf("\noutput array of %d bytes\n", _cnt);
return 0;
}

my code sucks works.

quote
my code sucks works.

i'll agree first part, , take word second part.
in code
code: [select]
while(arrayc < len){
long arrayc=0;
unsigned char s = pgm_read_byte_near(array + arrayc);
for(unsigned char bs = 1;bs < 128;bs *= 2){
 digitalwrite(soundpin, s & bs);
 delaymicroseconds(250);
}
arrayc++;
}

arrayc both global variable , local variable. not array.

there no comments explain going on, , (lack of) indenting makes hard follow.

there nothing in post defining how data progmem.

but, concept neat. thank taking time post this.


Arduino Forum > Forum 2005-2010 (read only) > Software > Development > Sampled sound function


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums