Commit 79e33cc2 authored by Mark Hymers's avatar Mark Hymers

Add first pywavplay octave/linux version

Signed-off-by: Mark Hymers's avatarMark Hymers <mark.hymers@hankel.co.uk>
parents
*.mex
*.o
pawavplay_alsa.mex: pawavplay_alsa.cpp
mkoctfile --mex pawavplay_alsa.cpp -lportaudio
.PHONY: clean
clean:
rm -f pawavplay_alsa.mex
PAWavPlay for Octave on Linux
=============================
This is a modified version of the original pawavplay (found at
https://github.com/jgdsens/pa-wavplay/) which has been modified
to support playing audio on Linux when using Octave rather
than MATLAB (although it should work with MATLAB too, it is
just untested). The license is as-per the original code.
This has been tested using an built-in Intel HDA sound chipset
on the developer's Thinkpad X260 as well as with a
USB-attached Komplete Audio 6 sound card (Only the first
four channels could be tested by the author due to lack
of a SPDIF device to receive channels 5 and 6), but all 6
should work.
For any queries, please contact:
Mark Hymers `<mark.hymers@ynic.york.ac.uk>`
Compiling
---------
To compile for Octave on Linux, simply run:
```
make
```
This will produce the file `pawavplay_alsa.mex`.
To build, you will require the `mkoctfile` command and the
Portaudio library and development headers.
On Debian (and derived distributions), you can ensure
that you have these dependencies by running:
```
apt-get install liboctave-dev portaudio19-dev
```
Note that you may need to run this as root (using `sudo` or
by changing to root using `su`).
Usage
-----
It works as the original, except that the only supported
device type is 'alsa'. So, to list your available devices,
use:
```
pa_wavplay('alsa')
```
This will list your devices and their IDs.
To play audio, use:
```
pa_wavplay(buffer, [samplerate], [deviceid], [devicetype]);
```
To record audio (currently untested), use:
```
inputbuffer = pa_wavrecord(channels, nsamples, [samplerate], [deviceid], [devicetype])
```
To play and record audio simultaneously (currently untested):
```
inputbuffer = pa_wavplayrecord(playbuffer,[playdevice],[samplerate], [recnsamples], [recchannels], [recdevice],[devicetype])
```
see help for each of these commands for much more information.
function pa_wavplay(varargin)
% pa_wavplay: playback a matrix to multichannel sound hardware
%
% Usage: pa_wavplay([devicetype])
% pa_wavplay(buffer, [samplerate], [deviceid], [devicetype])
%
% pa_wavplay is a tool for playing multi-channel audio through your ASIO
% device. Arguments in [] are optional
%
% pa_wavplay([devicetype]) will list all your audio devices for that
% device type then exit.
%
% - buffer is the matrix to play
% - samplerate is the sampling frequency. Default: 44100
% - deviceid is the device id to use for output. Default: 0
% - devicetype determines which sound driver to use
% 'win' Windows Multimedia Device
% 'dx' DirectX DirectSound driver
% 'wasapi' Windows WASAPI Device
% 'asio' ASIO Driver (default)
%
% For stereo playback, buffer should be an N-by-2 matrix. The number of
% audio channels supported is hardware dependent.
%
% samplerate should be a valid integer sample rate, eg. 22050 44100 etc.
%
% As of pa_wavplay 0.2, buffer should be either of type double or
% single. pa_wavplay uses 32-bit floats (single) internally.
%
% SEE ALSO: pa_wavrecord, pa_wavplayrecord
%
% Copyright 2014 Sensimetrics Corp.
% For additional Matlab tools for audio and video, consult www.sens.com
% check right num of args
error(nargchk(0,4,nargin));
% defaults
device_opt = 1;
device = 0;
fs = 44100;
% no args, print devices for asio
if (nargin==0)
pawavplay_alsa;
return;
end
% if devtype specified
if ischar(varargin{end}),
s=varargin{end};
varargin(end)=[];
if (nargin==1)
pawavplay_alsa;
return;
end
end
% data buffer
y = varargin{1};
% sample rate
if length(varargin)>=2,
fs=varargin{2};
end
% device id
if length(varargin) >= 3,
if (~(ischar(varargin{3})))
device = varargin{3};
end
end
fprintf('Using ALSA driver\n');
pawavplay_alsa(y, device, fs, 0, 0, 0, -1);
% [EOF] pa_wavplay.m
function y = pa_wavplayrecord(varargin)
% pa_wavplayrecord: simultaneous playback & record of multichannel sound
%
% Usage:
% inputbuffer = pa_wavplayrecord(playbuffer,[playdevice],[samplerate],
% [recnsamples], [recfirstchannel], [reclastchannel],
% [recdevice], [devicetype])
%
% pa_wavplayrecord is a tool for playing and recording multi-channel
% audio through your ASIO device. Arguments in [] are optional
%
% pa_wavplayrecord([devicetype]) will list all your audio devices for
% that device type then exit.
%
% - playbuffer the matrix to play
% - playdevice the device to play it on. Default: 0
% - samplerate the sampling frequency. Default: 44100
% - recnsamples the number of samples to record. Default: 0
% if 0 then we'll record for the duration of playbuffer
% - recfirstchannel the first channel to record from Default: 1
% - reclastchannel the last channel to record from Default: 1
% - recdevice the device to record from. Default: 0
% - devicetype determines which sound driver to use
% 'win' Windows Multimedia Device
% 'dx' DirectX DirectSound driver
% 'wasapi' Windows WASAPI Device
% 'asio' ASIO Driver (default)
%
% See the help for pa_wavplay for a list of some of these arguments,
% and the formatting of them.
%
% SEE ALSO: pa_wavrecord, pa_wavplayrecord
%
% Copyright 2014 Sensimetrics Corp.
% For additional Matlab tools for audio and video, consult www.sens.com
% check right num of args
error(nargchk(1,8,nargin));
% defaults
device_opt = 1;
playdevice = 0;
fs = 44100;
recnsamples = 0;
recfirstchannel = 1;
reclastchannel = 1;
recdevice = 0;
% if devtype specified
if ischar(varargin{end}),
s=varargin{end};
varargin(end)=[];
device_opt = find(strcmp(s,{'asio','win', 'dx', 'wasapi'})==1);
if isempty(device_opt),
error(['Unrecognized DEVICETYPE: ' s]);
end
if (nargin==1)
if device_opt==1, % asio
pawavplaya;
end
if device_opt==2, % win
pawavplayw;
end
if device_opt==3, % dx
pawavplayx;
end
if device_opt==4, % wasapi
pawavplaywasapi;
end
return;
end
end
% data buffer
playbuffer = varargin{1};
% play device
if length(varargin)>=2,
playdevice=varargin{2};
end
% sample rate
if length(varargin)>=3,
fs=varargin{3};
end
% recnsamples
if length(varargin)>=4,
recnsamples=varargin{4};
end
% recfirstchannel
if length(varargin)>=5,
recfirstchannel=varargin{5};
end
% reclastchannel
if length(varargin)>=6,
reclastchannel=varargin{6};
end
% recdevice
if length(varargin)>=7,
recdevice=varargin{7};
end
if device_opt==1, % asio
fprintf('Using ASIO driver\n');
y = pawavplaya(playbuffer, playdevice, fs, recfirstchannel, reclastchannel, recnsamples, recdevice);
elseif device_opt==2, % win
fprintf('Using WMME driver\n');
y = pawavplayw(playbuffer, playdevice, fs, recfirstchannel, reclastchannel, recnsamples, recdevice);
elseif device_opt==3, % dx
fprintf('Using DirectX driver\n');
y = pawavplayx(playbuffer, playdevice, fs, recfirstchannel, reclastchannel, recnsamples, recdevice);
elseif device_opt==4, % wasapi
fprintf('Using WASAPI driver\n');
y = pawavplaywasapi(playbuffer, playdevice, fs, recfirstchannel, reclastchannel, recnsamples, recdevice);
end
fprintf('Converting result to doubles\n');
y = double(y);
% [EOF] pa_wavplayrecord.m
\ No newline at end of file
function y = pa_wavrecord(varargin)
% pa_wavrecord: record from multichannel sound hardware
%
% Usage: pa_wavrecord([devicetype])
% inputbuffer = pa_wavrecord(firstchannel, lastchannel, nsamples, [samplerate],
% [deviceid], [devicetype])
%
% pa_wavrecord is a tool for recording multi-channel audio through your
% ASIO device. Arguments in [] are optional
%
% - firstchannel the first input channel to record from
% - lastchannel the last input channel to record from
% - nsamples the number of samples to record from each channel
% - samplerate the sampling frequency. Default: 44100
% - deviceid the device id to use for INPUT. Default: 0
% - informat the desired data type of inputbuffer. Valid types
% and the number of bits per sample are as follows:
% - devicetype determines which sound driver to use
% 'win' Windows Multimedia Device
% 'dx' DirectX DirectSound driver
% 'wasapi' Windows WASAPI Device
% 'asio' ASIO Driver (default)
% - inputbuffer is a variable that will hold the recorded audio,
% running along rows, with a seperate column for
% each channel
%
% SEE ALSO: pa_wavplay, pa_wavplayrecord
%
% Copyright 2014 Sensimetrics Corp.
% For additional Matlab tools for audio and video, consult www.sens.com
% check right num of args
error(nargchk(0,6,nargin));
% defaults
device_opt = 1;
device = 0;
fs = 44100;
%in_opt = 2;
% no args, print devices for asio
if (nargin==0)
pawavplaya;
return;
end
% if devtype specified
if ischar(varargin{end})
s=varargin{end};
varargin(end)=[];
device_opt = find(strcmp(s,{'asio','win', 'dx', 'wasapi'})==1);
if isempty(device_opt),
error(['Unrecognized DEVICETYPE: ' s]);
end
if (nargin==1)
if device_opt==1, % asio
pawavplaya;
end
if device_opt==2, % win
pawavplayw;
end
if device_opt==3, % dx
pawavplayx;
end
if device_opt==4, % wasapi
pawavplaywasapi;
end
return;
end
end
%{
% if informat specified
if ischar(varargin{end})
s=varargin{end};
varargin(end)=[];
in_opt = strmatch(s,{'int16','double'});
if isempty(in_opt),
error(['Unrecognized informat: ' s]);
end
end
%}
% channels
firstchannel = varargin{1};
lastchannel = varargin{2};
% samples
nsamples = varargin{3};
% sample rate
if length(varargin)>=4,
fs=varargin{4};
end
% device id
if length(varargin) >= 5,
device = varargin{5};
end
if device_opt==1, % asio
fprintf('Using ASIO driver\n');
y = pawavplaya(0, -1, fs, firstchannel, lastchannel, nsamples, device);
elseif device_opt==2, % win
fprintf('Using WMME driver\n');
y = pawavplayw(0, -1, fs, firstchannel, lastchannel, nsamples, device);
elseif device_opt==3, % dx
fprintf('Using DirectX driver\n');
y = pawavplayx(0, -1, fs, firstchannel, lastchannel, nsamples, device);
elseif device_opt==4, % wasapi
fprintf('Using WASAPI driver\n');
y = pawavplaywasapi(0, -1, fs, firstchannel, lastchannel, nsamples, device);
end
fprintf('Converting result to doubles\n');
y = double(y);
% [EOF] pa_wavrecord.m
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment