On 21.01.19 12:48, Roman Haefeli wrote:
> On Mon, 2019-01-21 at 10:03 +0100, Antoine Rousseau wrote:
>>> make an addition to soundfiler to read binary characters into an
>>> array.
>>
>>  wouldn't "read -raw 0 1 1 n" work?
> 
> It would be cool if -raw would allow 1 byte per sample. But even if it
> worked this way, the result would be scaled to the range of -1 to 1,
> which is not so handy. A way to read bytes represented in the array as
> numbers in the range of 0 to 255 would make [soundfiler] really useful
> for dealing with binary files.


attached is a vanilla abstraction to read binary files like
[mrpeach/binfile].

it allows both [read <filename>( and [read <filename> <maxsize>(.

caveats:
- if the file-size is not a multiple of 2, the last byte is omitted!
- when using the <maxsize> feature, you get an ugly error-like warning
on the Pd-console.
- it's not super-efficient. especially it reads the entire (up to
<maxsize> bytes) file into memory twice. so it's probably not a good
idea to use this with large files.
it also outputs the entire file as a list of bytes (as opposed to
[mrpeach/binfile] which just gives you have to bang to get the next byte).

gasdmr
IOhannes
#N canvas 520 385 274 318 10;
#X obj 47 95 unpack s 0;
#X obj 47 72 pack s -1;
#N canvas 5 51 450 595 16bit-to-8bit 0;
#X obj 100 129 until;
#X obj 99 212 list store;
#X obj 100 96 t b b l;
#X obj 100 154 f;
#X obj 138 155 + 1;
#X msg 99 183 get \$1 1;
#X obj 99 293 t f f;
#X obj 154 322 >> 8;
#X obj 100 373 t f;
#X obj 100 326 & 255;
#X obj 154 345 & 255;
#X obj 99 252 * 32768;
#X obj 100 426 list store;
#X msg 100 396 append \$1;
#X msg 45 408 bang;
#X msg 145 127 0;
#X obj 93 65 t b l b;
#X obj 93 32 inlet 16bit/floatnorm;
#X obj 100 449 outlet 8bit/bytenorm;
#X connect 0 0 3 0;
#X connect 1 0 11 0;
#X connect 1 1 0 1;
#X connect 2 0 0 0;
#X connect 2 1 15 0;
#X connect 2 2 1 1;
#X connect 3 0 4 0;
#X connect 3 0 5 0;
#X connect 4 0 3 1;
#X connect 5 0 1 0;
#X connect 6 0 9 0;
#X connect 6 1 7 0;
#X connect 7 0 10 0;
#X connect 8 0 13 0;
#X connect 9 0 8 0;
#X connect 10 0 8 0;
#X connect 11 0 6 0;
#X connect 12 0 18 0;
#X connect 13 0 12 0;
#X connect 14 0 12 0;
#X connect 15 0 3 1;
#X connect 16 0 14 0;
#X connect 16 1 2 0;
#X connect 16 2 12 1;
#X connect 17 0 16 0;
#X restore 47 177 pd 16bit-to-8bit;
#N canvas 5 51 450 300 maxsize 0;
#X obj 167 122 inlet maxsize;
#X obj 100 122 inlet list;
#X obj 100 228 list split;
#X obj 100 251 t a;
#X obj 100 181 t a a;
#X obj 100 204 spigot;
#X obj 173 205 spigot;
#X obj 138 181 != -1;
#X obj 206 184 == -1;
#X obj 167 155 t f f f;
#X obj 100 274 outlet;
#X text 36 85 only output firstN elements (or ALL elements \, if firstN==-1)
;
#X connect 0 0 9 0;
#X connect 1 0 4 0;
#X connect 2 0 3 0;
#X connect 3 0 10 0;
#X connect 4 0 5 0;
#X connect 4 1 6 0;
#X connect 5 0 2 0;
#X connect 6 0 3 0;
#X connect 7 0 5 1;
#X connect 8 0 6 1;
#X connect 9 0 7 0;
#X connect 9 1 2 1;
#X connect 9 2 8 0;
#X restore 47 207 pd maxsize;
#X obj 47 230 outlet;
#N canvas 5 51 450 656 file-to-16bit 0;
#X obj 139 398 array define \$0-sndfile;
#X obj 77 358 soundfiler;
#X obj 77 233 list append \$0-sndfile;
#X floatatom 77 381 5 0 0 0 - - -;
#X obj 50 435 array get \$0-sndfile;
#X msg 50 414 0 -1;
#X obj 255 203 + 1;
#X obj 255 226 / 2;
#X obj 255 252 int;
#X obj 255 275 select 0;
#X msg 300 296 read -maxsize \$1;
#X msg 255 298 read;
#X obj 77 305 list prepend read;
#X obj 77 331 list trim;
#X msg 77 253 -resize -raw 0 1 2 b \$1 \$2;
#X obj 255 321 t a;
#X obj 50 196 t b s;
#X obj 50 141 inlet filename;
#X obj 50 458 outlet 16bit/floatnorm;
#X obj 255 156 inlet maxsize;
#X obj 255 179 t f;
#X text 43 68 read (any) file as 16bit soundfile \, and output samples
;
#X text 45 92 read maxsize bytes at most (actually maxsize+1 \, since
we are reading in pairs);
#X connect 1 0 3 0;
#X connect 2 0 14 0;
#X connect 4 0 18 0;
#X connect 5 0 4 0;
#X connect 6 0 7 0;
#X connect 7 0 8 0;
#X connect 8 0 9 0;
#X connect 9 0 11 0;
#X connect 9 1 10 0;
#X connect 10 0 15 0;
#X connect 11 0 15 0;
#X connect 12 0 13 0;
#X connect 13 0 1 0;
#X connect 14 0 12 0;
#X connect 15 0 12 1;
#X connect 16 0 5 0;
#X connect 16 1 2 0;
#X connect 17 0 16 0;
#X connect 19 0 20 0;
#X connect 20 0 6 0;
#X restore 47 147 pd file-to-16bit;
#X obj 140 121 t f f f;
#X msg 195 121 -1;
#X obj 47 20 inlet;
#X obj 47 43 route read;
#X connect 0 0 5 0;
#X connect 0 1 6 0;
#X connect 1 0 0 0;
#X connect 2 0 3 0;
#X connect 3 0 4 0;
#X connect 5 0 2 0;
#X connect 6 0 5 1;
#X connect 6 1 3 1;
#X connect 6 2 7 0;
#X connect 7 0 1 1;
#X connect 8 0 9 0;
#X connect 9 0 1 0;

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Pd-list@lists.iem.at mailing list
UNSUBSCRIBE and account-management -> 
https://lists.puredata.info/listinfo/pd-list

Reply via email to