view audio/private/pipein.m @ 42:ae596261e75f

Various fixes and development to audio handling
author samer
date Tue, 02 Dec 2014 14:51:13 +0000
parents 672052bd81f8
children 62e31e7980e6
line wrap: on
line source
function [str,cleanup]=pipein(cmd,q)
	import java.io.*;

	if nargin<2, q=false; end
	if ~q, fprintf('Starting sub-process: %s\n',cmd); end

	% OK, here's the deal: the line below doesn't work if Matlab and
	% Java do not agree about their default character encoding, which
	% can happen if you change Matlab's character encoding with, eg
	% >> feature('DefaultCharacterSet','UTF-8')
	% As far as a I can tell, you cannot change the character encoding
	% using by Runtime.exec() for command line arguments once the JVM
	% has been started, and, somewhat perversely, Matlab ingores the 
	% value of the LANG environment variable on startup, preferring to
	% us the encoding specified in $MATLAB_ROOT/bin/lcdata.xml

	% NOT GOOD:
	% process=java.lang.Runtime.getRuntime().exec({'bash','-c',cmd});

	% this is what we're going to do instead: start bash with no
	% arguments to make it read from standard input, then write cmd 
	% with proper encoding to that stream.

	% BETTER:
	cs=feature('DefaultCharacterSet'),
	process=java.lang.Runtime.getRuntime().exec('bash');
	writer=OutputStreamWriter(process.getOutputStream(),cs);
	writer.write(cmd); writer.close();

	str=process.getInputStream();
	cleanup=@dispose;

	function dispose
		if ~q, fprintf('Killing subprocess...\n'); end
		process.destroy();
	end
end