1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
** Copyright (C) 2007 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <octave/oct.h>
#include "sndfile.h"
#include "format.h"
#define FOUR_GIG (0x100000000LL)
#define BUFFER_FRAMES 8192
DEFUN_DLD (sfread, args, nargout , "\
-*- texinfo -*-\n\
@deftypefn {Function File} {@var{I},@var{srate},@var{format} =} sfread (@var{filename})\n\
Read a sound file from disk using libsndfile.\n\
\n\
@seealso{wavread}\n\
@end deftypefn\n\
")
{ SNDFILE * file ;
SF_INFO sfinfo ;
octave_value_list retval ;
int nargin = args.length () ;
/* Bail out if the input parameters are bad. */
if ((nargin != 1) || !args (0) .is_string () || nargout < 1 || nargout > 3)
{ print_usage () ;
return retval ;
} ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
std::string filename = args (0).string_value () ;
if ((file = sf_open (filename.c_str (), SFM_READ, &sfinfo)) == NULL)
{ error ("sfread: couldn't open file %s : %s", filename.c_str (), sf_strerror (NULL)) ;
return retval ;
} ;
if (sfinfo.frames > FOUR_GIG)
printf ("This is a really huge file (%lld frames).\nYou may run out of memory trying to load it.\n", sfinfo.frames) ;
dim_vector dim = dim_vector () ;
dim.resize (2) ;
dim (0) = sfinfo.frames ;
dim (1) = sfinfo.channels ;
/* Should I be using Matrix instead? */
NDArray out (dim, 0.0) ;
float buffer [BUFFER_FRAMES * sfinfo.channels] ;
int readcount ;
sf_count_t total = 0 ;
do
{ readcount = sf_readf_float (file, buffer, BUFFER_FRAMES) ;
/* Make sure we don't read more frames than we allocated. */
if (total + readcount > sfinfo.frames)
readcount = sfinfo.frames - total ;
for (int ch = 0 ; ch < sfinfo.channels ; ch++)
{ for (int k = 0 ; k < readcount ; k++)
out (total + k, ch) = buffer [k * sfinfo.channels + ch] ;
} ;
total += readcount ;
} while (readcount > 0 && total < sfinfo.frames) ;
retval.append (out.squeeze ()) ;
if (nargout >= 2)
retval.append ((octave_uint32) sfinfo.samplerate) ;
if (nargout >= 3)
{ std::string fmt ("") ;
string_of_format (fmt, sfinfo.format) ;
retval.append (fmt) ;
} ;
/* Clean up. */
sf_close (file) ;
return retval ;
} /* sfread */
|