Mercurial > hg > octave-nkf
changeset 1636:21fe2afb3692
[project @ 1995-11-16 19:16:11 by jwe]
Initial revision
author | jwe |
---|---|
date | Thu, 16 Nov 1995 19:16:11 +0000 |
parents | e91ac27650dc |
children | edfc235ced97 |
files | scripts/audio/Makefile.in scripts/audio/lin2mu.m scripts/audio/loadaudio.m scripts/audio/mu2lin.m scripts/audio/playaudio.m scripts/audio/record.m scripts/audio/saveaudio.m scripts/audio/setaudio.m |
diffstat | 8 files changed, 419 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/audio/Makefile.in @@ -0,0 +1,73 @@ +# +# Makefile for octave's scripts/audio directory +# +# John W. Eaton +# jwe@bevo.che.wisc.edu +# University of Wisconsin-Madison +# Department of Chemical Engineering + +TOPDIR = ../.. + +script_sub_dir = audio + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ + +include $(TOPDIR)/Makeconf + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_DATA = @INSTALL_DATA@ + +SOURCES = *.m + +DISTFILES = Makefile.in $(SOURCES) + +FCN_FILES = $(wildcard $(srcdir)/*.m) +FCN_FILES_NO_DIR = $(notdir $(FCN_FILES)) + +all: +.PHONY: all + +install: + $(top_srcdir)/mkinstalldirs $(fcnfiledir)/$(script_sub_dir) + for f in $(FCN_FILES_NO_DIR) ; do \ + rm -f $(fcnfiledir)/$(script_sub_dir)/$$f ; \ + $(INSTALL_DATA) $(srcdir)/$$f $(fcnfiledir)/$(script_sub_dir)/$$f ; \ + done +.PHONY: install + +uninstall: + for f in $(FCN_FILES_NO_DIR) ; \ + do rm -f $(fcnfiledir)/$(script_sub_dir)/$$f ; \ + done +.PHONY: uninstall + +clean: +.PHONY: clean + +tags: $(SOURCES) + ctags $(SOURCES) + +TAGS: $(SOURCES) + etags $(SOURCES) + +mostlyclean: clean +.PHONY: mostlyclean + +distclean: clean + rm -f Makefile +.PHONY: distclean + +maintainer-clean: distclean + rm -f tags TAGS +.PHONY: maintainer-clean + +local-dist: + ln $(DISTFILES) ../../`cat ../../.fname`/scripts/audio +.PHONY: local-dist + +dist: + ln $(DISTFILES) ../../`cat ../../.fname`/scripts/audio +.PHONY: dist
new file mode 100644 --- /dev/null +++ b/scripts/audio/lin2mu.m @@ -0,0 +1,36 @@ +function y = lin2mu (x) + +# usage: y = lin2mu (x) +# +# x is a vector of an 8- or 16-bit linearly encoded audio sample, +# This is transformed into a mu-law encoded vector. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Oct 17, 1994 +# Copyright Department of Probability Theory and Statistics TU Wien + + if (nargin != 1) + usage ("y = lin2mu (x)"); + endif + + if (! is_vector (x)) + error ("lin2mu: x must be a vector"); + endif + + # transform 8-bit format to 16-bit + if (max (abs (x)) <= 128) + x = 256 .* x; + endif + + # determine sign of x, set sign(0) = 1. + sig = sign(x) + (x == 0); + + # take absolute value of x, but force it to be smaller than 32636; + # add bias + x = min (abs (x), 32635 * ones (size (x))) + 132; + + # find exponent and fraction of bineary representation + [f, e] = log2 (x); + + y = 64 * sig - 16 * e - fix (32 * f) + 335; + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/audio/loadaudio.m @@ -0,0 +1,63 @@ +function X = loadaudio (name, ext, bit) + +# usage: X = loadaudio (name [, ext [, bit]]) +# +# Loads audio data from the file "name.ext" into the data vector X. +# Default value for the "ext" argument, which has to be written +# without the initial ".", is "lin". +# Currently, the following audio formats are supported: +# *) mu-law encoding with extension "mu", "au" or "snd" +# *) linear encoding with extension "lin" or "raw" +# +# The `bit' argument can be either 8 (default) or 16. +# Depending on the value of bit, linearly encoded files are +# interpreted as being in 8 and 16 bit format, respectively, and +# mu-law encoded files are transformed to 8 and 16-bit linear +# format, respectively. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Apr 10, 1994 +# Last modified by AW on Oct 29, 1994 + + if (nargin == 0 || nargin > 3) + usage ("loadaudio (name [, ext [, bit]])"); + endif + + if (nargin == 1) + ext = "lin"; + endif + + if (nargin < 3) + bit = 8; + elseif (bit != 8 && bit != 16) + error ("loadaudio: bit must be either 8 or 16"); + endif + + name = [name, ".", ext]; + num = fopen (name, "r"); + + if (strcmp (ext, "lin") || strcmp (ext, "raw")) + if (bit == 8) + [Y, c] = fread (num, inf, "uchar"); + X = Y - 127; + else + [X, c] = fread (num, inf, "short"); + endif + elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd")) + [Y, c] = fread (num, inf, "uchar"); + # remove file header + m = max (find (Y(1:64) == 0)); + if (! isempty (m)) + Y(1:m) = []; + endif + X = mu2lin (Y, bit); + else + fclose (num); + error ("loadaudio does not support given extension"); + endif + + fclose (num); + +endfunction + + +
new file mode 100644 --- /dev/null +++ b/scripts/audio/mu2lin.m @@ -0,0 +1,54 @@ +function y = mu2lin (x, bit) + +# usage: y = mu2lin (x [, bit]) +# +# If x is a vector of audio data with mu-law encoding, mu2lin (x) +# holds the same data with linear encoding. +# The optional argument bit specifies whether the input data is +# 8 bit (default) or 16 bit. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Oct 18, 1994 +# Updated by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Oct 27, 1994 +# Copyright Department of Probability Theory and Statistics TU Wien + + if (nargin == 1) + bit = 8; + elseif (nargin == 2) + if (bit != 8 && bit != 16) + error ("mu2lin: bit must be either 8 or 16"); + endif + else + usage ("y = mu2lin (x [, bit])"); + endif + + if (! is_vector (x)) + error ("mu2lin: x must be a vector"); + endif + + exp_lut = [0; 132; 396; 924; 1980; 4092; 8316; 16764]; + + # invert x bitwise + x = 255 - x; + + # determine sign of y + sig = (x > 127); + + # determine exponent and fraction of y + e = fix (x / 16) - 8 .* sig + 1; + f = rem (x, 16); + + sig = 1 - 2 .* sig; + y = (pow2 (f, e + 2) + exp_lut (e)) .* sig; + + # convert to 8-bit + if (bit == 8) + ld = max (abs (y)); + if (ld < 16384) + sc = 64 / ld; + else + sc = 1 / 256; + endif + y = fix (y * sc); + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/audio/playaudio.m @@ -0,0 +1,62 @@ +function playaudio (name, ext) + +# usage: playaudio (name [, ext]) +# playaudio (X) +# +# `playaudio ("name" [, "ext"])' plays the audio file "name.ext". The +# default value for the "ext" argument, which has to be written +# without the initial ".", is "lin". +# Currently, the following audio formats are suppored: +# *) linear encoding with extension "lin" or "raw", played using +# /dev/dsp +# *) mu-law encoding with extension "mu", "au" or "snd", played +# using /dev/audio +# +# `playaudio (X)' plays the audio data contained in the vector X. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Apr 11, 1994 +# Last modified by AW on Nov 7, 1994 +# Copyright Dept of Probability Theory and Statistics TU Wien + + file = octave_tmp_file_name (); + + usage_msg = "playaudio (name [, ext]) or playaudio (X)"; + + if (nargin == 1 && is_vector (name) && ! isstr (name)) + # play a vector + [nr, nc] = size (name); + if (nc != 1) + if (nr == 1) + name = name'; + nr = nc; + else + error ("playaudio: X must be a vector"); + endif + endif + X = name + 127; + num = fopen (file, "w"); + c = fwrite (num, X, "uchar"); + fclose (num); + system (sprintf ("cat %s > /dev/dsp", file)); + unlink (file); + elseif (nargin >= 1 && isstr (name)) + # play a file + if (nargin == 1) + name = [name, ".lin"]; + elseif (nargin == 2) + name = [name, ".", ext]; + else + usage (usage_msg); + endif + if (strcmp (ext, "lin") || strcmp (ext, "raw")) + system (sprintf ("cat %s > /dev/dsp", name)); + elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd")) + system (sprintf ("cat %s > /dev/audio", name)); + else + error ("playaudio does not support given extension"); + endif + else + usage (usage_msg); + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/audio/record.m @@ -0,0 +1,39 @@ +function X = record (sec, sampling_rate) + +# usage: X = record (sec [, sampling_rate]) +# +# Records sec seconds of audio into the vector X. +# The default value for the sampling_rate is 8000, ie. 8kHz. +# The program waits for you to hit the ENTER key, then the recording +# starts immediatly. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Sep 19, 1994 +# Last modified by AW on Oct 5, 1994 +# Copyright Dept of Probability Theory and Statistics TU Wien + + if (nargin == 1) + sampling_rate = 8000; + elseif (nargin != 2) + usage ("X = record (sec [, sampling_rate])"); + endif + + file = octave_tmp_file_name (); + + input ("Please hit ENTER and speak afterwards!\n", 1); + + cmd = sprintf ("dd if=/dev/dsp of=%s bs=%d count=%d", + file, sampling_rate, sec) + + system (cmd); + + num = fopen (file, "r"); + + [Y, c] = fread (num, sampling_rate * sec, "uchar"); + + fclose (num); + + unlink (file); + + X = Y - 127; + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/audio/saveaudio.m @@ -0,0 +1,71 @@ +function saveaudio (name, X, ext, bit) + +# usage: saveaudio (name, X, [, ext [, bit]]) +# +# Saves a vector X of audio data in the file "name.ext". +# The format of the audio file is determined by ext which has to be +# written without an inital "."; default value for ext is "lin". +# +# Currently, the following audio formats are supported: +# *) mu-law files with extension "mu", "au" or "snd" +# *) linearly encoded files with extension "lin" or "raw" +# If the data is saved linearly, the bit argument decides whether an +# 8-bit (default) or a 16-bit format is used. + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Sep 5, 1994 +# Last modified by AW on Oct 29, 1994 +# Copyright Dept of Probability Theory and Statistics TU Wien + + if (nargin < 2 || nargin > 4) + usage ("saveaudio (X, name [, ext [, bit]])"); + endif + + if (nargin == 2) + ext = "lin"; + endif + + if (nargin < 4) + bit = 8; + elseif (bit != 8 && bit != 16) + error ("saveaudio: bit must be either 8 or 16"); + endif + + [nr, nc] = size (X); + if (nc != 1) + if (nr == 1) + X = X'; + nr = nc; + else + error ("saveaudio: X must be a vector."); + endif + endif + + num = fopen ([name, ".", ext], "w"); + + if (strcmp (ext, "lin") || strcmp (ext, "raw")) + if (bit == 8) + ld = max (abs (X)); + if (ld > 127) # convert 16 to 8 bit + if (ld < 16384) + sc = 64 / ld; + else + sc = 1 / 256; + endif + X = fix (X * sc); + endif + X = X + 127; + c = fwrite (num, X, "uchar"); + else + c = fwrite (num, X, "short"); + endif + elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd")) + Y = lin2mu (X); + c = fwrite (num, Y, "uchar"); + else + fclose (num); + error ("saveaudio does not support given extension"); + endif + + fclose (num); + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/audio/setaudio.m @@ -0,0 +1,21 @@ +function setaudio (w_type, value) + +# usage: setaudio ([w_type [, value]]) +# +# executes the shell command `mixer [w_type [, value]]' + +# Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Oct 5, 1994 +# Updated by AW on Nov 3, 1994 +# Copyright Department of Probability Theory and Statistics TU Wien + + if (nargin == 0) + system ("mixer"); + elseif (nargin == 1) + system (sprintf ("mixer %s", w_type)); + elseif (nargin == 2) + system (sprintf ("mixer %s %d", w_type, value)); + else + usage ("setaudio ([w_type [, value]])"); + endif + +endfunction