#include "sound.hpp" #include "readwav.hpp" #include "specs.hpp" #include "sdriver.hpp" #include "jmalloc.hpp" #include "specs.hpp" #include "macs.hpp" #include #include #include #include #include #include #include #include #include #include #include #include "dprint.hpp" #include #include int sound_child_fd=-1, sound_parent_fd=-1, sound_status[2], sound_child_pid; #define BUF_BITS 9 #define BUF_SIZE (1<last_channel_time) { last_channel_time=channels[i].add_time; if (free_channel==-1) free_channel=i; } } else free_channel=i; } channels[free_channel].add_time=last_channel_time+1; channels[free_channel].data=seffects[id].data+1024; channels[free_channel].left=seffects[id].size-1024; channels[free_channel].volume_shift=(127-volume)>>4; } int output_sounds() // return 0 if no sounds to ouput { unsigned char *s; int i,j,bytes=0; // usleep((BUF_SIZE*90000/SAMPLE_SPEED)); // sleep long enough for old sample to alomst finish signed short sums[BUF_SIZE],run_size,*sp; memset(sums,0,BUF_SIZE*sizeof(short)); for (j=0;jbytes) // how many bytes to output? bytes=run_size; // add the chanels together into a short for (i=0,sp=sums;i>channels[j].volume_shift; if (channels[j].left<=BUF_SIZE) channels[j].data=NULL; else channels[j].left-=BUF_SIZE; } } for (i=0,sp=sums,s=buf;i127) *s=255; else *s=(unsigned char)(*sp+128); write(s_dev, buf, BUF_SIZE); return 1; // always say we have something, we will do blanks if nothing else } int sound_init(int argc, char **argv) { int i; char buf[PIPE_BUF]; for (i=1;i=total_effects || !seffects[id].size) { fprintf(stderr,"SNDDRV : bad unload sound id %d\n",id); sound_quit(f); } free(seffects[id].data); seffects[id].size=0; } break; case SDRIVER_PLAY : { unsigned char vol; read(f,&vol,1); short id; read(f,&id,2); if (id>=total_effects || !seffects[id].size) { fprintf(stderr,"SNDDRV : bad play sound id %d\n",id); sound_quit(f); } play(id,vol); } break; default : fprintf(stderr,"SNDDRV : Bad command %d\n",cmd); sound_quit(f); break; } } } } sound_effect::sound_effect(char *filename) { long rate; short id; if (sound_child_fd>=0) { unsigned char cmd=SDRIVER_LOAD; write(sound_parent_fd,&cmd,1); unsigned short string_length=strlen(filename)+1; write(sound_parent_fd,&string_length,2); write(sound_parent_fd,filename,string_length); read(sound_status[0],&id,2); size=(long)id; } } sound_effect::~sound_effect() { short id; if (sound_child_fd>=0) { unsigned char cmd=SDRIVER_UNLOAD; // tell the driver to unload the sound write(sound_parent_fd,&cmd,1); id=(short)size; write(sound_parent_fd,&id,2); } } void sound_effect::play(int volume, int pitch, int panpot) { if (sound_child_fd>=0) { unsigned char cmd=SDRIVER_PLAY; write(sound_parent_fd,&cmd,1); unsigned char vol=(unsigned char)volume; write(sound_parent_fd,&vol,1); short id=(short)size; write(sound_parent_fd,&id,2); } } void sound_uninit() // called by parent { if (sound_child_fd>=0) // mkae sure the child has been forked { char cmd=SDRIVER_QUIT; write(sound_parent_fd,&cmd,1); close(sound_parent_fd); close(sound_child_fd); close(sound_status[0]); close(sound_status[1]); sound_child_fd=-1; } } song::song(char *filename) { data=NULL; Name=strcpy((char *)jmalloc(strlen(filename)+1,"song name"),filename); song_id=0; } song::~song() { if (playing()) stop(); if (data) jfree(data); jfree(Name); } void song::play(unsigned char volume) { printf("play song %s, volume %d\n",name(),volume); song_id=1; } void song::stop(long fadeout_time) // time in ms { printf("stop song %s, fade timeout %d\n",name(),fadeout_time); song_id=0; } int song::playing() { return song_id; } void set_music_volume(int volume) // 0...127 { ; }