samer@46
|
1 % wavsink - file reader implementation using ishara.audio.StreamSink
|
samer@0
|
2 %
|
samer@0
|
3 % wavsink ::
|
samer@0
|
4 % (unit -> (InputStream, unit -> unit)) ~'function to open stream',
|
samer@0
|
5 % (unit -> string) ~'function to create character representation',
|
samer@0
|
6 % options {
|
samer@0
|
7 % channels :: natural/nan ~'desired number of channels';
|
samer@0
|
8 % rate :: nonneg/nan ~'desired sampling rate';
|
samer@0
|
9 % bits :: natural/16 ~'desired bits per sample';
|
samer@0
|
10 % }
|
samer@0
|
11 % -> signal(C,R).
|
samer@0
|
12 %
|
samer@0
|
13 % If channels or rate are not nan, audio format will be converted to match.
|
samer@0
|
14 % If either of them are nan, the corresponding value from the audio file will
|
samer@0
|
15 % be left unchanged.
|
samer@0
|
16
|
samer@0
|
17 classdef wavsink < sink
|
samer@0
|
18 properties (GetAccess=private, SetAccess=immutable)
|
samer@0
|
19 streamfn
|
samer@0
|
20 stringfn
|
samer@0
|
21 format
|
samer@0
|
22 end
|
samer@0
|
23
|
samer@0
|
24 methods
|
samer@0
|
25 function s=wavsink(ch,rate,streamfn,stringfn,varargin)
|
samer@37
|
26 opts=options('bits',16,varargin{:});
|
samer@0
|
27 s.streamfn=streamfn;
|
samer@0
|
28 s.stringfn=stringfn;
|
samer@0
|
29 s.format=audio_format(ch,rate,opts.bits);
|
samer@0
|
30 end
|
samer@0
|
31
|
samer@0
|
32 function s=tostring(sig), s=sig.stringfn(); end
|
samer@0
|
33 function c=channels(s), c=s.format.getChannels(); end
|
samer@0
|
34 function r=rate(s), r=s.format.getSampleRate(); end
|
samer@0
|
35 function s=construct(sig)
|
samer@46
|
36 import ishara.audio.*;
|
samer@0
|
37
|
samer@0
|
38 [str,cleanup]=sig.streamfn();
|
samer@0
|
39 snk=StreamSink(StreamSink.wavOutputStream(str, sig.format));
|
samer@0
|
40 snk.setScale(0.999);
|
samer@0
|
41 ref=disposables('reg',snk);
|
samer@0
|
42 s.start = @()snk.start();
|
samer@0
|
43 s.stop = @()snk.stop();
|
samer@0
|
44 s.writer = @writer;
|
samer@0
|
45 s.dispose = @dispose;
|
samer@0
|
46
|
samer@0
|
47 function r=writer(n)
|
samer@0
|
48 ch=snk.getFormat.getChannels();
|
samer@0
|
49 wr=snk.writer(n*ch);
|
samer@0
|
50 r=@next;
|
samer@0
|
51 function rem=next(x), rem=wr.write(x(:))/ch; end
|
samer@0
|
52 end
|
samer@0
|
53 function dispose
|
samer@0
|
54 disposables('dereg',ref);
|
samer@0
|
55 fprintf('Closing WAV output stream.\n');
|
samer@0
|
56 snk.dispose();
|
samer@0
|
57 cleanup();
|
samer@0
|
58 end
|
samer@0
|
59 end
|
samer@0
|
60
|
samer@0
|
61 end
|
samer@0
|
62 end
|
samer@0
|
63
|
samer@0
|
64 function str=austream(str)
|
samer@0
|
65 if ~str.markSupported, str=java.io.BufferedInputStream(str); end
|
samer@0
|
66 str=javax.sound.sampled.AudioSystem.getAudioInputStream(str);
|
samer@0
|
67 end
|