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
cb891005
Commit
cb891005
authored
Dec 13, 2019
by
Stefan Westerfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge mp3 detect/init code into mp3inputstream.cc/hh.
Signed-off-by:
Stefan Westerfeld
<
stefan@space.twc.de
>
parent
e52e763b
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
92 additions
and
90 deletions
+92
-90
Makefile.am
src/Makefile.am
+1
-1
audiostream.cc
src/audiostream.cc
+1
-1
mp3.cc
src/mp3.cc
+0
-83
mp3.hh
src/mp3.hh
+0
-2
mp3inputstream.cc
src/mp3inputstream.cc
+87
-1
mp3inputstream.hh
src/mp3inputstream.hh
+1
-0
testmp3.cc
src/testmp3.cc
+2
-2
No files found.
src/Makefile.am
View file @
cb891005
bin_PROGRAMS
=
audiowmark
COMMON_SRC
=
utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc
mp3.cc mp3.hh
wavdata.cc wavdata.hh
\
COMMON_SRC
=
utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc wavdata.cc wavdata.hh
\
audiostream.cc audiostream.hh sfinputstream.cc sfinputstream.hh stdoutwavoutputstream.cc stdoutwavoutputstream.hh
\
sfoutputstream.cc sfoutputstream.hh rawinputstream.cc rawinputstream.hh rawoutputstream.cc rawoutputstream.hh
\
rawconverter.cc rawconverter.hh mp3inputstream.cc mp3inputstream.hh wmcommon.cc wmcommon.hh fft.cc fft.hh
...
...
src/audiostream.cc
View file @
cb891005
...
...
@@ -24,7 +24,7 @@ AudioInputStream::create (const string& filename, Error& err)
SFInputStream
*
sistream
=
new
SFInputStream
();
in_stream
.
reset
(
sistream
);
err
=
sistream
->
open
(
filename
);
if
(
err
&&
mp3_
detect
(
filename
))
if
(
err
&&
MP3InputStream
::
detect
(
filename
))
{
MP3InputStream
*
mistream
=
new
MP3InputStream
();
in_stream
.
reset
(
mistream
);
...
...
src/mp3.cc
View file @
cb891005
...
...
@@ -8,87 +8,4 @@
using
std
::
vector
;
using
std
::
string
;
struct
ScopedMHandle
{
mpg123_handle
*
mh
=
nullptr
;
bool
need_close
=
false
;
~
ScopedMHandle
()
{
if
(
mh
&&
need_close
)
mpg123_close
(
mh
);
if
(
mh
)
mpg123_delete
(
mh
);
}
};
void
mp3_init
()
{
static
bool
mpg123_init_ok
=
false
;
if
(
!
mpg123_init_ok
)
{
int
err
=
mpg123_init
();
if
(
err
!=
MPG123_OK
)
{
error
(
"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
*/
bool
mp3_detect
(
const
string
&
filename
)
{
int
err
=
0
;
mp3_init
();
mpg123_handle
*
mh
=
mpg123_new
(
NULL
,
&
err
);
if
(
err
!=
MPG123_OK
)
return
false
;
auto
smh
=
ScopedMHandle
{
mh
};
// cleanup on return
err
=
mpg123_param
(
mh
,
MPG123_ADD_FLAGS
,
MPG123_QUIET
,
0
);
if
(
err
!=
MPG123_OK
)
return
false
;
err
=
mpg123_open
(
mh
,
filename
.
c_str
());
if
(
err
!=
MPG123_OK
)
return
false
;
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
];
for
(
size_t
i
=
0
;
i
<
30
;
i
++
)
{
size_t
done
;
err
=
mpg123_read
(
mh
,
buffer
,
buffer_bytes
,
&
done
);
if
(
err
==
MPG123_DONE
)
{
return
true
;
}
else
if
(
err
!=
MPG123_OK
)
{
return
false
;
}
}
return
true
;
}
src/mp3.hh
View file @
cb891005
...
...
@@ -5,7 +5,5 @@
#include "wavdata.hh"
bool
mp3_detect
(
const
std
::
string
&
filename
);
void
mp3_init
();
#endif
/* AUDIOWMARK_MP3_HH */
src/mp3inputstream.cc
View file @
cb891005
...
...
@@ -5,6 +5,23 @@
#include <assert.h>
using
std
::
min
;
using
std
::
string
;
static
void
mp3_init
()
{
static
bool
mpg123_init_ok
=
false
;
if
(
!
mpg123_init_ok
)
{
int
err
=
mpg123_init
();
if
(
err
!=
MPG123_OK
)
{
error
(
"audiowmark: init mpg123 lib failed
\n
"
);
exit
(
1
);
}
mpg123_init_ok
=
true
;
}
}
MP3InputStream
::~
MP3InputStream
()
{
...
...
@@ -12,7 +29,7 @@ MP3InputStream::~MP3InputStream()
}
Error
MP3InputStream
::
open
(
const
st
d
::
st
ring
&
filename
)
MP3InputStream
::
open
(
const
string
&
filename
)
{
int
err
=
0
;
...
...
@@ -165,3 +182,72 @@ MP3InputStream::n_frames() const
{
return
m_n_values
/
m_n_channels
;
}
/* 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
*/
bool
MP3InputStream
::
detect
(
const
string
&
filename
)
{
struct
ScopedMHandle
{
mpg123_handle
*
mh
=
nullptr
;
bool
need_close
=
false
;
~
ScopedMHandle
()
{
if
(
mh
&&
need_close
)
mpg123_close
(
mh
);
if
(
mh
)
mpg123_delete
(
mh
);
}
};
int
err
=
0
;
mp3_init
();
mpg123_handle
*
mh
=
mpg123_new
(
NULL
,
&
err
);
if
(
err
!=
MPG123_OK
)
return
false
;
auto
smh
=
ScopedMHandle
{
mh
};
// cleanup on return
err
=
mpg123_param
(
mh
,
MPG123_ADD_FLAGS
,
MPG123_QUIET
,
0
);
if
(
err
!=
MPG123_OK
)
return
false
;
err
=
mpg123_open
(
mh
,
filename
.
c_str
());
if
(
err
!=
MPG123_OK
)
return
false
;
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
];
for
(
size_t
i
=
0
;
i
<
30
;
i
++
)
{
size_t
done
;
err
=
mpg123_read
(
mh
,
buffer
,
buffer_bytes
,
&
done
);
if
(
err
==
MPG123_DONE
)
{
return
true
;
}
else
if
(
err
!=
MPG123_OK
)
{
return
false
;
}
}
return
true
;
}
src/mp3inputstream.hh
View file @
cb891005
...
...
@@ -35,6 +35,7 @@ public:
int
n_channels
()
const
override
;
size_t
n_frames
()
const
override
;
static
bool
detect
(
const
std
::
string
&
filename
);
};
#endif
/* AUDIOWMARK_MP3_INPUT_STREAM_HH */
...
...
src/testmp3.cc
View file @
cb891005
#include "mp3.hh"
#include "mp3inputstream.hh"
#include "wavdata.hh"
using
std
::
string
;
...
...
@@ -9,7 +9,7 @@ main (int argc, char **argv)
WavData
wd
;
if
(
argc
>=
2
)
{
if
(
mp3_
detect
(
argv
[
1
]))
if
(
MP3InputStream
::
detect
(
argv
[
1
]))
{
MP3InputStream
m3i
;
Error
err
=
m3i
.
open
(
argv
[
1
]);
...
...
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