| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: read-wav-raw.ck |
| 3 |
// desc: example that illustrates manually parsing and rendering a |
| 4 |
// mono WAV file. This demonstrates using FileIO in binary mode. |
| 5 |
// FYI in practice, use SndBuf for easier audio file playback! |
| 6 |
// requires: chuck-1.5.0.1 or higher |
| 7 |
// |
| 8 |
// author: Perry R. Cook |
| 9 |
// date: Summer 2023 |
| 10 |
// credit "saigon44.wav": opening words from /Apocalypse Now/ |
| 11 |
//--------------------------------------------------------------------- |
| 12 |
|
| 13 |
// warn people what is about to happen |
| 14 |
<<< "raw mono WAV file reader!!", "" >>>; |
| 15 |
|
| 16 |
// default file |
| 17 |
me.dir() + "saigon44.wav" => string filename; |
| 18 |
// look at command line |
| 19 |
if( me.args() > 0 ) me.arg(0) => filename; |
| 20 |
|
| 21 |
// instantiate a file IO object |
| 22 |
FileIO fio; |
| 23 |
|
| 24 |
// open a file for reading in BINARY mode |
| 25 |
fio.open( filename, IO.READ | IO.BINARY ); |
| 26 |
// ensure it's ok |
| 27 |
if( !fio.good() ) |
| 28 |
{ |
| 29 |
// print error message |
| 30 |
cherr <= "can't open file: " <= filename <= " for reading..." |
| 31 |
<= IO.newline(); |
| 32 |
// bail |
| 33 |
me.exit(); |
| 34 |
} |
| 35 |
|
| 36 |
// variable to read into |
| 37 |
int val; |
| 38 |
// header |
| 39 |
"0123456789abcdefghijklmnopqrstuvwxyzABCD" @=> string header; |
| 40 |
|
| 41 |
// .wav header details: |
| 42 |
// struct wavhdr { |
| 43 |
// char riff[4]; /* "RIFF" */ |
| 44 |
// int flength; /* file length in bytes */ |
| 45 |
// char wave[4]; /* "WAVE" */ |
| 46 |
// char fmt[4]; /* "fmt " */ |
| 47 |
// int block_size; /* in bytes (generally 16) */ |
| 48 |
// short format_tag; /* 1=PCM, 257=Mu-Law, 258=A-Law, 259=ADPCM */ |
| 49 |
// short num_chans; /* 1=mono, 2=stereo */ |
| 50 |
// int srate; /* Sampling rate in samples per second */ |
| 51 |
// int bytes_per_sec; /* bytes per second */ |
| 52 |
// short bytes_per_samp; /* 2=16-bit mono, 4=16-bit stereo */ |
| 53 |
// short bits_per_samp; /* Number of bits per sample */ |
| 54 |
// char data[4]; /* "data" */ |
| 55 |
// int dlength; /* data length in bytes (filelength - 44) */ |
| 56 |
// }; |
| 57 |
|
| 58 |
// read in the WAV file header, field by field |
| 59 |
// FYI since chuck-1.5.0.1, in binary mode, readInt() for IO.INT8, |
| 60 |
// IO.INT16, or IO.INT32 by default returns unsigned integers; to read |
| 61 |
// signed integers, call readInt() with IO.SINT8, IO.SINT16, or IO.SINT32 |
| 62 |
// e.g., IO.SINT16 is used below when reading signed 16-bits samples |
| 63 |
|
| 64 |
// "RIFF" |
| 65 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(0,val); |
| 66 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(1,val); |
| 67 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(2,val); |
| 68 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(3,val); |
| 69 |
// file length |
| 70 |
fio.readInt( IO.INT32 ) => int flength; |
| 71 |
// "WAVE" |
| 72 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(8,val); |
| 73 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(9,val); |
| 74 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(10,val); |
| 75 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(11,val); |
| 76 |
// "fmt " |
| 77 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(12,val); |
| 78 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(13,val); |
| 79 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(14,val); |
| 80 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(15,val); |
| 81 |
// file info |
| 82 |
fio.readInt( IO.INT32 ) => int blockSize; |
| 83 |
fio.readInt( IO.INT16 ) => int format; |
| 84 |
fio.readInt( IO.INT16 ) => int numChannels; |
| 85 |
fio.readInt( IO.INT32 ) => int sRate; |
| 86 |
fio.readInt( IO.INT32 ) => int bytesPerSecond; |
| 87 |
fio.readInt( IO.INT16 ) => int bytesPerSample; |
| 88 |
fio.readInt( IO.INT16 ) => int bitsPerSample; |
| 89 |
// "data" |
| 90 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(36,val); |
| 91 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(37,val); |
| 92 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(38,val); |
| 93 |
fio.readInt( IO.INT8 ) => val; header.setCharAt(39,val); |
| 94 |
// size of data region in bytes |
| 95 |
fio.readInt( IO.INT32 ) => int dLength; |
| 96 |
|
| 97 |
<<< "***", header, "***" >>>; |
| 98 |
<<< "---------------------", "" >>>; |
| 99 |
<<< "File Length =", flength >>>; |
| 100 |
<<< "Block Size =", blockSize >>>; |
| 101 |
<<< "Format =", format >>>; |
| 102 |
<<< "Num Chans =", numChannels >>>; |
| 103 |
<<< "Sample Rate =", sRate >>>; |
| 104 |
<<< "Bytes/Sec =", bytesPerSecond >>>; |
| 105 |
<<< "Bytes/Sample =", bytesPerSample >>>; |
| 106 |
<<< "Bits/Sample =", bitsPerSample >>>; |
| 107 |
<<< "Data Length =", dLength >>>; |
| 108 |
<<< "---------------------", "" >>>; |
| 109 |
|
| 110 |
// check |
| 111 |
if( numChannels > 1 ) |
| 112 |
{ |
| 113 |
// print error message |
| 114 |
<<< "WAV file contains", numChannels, "channels..." >>>; |
| 115 |
<<< "this examples only supports mono WAV files...", "" >>>; |
| 116 |
// bail |
| 117 |
me.exit(); |
| 118 |
} |
| 119 |
|
| 120 |
// render the waveform |
| 121 |
Impulse imp => dac; |
| 122 |
// number of samples |
| 123 |
dLength / bytesPerSample => int numSamples; |
| 124 |
// array |
| 125 |
float samples[numSamples]; |
| 126 |
// sample number |
| 127 |
0 => int n; |
| 128 |
|
| 129 |
// loop until end |
| 130 |
while( n < numSamples ) |
| 131 |
// while( fio.more() ) |
| 132 |
// (^ this may not always work; WAV files may contain debris beyond data region) |
| 133 |
{ |
| 134 |
// read the next sample: signed 16-bit integer |
| 135 |
fio.readInt( IO.SINT16 ) => int sample; |
| 136 |
// map sample value from [-32768,32767] to [-1,1] |
| 137 |
Math.remap(sample, -32768, 32767, -1, 1) => samples[n++] => imp.next; |
| 138 |
// print dot every 1000 samples |
| 139 |
if( n % 1000 == 0 ) { |
| 140 |
chout <= "."; chout.flush(); |
| 141 |
} |
| 142 |
// print marker every second |
| 143 |
if( n % sRate == 0 ) { |
| 144 |
chout <= "|"; chout.flush(); |
| 145 |
} |
| 146 |
|
| 147 |
// advance one sample |
| 148 |
samp => now; |
| 149 |
} |