here is the player: haven't toyboxed the mixer yet (just trying to get enough to test sound in aboriginal builds, so will need the mixer to ensure volume is audible)
Code: Select all
/* wav.c - Plays wav file(s).
*
* Copyright 2014 Brad Conroy <bconroy@uis.edu>
*
* See https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
USE_WAV(NEWTOY(wav, "<1", TOYFLAG_BIN))
config WAV
bool "wav"
default n
help
usage: wav [wav file...]
Plays wav files.
*/
#define FOR_wav
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include "toys.h"
void do_wav(int fd, char *name){
struct {
char ChunkID[4]; // "RIFF"
uint32_t ChunkSize;
char Format[4]; // "WAVE"
char Subchunk1ID[4]; // "fmt "
uint32_t Subchunk1Size;
uint16_t AudioFormat; //1
uint16_t NumChannels; //1==mono, 2==stereo
uint32_t SampleRate; //8000, 44100,...
uint32_t ByteRate;
uint16_t BlockAlign;
uint16_t BitsPerSample;
char Subchunk2ID[4]; //0x64617461 "data"
uint32_t Subchunk2Size;
} *hdr = (void *) &toybuf; //to save 44 bytes
read(fd,hdr,44);
if (
memcmp(hdr->ChunkID,"RIFF",4) ||
memcmp(hdr->Format,"WAVE",4) ||
memcmp(hdr->Subchunk1ID,"fmt ",4) ||
memcmp(hdr->Subchunk2ID,"data",4) ||
(1 != hdr->AudioFormat) //or mulaw, alaw, ima-adpcm?
) return;
#if !IS_LITTLE_ENDIAN
bswap_32(hdr->SampleRate);
bswap_16(hdr->NumChannels);
bswap_16(hdr->BitsPerSample);
#endif
int value, dsp=open("/dev/dsp",O_WRONLY);
if (dsp<0) return;
value=hdr->NumChannels;
xioctl(dsp, SNDCTL_DSP_CHANNELS, &value);
value=hdr->SampleRate;
xioctl(dsp, SNDCTL_DSP_SPEED, &value);
value=hdr->BitsPerSample;
xioctl(dsp, SNDCTL_DSP_SETFMT, &value);
while ((value=readall(fd,toybuf,sizeof(toybuf))))
writeall(dsp,toybuf,value);
close(dsp);
}
void wav_main(void){
loopfiles(toys.optargs, do_wav);
}