~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/audio/saveaudio.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006,
 
2
##               2007 John W. Eaton
 
3
##
 
4
## This file is part of Octave.
 
5
##
 
6
## Octave is free software; you can redistribute it and/or modify it
 
7
## under the terms of the GNU General Public License as published by
 
8
## the Free Software Foundation; either version 3 of the License, or (at
 
9
## your option) any later version.
 
10
##
 
11
## Octave is distributed in the hope that it will be useful, but
 
12
## WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
## General Public License for more details.
 
15
##
 
16
## You should have received a copy of the GNU General Public License
 
17
## along with Octave; see the file COPYING.  If not, see
 
18
## <http://www.gnu.org/licenses/>.
 
19
 
 
20
## -*- texinfo -*-
 
21
## @deftypefn {Function File} {} saveaudio (@var{name}, @var{x}, @var{ext}, @var{bps})
 
22
## Saves a vector @var{x} of audio data to the file
 
23
## @file{@var{name}.@var{ext}}.  The optional parameters @var{ext} and
 
24
## @var{bps} determine the encoding and the number of bits per sample used
 
25
## in the audio file (see @code{loadaudio});  defaults are @file{lin} and
 
26
## 8, respectively.
 
27
## @seealso{lin2mu, mu2lin, loadaudio, playaudio, setaudio, record}
 
28
## @end deftypefn
 
29
 
 
30
## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
 
31
## Created: 5 September 1994
 
32
## Adapted-By: jwe
 
33
 
 
34
function saveaudio (name, X, ext, bit)
 
35
 
 
36
  if (nargin < 2 || nargin > 4)
 
37
    print_usage ();
 
38
  endif
 
39
 
 
40
  if (nargin == 2)
 
41
    ext = "lin";
 
42
  endif
 
43
 
 
44
  if (nargin < 4)
 
45
    bit = 8;
 
46
  elseif (bit != 8 && bit != 16)
 
47
    error ("saveaudio: bit must be either 8 or 16");
 
48
  endif
 
49
 
 
50
  [nr, nc] = size (X);
 
51
  if (nc != 1)
 
52
    if (nr == 1)
 
53
      X = X';
 
54
      nr = nc;
 
55
    else
 
56
      error ("saveaudio: X must be a vector");
 
57
    endif
 
58
  endif
 
59
 
 
60
  num = fopen ([name, ".", ext], "wb");
 
61
 
 
62
  if (strcmp (ext, "lin") || strcmp (ext, "raw"))
 
63
    if (bit == 8)
 
64
      ld = max (abs (X));
 
65
      if (ld > 127)   # convert 16 to 8 bit
 
66
        if (ld < 16384)
 
67
          sc = 64 / ld;
 
68
        else
 
69
          sc = 1 / 256;
 
70
        endif
 
71
        X = fix (X * sc);
 
72
      endif
 
73
      X = X + 127;
 
74
      c = fwrite (num, X, "uchar");
 
75
    else
 
76
      c = fwrite (num, X, "short");
 
77
    endif
 
78
  elseif (strcmp (ext, "mu") || strcmp (ext, "au")
 
79
          || strcmp (ext, "snd") || strcmp (ext, "ul"))
 
80
    Y = lin2mu (X);
 
81
    c = fwrite (num, Y, "uchar");
 
82
  else
 
83
    fclose (num);
 
84
    error ("saveaudio does not support given extension");
 
85
  endif
 
86
 
 
87
  fclose (num);
 
88
 
 
89
endfunction