static FileInfo *file_info_new(const char *file){ int fd; FileInfo *fi; wave_header_t header; int hsize; struct stat stbuf; int size; if ((fd=open(file,O_RDONLY|O_BINARY))==-1){ ms_error("Failed to open %s : %s",file,strerror(errno)); return NULL; } if (fstat(fd,&stbuf)==-1){ ms_error("could not fstat."); return NULL; } hsize=ms_read_wav_header_from_fd(&header,fd); if (hsize<=0){ ms_error("%s: not a wav file", file); return NULL; } if (wave_header_get_channel(&header)<1){ ms_error("%s: incorrect number of channels", file); return NULL; } fi=ms_new0(FileInfo,1); size=stbuf.st_size-hsize; fi->rate=wave_header_get_rate(&header); fi->nchannels=wave_header_get_channel(&header); fi->nsamples=size/(sizeof(int16_t)*fi->nchannels); fi->fd = fd; return fi; }
/** * ms_ring_player_new: * @name: The path to the 16-bit 8khz raw file to be played as a ring. * @seconds: The number of seconds that separates two rings. * * Allocates a new MSRingPlayer object. * * * Returns: a pointer the the object, NULL if name could not be open. */ MSFilter * ms_ring_player_new(char *name, gint seconds) { MSRingPlayer *r; int fd=-1; if ((name!=NULL) && (strlen(name)!=0)) { fd=open(name,O_RDONLY); if (fd<0) { g_warning("ms_ring_player_new: failed to open %s.\n",name); return NULL; } }else { g_warning("ms_ring_player_new: Bad file name"); return NULL; } r=g_new(MSRingPlayer,1); ms_ring_player_init(r); if (ms_ring_player_class==NULL) { ms_ring_player_class=g_new(MSRingPlayerClass,1); ms_ring_player_class_init(ms_ring_player_class); } MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_ring_player_class); r->fd=fd; r->silence=seconds; r->freq=8000; if (strstr(name,".wav")!=NULL){ wave_header_t header; int freq,freq2; /* read the header */ read(fd,&header,sizeof(wave_header_t)); freq=wave_header_get_rate(&header); if ((freq2=freq_is_supported(freq))>0){ r->freq=freq2; }else { g_warning("Unsupported sampling rate %i",freq); r->freq=8000; } r->channel=wave_header_get_channel(&header); lseek(fd,WAVE_HEADER_OFFSET,SEEK_SET); #ifdef WORDS_BIGENDIAN r->need_swap=1; #else r->need_swap=0; #endif } ms_ring_player_set_property(r, MS_FILTER_PROPERTY_FREQ,&r->freq); r->state=PLAY_RING; return(MS_FILTER(r)); }
static int read_wav_header(PlayerData *d){ wave_header_t wav; if (read(d->fd,&wav,sizeof(wav))!=sizeof(wav)){ ms_warning("Could not read wav header"); return -1; } if (0!=strncmp(wav.riff_chunk.riff, "RIFF", 4) || 0!=strncmp(wav.riff_chunk.wave, "WAVE", 4)){ ms_warning("Wrong wav header: (default rate/channel -> %i:%i)", d->rate, d->nchannels); return -1; } d->rate=wave_header_get_rate(&wav); d->nchannels=wave_header_get_channel(&wav); d->hsize=sizeof(wav); #ifdef WORDS_BIGENDIAN if (wave_header_get_bpsmpl(&wav)==wave_header_get_channel(&wav) * 2) d->swap=TRUE; #endif return 0; }
static FileInfo *file_info_new(const char *file){ int fd; FileInfo *fi; wave_header_t header; int hsize; struct stat stbuf; int size; int err; if ((fd=open(file,O_RDONLY|O_BINARY))==-1){ ms_error("Failed to open %s : %s",file,strerror(errno)); return NULL; } if (fstat(fd,&stbuf)==-1){ ms_error("could not fstat."); return NULL; } hsize=ms_read_wav_header_from_fd(&header,fd); if (hsize<=0){ ms_error("%s: not a wav file", file); return NULL; } fi=ms_new0(FileInfo,1); size=stbuf.st_size-hsize; fi->buffer=ms_new0(int16_t,size/sizeof(int16_t)); fi->rate=wave_header_get_rate(&header); fi->nchannels=wave_header_get_channel(&header); fi->nsamples=size/(sizeof(int16_t)*fi->nchannels); err=read(fd,fi->buffer,size); if (err==-1){ ms_error("Could not read file: %s",strerror(errno)); goto error; }else if (err<size){ ms_error("Partial read of %i bytes",err); goto error; } return fi; error: file_info_destroy(fi); return NULL; }