Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
audiowmark
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Stefan Westerfeld
audiowmark
Commits
fcd89683
Commit
fcd89683
authored
Feb 07, 2019
by
Stefan Westerfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Report mp3 loading error (if any).
Signed-off-by:
Stefan Westerfeld
<
stefan@space.twc.de
>
parent
c9cb4f7d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
38 deletions
+70
-38
mp3.cc
src/mp3.cc
+39
-27
mp3.hh
src/mp3.hh
+2
-1
testmp3.cc
src/testmp3.cc
+18
-7
wavdata.cc
src/wavdata.cc
+11
-3
No files found.
src/mp3.cc
View file @
fcd89683
...
...
@@ -23,16 +23,34 @@ struct ScopedMHandle
}
};
void
mp3_init
()
{
static
bool
mpg123_init_ok
=
false
;
if
(
!
mpg123_init_ok
)
{
int
err
=
mpg123_init
();
if
(
err
!=
MPG123_OK
)
{
fprintf
(
stderr
,
"audiowmark: init mpg123 lib failed
\n
"
);
exit
(
1
);
}
mpg123_init_ok
=
true
;
}
}
/* there is no really simple way of detecting if something is an mp3
*
* so we try to decode a few frames; if that works without error the
* file is probably a valid mp3
*/
static
bool
bool
mp3_detect
(
const
string
&
filename
)
{
int
err
=
0
;
mp3_init
();
mpg123_handle
*
mh
=
mpg123_new
(
NULL
,
&
err
);
if
(
err
!=
MPG123_OK
)
return
false
;
...
...
@@ -49,6 +67,13 @@ mp3_detect (const string& filename)
smh
.
need_close
=
true
;
long
rate
;
int
channels
;
int
encoding
;
err
=
mpg123_getformat
(
mh
,
&
rate
,
&
channels
,
&
encoding
);
if
(
err
!=
MPG123_OK
)
return
false
;
size_t
buffer_bytes
=
mpg123_outblock
(
mh
);
unsigned
char
buffer
[
buffer_bytes
];
...
...
@@ -72,39 +97,27 @@ mp3_detect (const string& filename)
return
true
;
}
bool
mp3_
try_
load
(
const
string
&
filename
,
WavData
&
wav_data
)
string
mp3_load
(
const
string
&
filename
,
WavData
&
wav_data
)
{
int
err
=
0
;
static
bool
mpg123_init_ok
=
false
;
if
(
!
mpg123_init_ok
)
{
err
=
mpg123_init
();
if
(
err
!=
MPG123_OK
)
return
false
;
mpg123_init_ok
=
true
;
}
const
bool
is_mp3
=
mp3_detect
(
filename
);
if
(
!
is_mp3
)
return
false
;
mp3_init
();
mpg123_handle
*
mh
=
mpg123_new
(
NULL
,
&
err
);
if
(
err
!=
MPG123_OK
)
return
false
;
return
"mpg123_new failed"
;
auto
smh
=
ScopedMHandle
{
mh
};
// cleanup on return
err
=
mpg123_param
(
mh
,
MPG123_ADD_FLAGS
,
MPG123_QUIET
,
0
);
if
(
err
!=
MPG123_OK
)
return
false
;
return
"setting quiet mode failed"
;
// allow arbitary amount of data for resync */
err
=
mpg123_param
(
mh
,
MPG123_RESYNC_LIMIT
,
-
1
,
0
);
if
(
err
!=
MPG123_OK
)
return
false
;
return
"setting resync limit parameter failed"
;
// force floating point output
{
...
...
@@ -118,13 +131,13 @@ mp3_try_load (const string& filename, WavData& wav_data)
{
err
=
mpg123_format
(
mh
,
rates
[
i
],
MPG123_MONO
|
MPG123_STEREO
,
MPG123_ENC_FLOAT_32
);
if
(
err
!=
MPG123_OK
)
return
false
;
return
mpg123_strerror
(
mh
)
;
}
}
err
=
mpg123_open
(
mh
,
filename
.
c_str
());
if
(
err
!=
MPG123_OK
)
return
false
;
return
mpg123_strerror
(
mh
)
;
smh
.
need_close
=
true
;
...
...
@@ -134,7 +147,7 @@ mp3_try_load (const string& filename, WavData& wav_data)
err
=
mpg123_getformat
(
mh
,
&
rate
,
&
channels
,
&
encoding
);
if
(
err
!=
MPG123_OK
)
return
false
;
return
mpg123_strerror
(
mh
)
;
/* ensure that the format will not change */
mpg123_format_none
(
mh
);
...
...
@@ -158,7 +171,9 @@ mp3_try_load (const string& filename, WavData& wav_data)
}
else
if
(
err
==
MPG123_DONE
)
{
break
;
wav_data
=
WavData
(
samples
,
channels
,
rate
,
24
);
return
""
;
/* success */
}
else
if
(
err
==
MPG123_NEED_MORE
)
{
...
...
@@ -166,10 +181,7 @@ mp3_try_load (const string& filename, WavData& wav_data)
}
else
{
return
false
;
return
mpg123_strerror
(
mh
)
;
}
}
wav_data
=
WavData
(
samples
,
channels
,
rate
,
24
);
return
true
;
}
src/mp3.hh
View file @
fcd89683
...
...
@@ -5,6 +5,7 @@
#include "wavdata.hh"
bool
mp3_try_load
(
const
std
::
string
&
filename
,
WavData
&
wav_data
);
bool
mp3_detect
(
const
std
::
string
&
filename
);
std
::
string
mp3_load
(
const
std
::
string
&
filename
,
WavData
&
wav_data
);
#endif
/* AUDIOWMARK_MP3_HH */
src/testmp3.cc
View file @
fcd89683
#include "mp3.hh"
using
std
::
string
;
int
main
(
int
argc
,
char
**
argv
)
{
WavData
wd
;
if
(
argc
>=
2
)
{
if
(
mp3_
try_load
(
argv
[
1
],
wd
))
if
(
mp3_
detect
(
argv
[
1
]
))
{
int
sec
=
wd
.
n_values
()
/
wd
.
n_channels
()
/
wd
.
sample_rate
();
string
error
=
mp3_load
(
argv
[
1
],
wd
);
if
(
error
==
""
)
{
int
sec
=
wd
.
n_values
()
/
wd
.
n_channels
()
/
wd
.
sample_rate
();
printf
(
"loaded mp3 %s: %d:%02d
\n
"
,
argv
[
1
],
sec
/
60
,
sec
%
60
);
if
(
argc
==
3
)
printf
(
"loaded mp3 %s: %d:%02d
\n
"
,
argv
[
1
],
sec
/
60
,
sec
%
60
);
if
(
argc
==
3
)
{
wd
.
save
(
argv
[
2
]);
printf
(
"saved wav: %s
\n
"
,
argv
[
2
]);
}
}
else
{
wd
.
save
(
argv
[
2
]
);
printf
(
"saved wav: %s
\n
"
,
argv
[
2
])
;
printf
(
"mp3 load %s failed: %s
\n
"
,
argv
[
1
],
error
.
c_str
()
);
return
1
;
}
}
else
{
printf
(
"mp3
try load
%s failed
\n
"
,
argv
[
1
]);
printf
(
"mp3
detect
%s failed
\n
"
,
argv
[
1
]);
return
1
;
}
}
...
...
src/wavdata.cc
View file @
fcd89683
...
...
@@ -36,10 +36,18 @@ WavData::load (const string& filename)
int
error
=
sf_error
(
sndfile
);
if
(
error
)
{
if
(
mp3_
try_load
(
filename
,
*
this
))
if
(
mp3_
detect
(
filename
))
{
// ok, if its an mp3, take it
return
true
;
string
error
=
mp3_load
(
filename
,
*
this
);
if
(
error
==
""
)
{
return
true
;
// mp3 loaded successfully
}
else
{
m_error_blurb
=
"mp3 load error: "
+
error
;
return
false
;
}
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment