Compare commits
143 Commits
disable-pa
...
master
Author | SHA1 | Date | |
---|---|---|---|
eacae68d37 | |||
6d9489d3a6 | |||
f446e4ade7 | |||
719f6baaa4 | |||
|
44f79153ea | ||
|
6b747e224c | ||
|
8d3a3c6433 | ||
75d6db0ee8 | |||
13eb658cb9 | |||
3d23d369d1 | |||
|
3fd91bbfae | ||
|
ed2dd72c6b | ||
|
dc3592147c | ||
f06461313e | |||
|
bf35f643fd | ||
|
479fbc7b89 | ||
082e8b6596 | |||
eb9ca5b405 | |||
faec9f521d | |||
72b39e726d | |||
ec7df7fd9e | |||
f4ddd42705 | |||
6f29371973 | |||
7333754bb2 | |||
|
1445939bf8 | ||
|
daee149271 | ||
fa2e65d462 | |||
c7df56da97 | |||
66e1a77b1e | |||
98c9a2bb27 | |||
06a49f15e2 | |||
5094ab083a | |||
8b55887d4e | |||
42a18fd603 | |||
591ed70b5c | |||
ac7c9463b4 | |||
|
d10a1cf526 | ||
7e45aea6fa | |||
|
f82c98a12e | ||
dd2aece59d | |||
fec580dc52 | |||
dbf8c5daf7 | |||
8b25914f47 | |||
2a1c6d164f | |||
2300c25a12 | |||
b66d32c84e | |||
3e5a9f32a0 | |||
2a89ddb2ca | |||
5fd2ef3902 | |||
b4d5019d83 | |||
6d93ed3a70 | |||
5a84e8831a | |||
23556103fb | |||
|
cefd48021f | ||
|
534554a18e | ||
|
7fcaedd2ef | ||
|
b4ca23a0b6 | ||
|
708201e6a1 | ||
|
87559b900b | ||
|
452653afc4 | ||
|
0829cbf494 | ||
|
9ddadb4a13 | ||
|
448b03b119 | ||
|
fc800d2cb5 | ||
|
e07fb5abe0 | ||
|
5c49b18670 | ||
|
d2769d28d3 | ||
|
743f779529 | ||
|
0fc9f8d966 | ||
|
97f563dd6e | ||
|
f2e8dfe5b8 | ||
|
266913ef13 | ||
|
9cf425a8ba | ||
|
08aba11e39 | ||
|
8391095198 | ||
|
4097fcce6b | ||
|
e51c242a48 | ||
|
eef648bda5 | ||
|
10cddd64a3 | ||
|
ad947643ee | ||
|
a3be133113 | ||
|
1c40ac7673 | ||
|
cd3fd54018 | ||
|
8d3e328cee | ||
|
df976c3725 | ||
|
bf428a62af | ||
|
40f8e399c9 | ||
|
fa3d467536 | ||
|
e6ad4c96a6 | ||
|
661d848331 | ||
|
c3f288eb71 | ||
|
8f36dd3ee8 | ||
|
411cdb6ecf | ||
|
8d0d77da7d | ||
|
701feb414d | ||
|
7420da5041 | ||
|
c5ffb619bb | ||
|
b3f9ef5fa3 | ||
|
89e389e1c9 | ||
|
774f4972c2 | ||
|
f3006e843c | ||
|
a0fe109b96 | ||
|
7498cf4620 | ||
|
0227c9596a | ||
|
5302e1e45c | ||
|
b61c0e0b62 | ||
|
c08fd3d703 | ||
|
43fbafa80b | ||
|
b12773d52d | ||
|
d2124a3b2c | ||
|
3a4bac5a34 | ||
|
c9342d7226 | ||
|
ef263c1eb1 | ||
|
5930d2bc93 | ||
|
5f366b0d80 | ||
|
51e8cf9344 | ||
|
80f4ed11aa | ||
|
85e13251ae | ||
|
0f3e209202 | ||
|
a4f2dd6a96 | ||
|
8468a9ab8f | ||
|
a27b91c6fd | ||
|
80b8541f8f | ||
|
eee2bf612c | ||
|
68d1d9d71b | ||
|
13899e6b58 | ||
|
fac5579a66 | ||
|
d2442260da | ||
|
be0a3e5290 | ||
|
80a9e7c446 | ||
|
1805ff8161 | ||
|
5107bc6087 | ||
|
23d4d36cb4 | ||
|
a3fcccbf2a | ||
|
dd997510d3 | ||
|
833d8b020b | ||
|
e9e63f961c | ||
|
82d229346b | ||
|
b7a6812e92 | ||
|
3ed0d7126a | ||
|
0975fa1fd4 | ||
|
bd6ff422a7 | ||
|
59b09129f4 |
36
.github/workflows/rust.yml
vendored
36
.github/workflows/rust.yml
vendored
|
@ -1,36 +0,0 @@
|
|||
name: Rust
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: nightly-2021-04-01
|
||||
override: false
|
||||
- name: Packages
|
||||
run: sudo apt-get install build-essential yasm libavutil-dev libavcodec-dev libavformat-dev libavfilter-dev libavfilter-dev libavdevice-dev libswresample-dev libfftw3-dev ffmpeg
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
- name: Run example tests
|
||||
run: cargo test --verbose --examples
|
||||
- name: Build benches
|
||||
run: cargo +nightly-2021-04-01 bench --verbose --features=bench --no-run
|
||||
- name: Build examples
|
||||
run: cargo build --examples --verbose
|
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
target
|
||||
node_modules
|
||||
index.node
|
||||
index-*.node
|
||||
bliss-rs-bliss-rs-*.tgz
|
96
CHANGELOG.md
96
CHANGELOG.md
|
@ -1,4 +1,98 @@
|
|||
# Changelog
|
||||
#Changelog
|
||||
|
||||
## bliss 0.6.11
|
||||
* Bump rust-ffmpeg to 6.1.1 to fix build for raspberry pis.
|
||||
|
||||
## bliss 0.6.10
|
||||
* Make the `analyze` function public, for people who don't want to use
|
||||
ffmpeg
|
||||
* Run `cargo update`, bump ffmpeg to 6.1
|
||||
* Fix the library module erroring when wrong UTF-8 ends up in the database.
|
||||
|
||||
## bliss 0.6.9
|
||||
* Add a feature flag for compilation on raspberry pis.
|
||||
|
||||
## bliss 0.6.8
|
||||
* Add an `update-aubio-bindings` feature.
|
||||
|
||||
## bliss 0.6.7
|
||||
* Fix compatibility for ffmpeg 6.0, and bump ffmpeg version to 6.0.
|
||||
* Update and remove extraneous dependencies.
|
||||
|
||||
## bliss 0.6.6
|
||||
* Add a `delete_everything_else` function in `library`'s update functions.
|
||||
* Use Rust 2021.
|
||||
|
||||
## bliss 0.6.5
|
||||
* Fix library update performance issues.
|
||||
* Pretty-print JSON in the config file.
|
||||
|
||||
## bliss 0.6.4
|
||||
* Fix a bug in the customizable CPU number option in `library`.
|
||||
|
||||
## bliss 0.6.3
|
||||
* Add customizable CPU number in the `library` module.
|
||||
|
||||
## bliss 0.6.2
|
||||
* Add a `library` module, that greatly helps when making player plug-ins.
|
||||
|
||||
## bliss 0.6.1
|
||||
* Fix a decoding bug while decoding certain WAV files.
|
||||
|
||||
## bliss 0.6.0
|
||||
* Change String to PathBuf in `analyze_paths`.
|
||||
* Add `analyze_paths_with_cores`.
|
||||
|
||||
## bliss 0.5.2
|
||||
* Fix a bug with some broken MP3 files.
|
||||
* Bump ffmpeg to 5.1.0.
|
||||
|
||||
## bliss 0.5.0
|
||||
* Add support for CUE files.
|
||||
* Add `album_artist` and `duration` to `Song`.
|
||||
* Fix a bug in `estimate_tuning` that led to empty chroma errors.
|
||||
* Remove the unusued Library trait, and extract a few useful functions from
|
||||
there (`analyze_paths`, `closest_to_album_group`).
|
||||
* Rename `distance` module to `playlist`.
|
||||
* Remove all traces of the "analyse" word vs "analyze" to make the codebase
|
||||
more coherent.
|
||||
* Rename `Song::new` to `Song::from_path`.
|
||||
|
||||
## bliss 0.4.6
|
||||
* Bump ffmpeg crate version to allow for cross-compilation.
|
||||
|
||||
## bliss 0.4.5
|
||||
* Bump ffmpeg crate version.
|
||||
* Add an "ffmpeg-static" option.
|
||||
|
||||
## bliss 0.4.4
|
||||
* Make features' version public.
|
||||
|
||||
## bliss 0.4.3
|
||||
* Add features' version on each Song instance.
|
||||
|
||||
## bliss 0.4.2
|
||||
* Add a binary example to easily make playlists.
|
||||
|
||||
## bliss 0.4.1
|
||||
* Add a function to make album playlists.
|
||||
|
||||
## bliss 0.4.0
|
||||
* Make the song-to-song custom sorting method faster.
|
||||
* Rename `to_vec` and `to_arr1` to `as_vec` and `as_arr1` .
|
||||
* Add a playlist_dedup function.
|
||||
|
||||
## bliss 0.3.5
|
||||
* Add custom sorting methods for playlist-making.
|
||||
|
||||
## bliss 0.3.4
|
||||
* Bump ffmpeg's version to avoid building ffmpeg when building bliss.
|
||||
|
||||
## bliss 0.3.3
|
||||
* Add a streaming analysis function, to help libraries displaying progress.
|
||||
|
||||
## bliss 0.3.2
|
||||
* Fixed a rare ffmpeg multithreading bug.
|
||||
|
||||
## bliss 0.3.1
|
||||
* Show error message when song storage fails in the Library trait.
|
||||
|
|
1119
Cargo.lock
generated
1119
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
74
Cargo.toml
74
Cargo.toml
|
@ -1,49 +1,57 @@
|
|||
[package]
|
||||
name = "bliss-audio"
|
||||
version = "0.3.1"
|
||||
name = "bliss-rs"
|
||||
version = "0.6.11"
|
||||
build = "build.rs"
|
||||
authors = ["Polochon-street <polochonstreet@gmx.fr>"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-only"
|
||||
description = "A song analysis library for making playlists"
|
||||
homepage = "https://lelele.io/bliss.html"
|
||||
repository = "https://github.com/Polochon-street/bliss-rs"
|
||||
keywords = ["audio", "analysis", "MIR", "playlist", "similarity"]
|
||||
readme = "README.md"
|
||||
exclude = ["data/", "index.node"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib", "cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["bliss-audio-aubio-rs/rustdoc"]
|
||||
features = ["bliss-audio-aubio-rs/rustdoc", "library"]
|
||||
no-default-features = true
|
||||
|
||||
[features]
|
||||
# Building ffmpeg until either
|
||||
# https://github.com/zmwangx/rust-ffmpeg/pull/60
|
||||
# or https://github.com/zmwangx/rust-ffmpeg/pull/62 is in
|
||||
default = ["bliss-audio-aubio-rs/static", "build-ffmpeg"]
|
||||
# Build ffmpeg instead of using the host's.
|
||||
build-ffmpeg = ["ffmpeg-next/build"]
|
||||
# Use if you want to build python bindings with maturin.
|
||||
python-bindings = ["bliss-audio-aubio-rs/fftw3"]
|
||||
# Enable the benchmarks with `cargo +nightly bench --features=bench`
|
||||
bench = []
|
||||
|
||||
[dependencies]
|
||||
ripemd160 = "0.9.0"
|
||||
ndarray-npy = "0.8.0"
|
||||
ndarray = { version = "0.15.0", features = ["rayon"] }
|
||||
num_cpus = "1.13.0"
|
||||
ndarray-stats = "0.5.0"
|
||||
rustfft = "5.0.1"
|
||||
lazy_static = "1.4.0"
|
||||
rayon = "1.5.0"
|
||||
crossbeam = "0.8.0"
|
||||
noisy_float = "0.2.0"
|
||||
ffmpeg-next = "4.3.8"
|
||||
log = "0.4.14"
|
||||
env_logger = "0.8.3"
|
||||
thiserror = "1.0.24"
|
||||
# Until https://github.com/aubio/aubio/issues/336 is somehow solved
|
||||
# Hopefully we'll be able to use the official aubio-rs at some point.
|
||||
bliss-audio-aubio-rs = "0.2.0"
|
||||
strum = "0.21"
|
||||
strum_macros = "0.21"
|
||||
bliss-audio-aubio-rs = { version = "0.2.1", features = ["static"] }
|
||||
crossbeam = "0.8.2"
|
||||
ffmpeg-next = { version = "6.1.1", features = ["static"] }
|
||||
log = "0.4.17"
|
||||
# `rayon` is used only by `par_mapv_inplace` in chroma.rs.
|
||||
# TODO: is the speed gain that substantial?
|
||||
ndarray = { version = "0.15.6", features = ["rayon"] }
|
||||
ndarray-stats = "0.5.1"
|
||||
noisy_float = "0.2.0"
|
||||
adler32 = "1.0.2"
|
||||
rustfft = "6.1.0"
|
||||
thiserror = "1.0.40"
|
||||
strum = "0.24.1"
|
||||
strum_macros = "0.24.3"
|
||||
|
||||
# Deps for the library feature
|
||||
serde = { version = "1.0", optional = true, features = ["derive"] }
|
||||
serde_json = { version = "1.0.59", optional = true }
|
||||
serde_ini = { version = "0.2.0", optional = true }
|
||||
|
||||
[dependencies.neon]
|
||||
version = "1.0.0-alpha.4"
|
||||
default-features = false
|
||||
features = ["napi-6", "channel-api", "promise-api", "try-catch-api"]
|
||||
|
||||
[dev-dependencies]
|
||||
ndarray-npy = { version = "0.8.1", default-features = false }
|
||||
mime_guess = "2.0.3"
|
||||
glob = "0.3.0"
|
||||
anyhow = "1.0.45"
|
||||
clap = "2.33.3"
|
||||
pretty_assertions = "1.3.0"
|
||||
serde_json = "1.0.59"
|
||||
|
|
20
Dockerfile
Normal file
20
Dockerfile
Normal file
|
@ -0,0 +1,20 @@
|
|||
FROM node:20-slim
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -yqq gnupg dirmngr apt-transport-https software-properties-common
|
||||
|
||||
RUN gpg -K && gpg --no-default-keyring \
|
||||
--keyring /usr/share/keyrings/deb-multimedia.gpg \
|
||||
--keyserver keyserver.ubuntu.com \
|
||||
--recv-keys 5C808C2B65558117
|
||||
RUN echo "deb [signed-by=/usr/share/keyrings/deb-multimedia.gpg] https://www.deb-multimedia.org $(lsb_release -sc) main non-free" \
|
||||
| tee /etc/apt/sources.list.d/deb-multimedia.list
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -yqq wget build-essential yasm libavutil-dev libavcodec-dev libavformat-dev libavfilter-dev libavfilter-dev libavdevice-dev libswresample-dev libfftw3-dev libclang-dev ffmpeg
|
||||
|
||||
WORKDIR /opt/rust
|
||||
RUN wget https://sh.rustup.rs -O rustup-init.sh
|
||||
RUN chmod +x rustup-init.sh
|
||||
|
||||
RUN ./rustup-init.sh -y -t x86_64-unknown-linux-gnu x86_64-unknown-linux-musl aarch64-unknown-linux-gnu aarch64-unknown-linux-musl
|
674
LICENSE
Normal file
674
LICENSE
Normal file
|
@ -0,0 +1,674 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
103
README.md
103
README.md
|
@ -1,8 +1,30 @@
|
|||
[![crate](https://img.shields.io/crates/v/bliss-audio.svg)](https://crates.io/crates/bliss-audio)
|
||||
[![build](https://github.com/Polochon-street/bliss-rs/workflows/Rust/badge.svg)](https://github.com/Polochon-street/bliss-rs/actions)
|
||||
[![doc](https://docs.rs/bliss-rs/badge.svg)](https://docs.rs/bliss-audio/)
|
||||
[![doc](https://docs.rs/bliss-audio/badge.svg)](https://docs.rs/bliss-audio/)
|
||||
|
||||
# bliss music analyser - Rust version
|
||||
# Deprecation notice
|
||||
This repo is no longer maintained - please refer to [bliss-js](https://gitea.antonlyap.pp.ua/antonlyap/bliss-js) instead.
|
||||
|
||||
# Fork notice
|
||||
This repo is a fork of [bliss-rs](https://github.com/Polochon-street/bliss-rs) with bindings for Node.js (using N-API and Neon).
|
||||
|
||||
## Example usage:
|
||||
The package is published to the Gitea registry: https://gitea.antonlyap.pp.ua/antonlyap/-/packages/npm/@bliss-rs%2Fbliss-rs/1.0.0
|
||||
```ts
|
||||
import { analyze, analyzeSync } from '@bliss-rs/bliss-rs';
|
||||
|
||||
await analyze("/path/to/track.mp3") // returns Uint8Array
|
||||
```
|
||||
|
||||
## Return value
|
||||
The output of `bliss-rs` consists of single-precision floats, currently 20 of them. This fork contains code to convert it into an array of 80 bytes in little endian order. An additional version (also comes from `bliss-rs`, currently equal to `1`) is prepended at the start (16-bit unsigned little-endian integer). Therefore, the total output size is 82 bytes.
|
||||
|
||||
### Usage
|
||||
The output (without the version) is meant to be converted back into floats and used to calculate the [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions) between two songs. Other distance algorithms are being worked on by the Bliss team.
|
||||
|
||||
---
|
||||
|
||||
# (Original README) bliss music analyzer - Rust version
|
||||
bliss-rs is the Rust improvement of [bliss](https://github.com/Polochon-street/bliss), a
|
||||
library used to make playlists by analyzing songs, and computing distance between them.
|
||||
|
||||
|
@ -16,6 +38,9 @@ For now (and if you're looking for an easy-to use smooth play experience),
|
|||
[MPD](https://www.musicpd.org/).
|
||||
|
||||
There are also [python](https://pypi.org/project/bliss-audio/) bindings.
|
||||
The wheels are compiled used [maturin](https://github.com/PyO3/maturin/); the
|
||||
sources [are available here](https://github.com/Polochon-street/bliss-python)
|
||||
for inspiration.
|
||||
|
||||
Note 1: the features bliss-rs outputs is not compatible with the ones
|
||||
used by C-bliss, since it uses
|
||||
|
@ -23,18 +48,33 @@ different, more accurate values, based on
|
|||
[actual literature](https://lelele.io/thesis.pdf). It is also faster.
|
||||
|
||||
## Examples
|
||||
For simple analysis / distance computing, a look at `examples/distance.rs` and
|
||||
`examples/analyse.rs`.
|
||||
For simple analysis / distance computing, take a look at `examples/distance.rs` and
|
||||
`examples/analyze.rs`.
|
||||
|
||||
Ready to use examples:
|
||||
If you simply want to try out making playlists from a folder containing songs,
|
||||
[this example](https://github.com/Polochon-street/bliss-rs/blob/master/examples/playlist.rs)
|
||||
contains all you need. Usage:
|
||||
|
||||
cargo run --features=serde --release --example=playlist /path/to/folder /path/to/first/song
|
||||
|
||||
Don't forget the `--release` flag!
|
||||
|
||||
By default, it outputs the playlist to stdout, but you can use `-o <path>`
|
||||
to output it to a specific path.
|
||||
|
||||
To avoid having to analyze the entire folder
|
||||
several times, it also stores the analysis in `/tmp/analysis.json`. You can customize
|
||||
this behavior by using `-a <path>` to store this file in a specific place.
|
||||
|
||||
Ready to use code examples:
|
||||
|
||||
### Compute the distance between two songs
|
||||
```
|
||||
use bliss_audio::{BlissError, Song};
|
||||
|
||||
fn main() -> Result<(), BlissError> {
|
||||
let song1 = Song::new("/path/to/song1")?;
|
||||
let song2 = Song::new("/path/to/song2")?;
|
||||
let song1 = Song::from_path("/path/to/song1")?;
|
||||
let song2 = Song::from_path("/path/to/song2")?;
|
||||
|
||||
println!("Distance between song1 and song2 is {}", song1.distance(&song2));
|
||||
Ok(())
|
||||
|
@ -50,7 +90,7 @@ fn main() -> Result<(), BlissError> {
|
|||
let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"];
|
||||
let mut songs: Vec<Song> = paths
|
||||
.iter()
|
||||
.map(|path| Song::new(path))
|
||||
.map(|path| Song::from_path(path))
|
||||
.collect::<Result<Vec<Song>, BlissError>>()?;
|
||||
|
||||
// Assuming there is a first song
|
||||
|
@ -71,17 +111,50 @@ fn main() -> Result<(), BlissError> {
|
|||
## Further use
|
||||
|
||||
Instead of reinventing ways to fetch a user library, play songs, etc,
|
||||
and embed that into bliss, it is easier to look at the
|
||||
[Library](https://github.com/Polochon-street/bliss-rs/blob/master/src/library.rs#L12)
|
||||
trait.
|
||||
|
||||
By implementing a few functions to get songs from a media library, and store
|
||||
the resulting analysis, you get access to functions to analyze an entire
|
||||
library (with multithreading), and to make playlists easily.
|
||||
and embed that into bliss, it is easier to look at the
|
||||
[library](https://docs.rs/bliss-audio/latest/bliss_audio/library/index.html) module.
|
||||
It implements common analysis functions, and allows analyzed songs to be put
|
||||
in a sqlite database seamlessly.
|
||||
|
||||
See [blissify](https://crates.io/crates/blissify) for a reference
|
||||
implementation.
|
||||
|
||||
## Cross-compilation
|
||||
|
||||
To cross-compile bliss-rs from linux to x86_64 windows, install the
|
||||
`x86_64-pc-windows-gnu` target via:
|
||||
|
||||
rustup target add x86_64-pc-windows-gnu
|
||||
|
||||
Make sure you have `x86_64-w64-mingw32-gcc` installed on your computer.
|
||||
|
||||
Then after downloading and extracting [ffmpeg's prebuilt binaries](https://www.gyan.dev/ffmpeg/builds/),
|
||||
running:
|
||||
|
||||
FFMPEG_DIR=/path/to/prebuilt/ffmpeg cargo build --target x86_64-pc-windows-gnu --release
|
||||
|
||||
Will produce a `.rlib` library file. If you want to generate a shared `.dll`
|
||||
library, add:
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
to `Cargo.toml` before compiling, and if you want to generate a `.lib` static
|
||||
library, add:
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
You can of course test the examples yourself by compiling them as .exe:
|
||||
|
||||
FFMPEG_DIR=/path/to/prebuilt/ffmpeg cargo build --target x86_64-pc-windows-gnu --release --examples
|
||||
|
||||
WARNING: Doing all of the above and making it work on windows requires to have
|
||||
ffmpeg's dll on your Windows `%PATH%` (`avcodec-59.dll`, etc).
|
||||
Usually installing ffmpeg on the target windows is enough, but you can also just
|
||||
extract them from `/path/to/prebuilt/ffmpeg/bin` and put them next to the thing
|
||||
you generated from cargo (either bliss' dll or executable).
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
* This library relies heavily on [aubio](https://aubio.org/)'s
|
||||
|
|
12
build.rs
Normal file
12
build.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use std::env;
|
||||
|
||||
fn main() {
|
||||
for (name, _value) in env::vars() {
|
||||
if name.starts_with("DEP_FFMPEG_") {
|
||||
println!(
|
||||
r#"cargo:rustc-cfg=feature="{}""#,
|
||||
name["DEP_FFMPEG_".len()..name.len()].to_lowercase()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
1
ci_check.sh
Executable file
1
ci_check.sh
Executable file
|
@ -0,0 +1 @@
|
|||
cargo fmt -- --check && cargo clippy --examples --features=serde -- -D warnings && cargo build --verbose && cargo test --verbose && cargo test --verbose --examples && cargo +nightly-2023-02-16 bench --verbose --features=bench --no-run && cargo build --examples --verbose --features=serde
|
Binary file not shown.
Binary file not shown.
29
data/empty.cue
Normal file
29
data/empty.cue
Normal file
|
@ -0,0 +1,29 @@
|
|||
REM GENRE Random
|
||||
REM DATE 2022
|
||||
PERFORMER "Polochon_street"
|
||||
TITLE "Album for CUE test"
|
||||
FILE "empty.wav" WAVE
|
||||
TRACK 01 AUDIO
|
||||
TITLE "Renaissance"
|
||||
PERFORMER "David TMX"
|
||||
INDEX 01 0:00:00
|
||||
TRACK 02 AUDIO
|
||||
TITLE "Piano"
|
||||
PERFORMER "Polochon_street"
|
||||
INDEX 01 0:11:05
|
||||
TRACK 03 AUDIO
|
||||
TITLE "Tone"
|
||||
PERFORMER "Polochon_street"
|
||||
INDEX 01 0:16:69
|
||||
|
||||
FILE "not-existing.wav" WAVE
|
||||
TRACK 01 AUDIO
|
||||
TITLE "Nope"
|
||||
PERFORMER "Charlie"
|
||||
INDEX 01 0:00:00
|
||||
TRACK 02 AUDIO
|
||||
TITLE "Nope"
|
||||
PERFORMER "Charlie"
|
||||
INDEX 01 0:10:00
|
||||
|
||||
|
BIN
data/empty.wav
Normal file
BIN
data/empty.wav
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
data/piano.wav
Normal file
BIN
data/piano.wav
Normal file
Binary file not shown.
BIN
data/picture.jpg
BIN
data/picture.jpg
Binary file not shown.
Before Width: | Height: | Size: 826 KiB |
BIN
data/picture.png
BIN
data/picture.png
Binary file not shown.
Before Width: | Height: | Size: 826 KiB After Width: | Height: | Size: 5.1 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
29
data/testcue.cue
Normal file
29
data/testcue.cue
Normal file
|
@ -0,0 +1,29 @@
|
|||
REM GENRE Random
|
||||
REM DATE 2022
|
||||
PERFORMER "Polochon_street"
|
||||
TITLE "Album for CUE test"
|
||||
FILE "testcue.flac" WAVE
|
||||
TRACK 01 AUDIO
|
||||
TITLE "Renaissance"
|
||||
PERFORMER "David TMX"
|
||||
INDEX 01 0:00:00
|
||||
TRACK 02 AUDIO
|
||||
TITLE "Piano"
|
||||
PERFORMER "Polochon_street"
|
||||
INDEX 01 0:11:05
|
||||
TRACK 03 AUDIO
|
||||
TITLE "Tone"
|
||||
PERFORMER "Polochon_street"
|
||||
INDEX 01 0:16:69
|
||||
|
||||
FILE "not-existing.wav" WAVE
|
||||
TRACK 01 AUDIO
|
||||
TITLE "Nope"
|
||||
PERFORMER "Charlie"
|
||||
INDEX 01 0:00:00
|
||||
TRACK 02 AUDIO
|
||||
TITLE "Nope"
|
||||
PERFORMER "Charlie"
|
||||
INDEX 01 0:10:00
|
||||
|
||||
|
BIN
data/testcue.flac
Normal file
BIN
data/testcue.flac
Normal file
Binary file not shown.
Binary file not shown.
BIN
data/white_noise.mp3
Normal file
BIN
data/white_noise.mp3
Normal file
Binary file not shown.
|
@ -1,17 +1,17 @@
|
|||
use bliss_audio::Song;
|
||||
use bliss_rs::bliss_lib::Song;
|
||||
use std::env;
|
||||
|
||||
/**
|
||||
* Simple utility to print the result of an Analysis.
|
||||
*
|
||||
* Takes a list of files to analyse an the result of the corresponding Analysis.
|
||||
* Takes a list of files to analyze an the result of the corresponding Analysis.
|
||||
*/
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().skip(1).collect();
|
||||
for path in &args {
|
||||
match Song::new(&path) {
|
||||
match Song::from_path(path) {
|
||||
Ok(song) => println!("{}: {:?}", path, song.analysis),
|
||||
Err(e) => println!("{}: {}", path, e),
|
||||
Err(e) => println!("{path}: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
use bliss_audio::Song;
|
||||
use bliss_rs::bliss_lib::Song;
|
||||
use std::env;
|
||||
|
||||
/**
|
||||
* Simple utility to print distance between two songs according to bliss.
|
||||
*
|
||||
* Takes two file paths, and analyse the corresponding songs, printing
|
||||
* Takes two file paths, and analyze the corresponding songs, printing
|
||||
* the distance between the two files according to bliss.
|
||||
*/
|
||||
fn main() -> Result<(), String> {
|
||||
|
@ -13,14 +13,21 @@ fn main() -> Result<(), String> {
|
|||
let first_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
|
||||
let second_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
|
||||
|
||||
let song1 = Song::new(&first_path).map_err(|x| x.to_string())?;
|
||||
let song2 = Song::new(&second_path).map_err(|x| x.to_string())?;
|
||||
let song1 = Song::from_path(&first_path).map_err(|x| x.to_string())?;
|
||||
let song2 = Song::from_path(&second_path).map_err(|x| x.to_string())?;
|
||||
|
||||
let mut distance_squared: f64 = 0.0;
|
||||
let analysis1 = song1.analysis.as_bytes();
|
||||
let analysis2 = song2.analysis.as_bytes();
|
||||
for (i, feature1) in analysis1.iter().enumerate() {
|
||||
distance_squared += (feature1 - analysis2[i]).pow(2) as f64;
|
||||
}
|
||||
|
||||
println!(
|
||||
"d({:?}, {:?}) = {}",
|
||||
song1.path,
|
||||
song2.path,
|
||||
song1.distance(&song2)
|
||||
&first_path,
|
||||
&second_path,
|
||||
distance_squared.sqrt(),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
2
index.d.ts
vendored
Normal file
2
index.d.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
export function analyzeSync(path: string): Uint8Array;
|
||||
export function analyze(path: string): Promise<Uint8Array>;
|
13
index.js
Normal file
13
index.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
try {
|
||||
module.exports = require('./index.node');
|
||||
} catch {
|
||||
const isLinux = process.platform === 'linux';
|
||||
|
||||
if (isLinux && process.arch === 'x64') {
|
||||
module.exports = require('./index-x86_64-unknown-linux-gnu.node');
|
||||
} else if (isLinux && process.arch === 'arm64') {
|
||||
module.exports = require('./index-aarch64-unknown-linux-gnu.node');
|
||||
} else {
|
||||
throw new Error('Bliss: unsupported architecture');
|
||||
}
|
||||
}
|
42
package-lock.json
generated
Normal file
42
package-lock.json
generated
Normal file
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
"name": "bliss-rs",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "bliss-rs",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cargo-cp-artifact": "^0.1.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.5"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "20.10.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.5.tgz",
|
||||
"integrity": "sha512-nNPsNE65wjMxEKI93yOP+NPGGBJz/PoN3kZsVLee0XMiJolxSekEVD8wRwBUBqkwc7UWop0edW50yrCQW4CyRw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"undici-types": "~5.26.4"
|
||||
}
|
||||
},
|
||||
"node_modules/cargo-cp-artifact": {
|
||||
"version": "0.1.8",
|
||||
"resolved": "https://registry.npmjs.org/cargo-cp-artifact/-/cargo-cp-artifact-0.1.8.tgz",
|
||||
"integrity": "sha512-3j4DaoTrsCD1MRkTF2Soacii0Nx7UHCce0EwUf4fHnggwiE4fbmF2AbnfzayR36DF8KGadfh7M/Yfy625kgPlA==",
|
||||
"bin": {
|
||||
"cargo-cp-artifact": "bin/cargo-cp-artifact.js"
|
||||
}
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "5.26.5",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
|
||||
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
}
|
23
package.json
Normal file
23
package.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "@bliss-rs/bliss-rs",
|
||||
"version": "0.0.4",
|
||||
"description": "A fork of the bliss-rs library with Node.js bindings",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"directories": {
|
||||
"example": "examples"
|
||||
},
|
||||
"files": ["index.js", "index.d.ts", "index-*.node"],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics"
|
||||
},
|
||||
"author": "antonlyap",
|
||||
"license": "GPL",
|
||||
"dependencies": {
|
||||
"cargo-cp-artifact": "^0.1.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.5"
|
||||
}
|
||||
}
|
82
src/bliss_lib.rs
Normal file
82
src/bliss_lib.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
//! # bliss audio library
|
||||
//!
|
||||
//! bliss is a library for making "smart" audio playlists.
|
||||
//!
|
||||
//! The core of the library is the [Song] object, which relates to a
|
||||
//! specific analyzed song and contains its path, title, analysis, and
|
||||
//! other metadata fields (album, genre...).
|
||||
//! Analyzing a song is as simple as running `Song::from_path("/path/to/song")`.
|
||||
//!
|
||||
//! The [analysis](Song::analysis) field of each song is an array of f32, which
|
||||
//! makes the comparison between songs easy, by just using e.g. euclidean
|
||||
//! distance (see [distance](Song::distance) for instance).
|
||||
//!
|
||||
//! Once several songs have been analyzed, making a playlist from one Song
|
||||
//! is as easy as computing distances between that song and the rest, and ordering
|
||||
//! the songs by distance, ascending.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ### Analyze & compute the distance between two songs
|
||||
//! ```no_run
|
||||
//! use bliss_audio::{BlissResult, Song};
|
||||
//!
|
||||
//! fn main() -> BlissResult<()> {
|
||||
//! let song1 = Song::from_path("/path/to/song1")?;
|
||||
//! let song2 = Song::from_path("/path/to/song2")?;
|
||||
//!
|
||||
//! println!("Distance between song1 and song2 is {}", song1.distance(&song2));
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
#![cfg_attr(feature = "bench", feature(test))]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
pub use crate::song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES};
|
||||
|
||||
/// Target channels for ffmpeg
|
||||
pub const CHANNELS: u16 = 1;
|
||||
|
||||
/// Target sample rate for ffmpeg
|
||||
pub const SAMPLE_RATE: u32 = 22050;
|
||||
/// Stores the current version of bliss-rs' features.
|
||||
/// It is bumped every time one or more feature is added, updated or removed,
|
||||
/// so plug-ins can rescan libraries when there is a major change.
|
||||
pub const FEATURES_VERSION: u16 = 1;
|
||||
|
||||
#[derive(Error, Clone, Debug, PartialEq, Eq)]
|
||||
/// Umbrella type for bliss error types
|
||||
pub enum BlissError {
|
||||
#[error("error happened while decoding file – {0}")]
|
||||
/// An error happened while decoding an (audio) file.
|
||||
DecodingError(String),
|
||||
#[error("error happened while analyzing file – {0}")]
|
||||
/// An error happened during the analysis of the song's samples by bliss.
|
||||
AnalysisError(String),
|
||||
#[error("error happened with the music library provider - {0}")]
|
||||
/// An error happened with the music library provider.
|
||||
/// Useful to report errors when you implement bliss for an audio player.
|
||||
ProviderError(String),
|
||||
}
|
||||
|
||||
/// bliss error type
|
||||
pub type BlissResult<T> = Result<T, BlissError>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_send_song() {
|
||||
fn assert_send<T: Send>() {}
|
||||
assert_send::<Song>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_song() {
|
||||
fn assert_sync<T: Send>() {}
|
||||
assert_sync::<Song>();
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ extern crate noisy_float;
|
|||
|
||||
use crate::utils::stft;
|
||||
use crate::utils::{hz_to_octs_inplace, Normalize};
|
||||
use crate::{BlissError, BlissResult};
|
||||
use crate::bliss_lib::{BlissError, BlissResult};
|
||||
use ndarray::{arr1, arr2, concatenate, s, Array, Array1, Array2, Axis, Zip};
|
||||
use ndarray_stats::interpolate::Midpoint;
|
||||
use ndarray_stats::QuantileExt;
|
||||
|
@ -53,13 +53,7 @@ impl ChromaDesc {
|
|||
*/
|
||||
pub fn do_(&mut self, signal: &[f32]) -> BlissResult<()> {
|
||||
let mut stft = stft(signal, ChromaDesc::WINDOW_SIZE, 2205);
|
||||
let tuning = estimate_tuning(
|
||||
self.sample_rate as u32,
|
||||
&stft,
|
||||
ChromaDesc::WINDOW_SIZE,
|
||||
0.01,
|
||||
12,
|
||||
)?;
|
||||
let tuning = estimate_tuning(self.sample_rate, &stft, ChromaDesc::WINDOW_SIZE, 0.01, 12)?;
|
||||
let chroma = chroma_stft(
|
||||
self.sample_rate,
|
||||
&mut stft,
|
||||
|
@ -162,7 +156,7 @@ fn chroma_filter(
|
|||
let n_chroma2 = (n_chroma_float / 2.0).round() as u32;
|
||||
let n_chroma2_float = f64::from(n_chroma2);
|
||||
|
||||
let frequencies = Array::linspace(0., f64::from(sample_rate), (n_fft + 1) as usize);
|
||||
let frequencies = Array::linspace(0., f64::from(sample_rate), n_fft + 1);
|
||||
|
||||
let mut freq_bins = frequencies;
|
||||
hz_to_octs_inplace(&mut freq_bins, tuning, n_chroma);
|
||||
|
@ -180,7 +174,7 @@ fn chroma_filter(
|
|||
}),
|
||||
);
|
||||
|
||||
let mut d: Array2<f64> = Array::zeros((n_chroma as usize, (&freq_bins).len()));
|
||||
let mut d: Array2<f64> = Array::zeros((n_chroma as usize, (freq_bins).len()));
|
||||
for (idx, mut row) in d.rows_mut().into_iter().enumerate() {
|
||||
row.fill(idx as f64);
|
||||
}
|
||||
|
@ -207,18 +201,18 @@ fn chroma_filter(
|
|||
wts *= &freq_bins;
|
||||
|
||||
// np.roll(), np bro
|
||||
let mut uninit: Vec<f64> = Vec::with_capacity((&wts).len());
|
||||
let mut uninit: Vec<f64> = vec![0.; (wts).len()];
|
||||
unsafe {
|
||||
uninit.set_len(wts.len());
|
||||
}
|
||||
let mut b = Array::from(uninit)
|
||||
.into_shape(wts.dim())
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e.to_string())))?;
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?;
|
||||
b.slice_mut(s![-3.., ..]).assign(&wts.slice(s![..3, ..]));
|
||||
b.slice_mut(s![..-3, ..]).assign(&wts.slice(s![3.., ..]));
|
||||
|
||||
wts = b;
|
||||
let non_aliased = (1 + n_fft / 2) as usize;
|
||||
let non_aliased = 1 + n_fft / 2;
|
||||
Ok(wts.slice_move(s![.., ..non_aliased]))
|
||||
}
|
||||
|
||||
|
@ -308,7 +302,7 @@ fn pitch_tuning(
|
|||
}
|
||||
let max_index = counts
|
||||
.argmax()
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e.to_string())))?;
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?;
|
||||
|
||||
// Return the bin with the most reoccuring frequency.
|
||||
Ok((-50. + (100. * resolution * max_index as f64)) / 100.)
|
||||
|
@ -321,7 +315,7 @@ fn estimate_tuning(
|
|||
resolution: f64,
|
||||
bins_per_octave: u32,
|
||||
) -> BlissResult<f64> {
|
||||
let (pitch, mag) = pip_track(sample_rate, &spectrum, n_fft)?;
|
||||
let (pitch, mag) = pip_track(sample_rate, spectrum, n_fft)?;
|
||||
|
||||
let (filtered_pitch, filtered_mag): (Vec<N64>, Vec<N64>) = pitch
|
||||
.iter()
|
||||
|
@ -330,11 +324,14 @@ fn estimate_tuning(
|
|||
.map(|(x, y)| (n64(*x), n64(*y)))
|
||||
.unzip();
|
||||
|
||||
if pitch.is_empty() {
|
||||
return Ok(0.);
|
||||
}
|
||||
|
||||
let threshold: N64 = Array::from(filtered_mag.to_vec())
|
||||
.quantile_axis_mut(Axis(0), n64(0.5), &Midpoint)
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e.to_string())))?
|
||||
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?
|
||||
.into_scalar();
|
||||
|
||||
let mut pitch = filtered_pitch
|
||||
.iter()
|
||||
.zip(&filtered_mag)
|
||||
|
@ -368,7 +365,7 @@ fn chroma_stft(
|
|||
mod test {
|
||||
use super::*;
|
||||
use crate::utils::stft;
|
||||
use crate::{Song, SAMPLE_RATE};
|
||||
use crate::bliss_lib::{Song, SAMPLE_RATE};
|
||||
use ndarray::{arr1, arr2, Array2};
|
||||
use ndarray_npy::ReadNpyExt;
|
||||
use std::fs::File;
|
||||
|
@ -440,7 +437,7 @@ mod test {
|
|||
fn test_chroma_desc() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut chroma_desc = ChromaDesc::new(SAMPLE_RATE, 12);
|
||||
chroma_desc.do_(&song.sample_array).unwrap();
|
||||
chroma_desc.do_(&song).unwrap();
|
||||
let expected_values = vec![
|
||||
-0.35661936,
|
||||
-0.63578653,
|
||||
|
@ -460,9 +457,7 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_chroma_stft_decode() {
|
||||
let signal = Song::decode(Path::new("data/s16_mono_22_5kHz.flac"))
|
||||
.unwrap()
|
||||
.sample_array;
|
||||
let signal = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut stft = stft(&signal, 8192, 2205);
|
||||
|
||||
let file = File::open("data/chroma.npy").unwrap();
|
||||
|
@ -486,11 +481,14 @@ mod test {
|
|||
assert!(0.000001 > (-0.09999999999999998 - tuning).abs());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chroma_estimate_tuning_empty_fix() {
|
||||
assert!(0. == estimate_tuning(22050, &Array2::zeros((8192, 1)), 8192, 0.01, 12).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_estimate_tuning_decode() {
|
||||
let signal = Song::decode(Path::new("data/s16_mono_22_5kHz.flac"))
|
||||
.unwrap()
|
||||
.sample_array;
|
||||
let signal = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let stft = stft(&signal, 8192, 2205);
|
||||
|
||||
let tuning = estimate_tuning(22050, &stft, 8192, 0.01, 12).unwrap();
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
//! Module containing various distance metric functions.
|
||||
//!
|
||||
//! All of these functions are intended to be used with the
|
||||
//! [custom_distance](Song::custom_distance) method, or with
|
||||
//! [playlist_from_songs_custom_distance](Library::playlist_from_song_custom_distance).
|
||||
//!
|
||||
//! They will yield different styles of playlists, so don't hesitate to
|
||||
//! experiment with them if the default (euclidean distance for now) doesn't
|
||||
//! suit you.
|
||||
use crate::NUMBER_FEATURES;
|
||||
#[cfg(doc)]
|
||||
use crate::{Library, Song};
|
||||
use ndarray::{Array, Array1};
|
||||
|
||||
/// Convenience trait for user-defined distance metrics.
|
||||
pub trait DistanceMetric: Fn(&Array1<f32>, &Array1<f32>) -> f32 {}
|
||||
impl<F> DistanceMetric for F where F: Fn(&Array1<f32>, &Array1<f32>) -> f32 {}
|
||||
|
||||
/// Return the [euclidean
|
||||
/// distance](https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions)
|
||||
/// between two vectors.
|
||||
pub fn euclidean_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
||||
// Could be any square symmetric positive semi-definite matrix;
|
||||
// just no metric learning has been done yet.
|
||||
// See https://lelele.io/thesis.pdf chapter 4.
|
||||
let m = Array::eye(NUMBER_FEATURES);
|
||||
|
||||
(a - b).dot(&m).dot(&(a - b)).sqrt()
|
||||
}
|
||||
|
||||
/// Return the [cosine
|
||||
/// distance](https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity)
|
||||
/// between two vectors.
|
||||
pub fn cosine_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
||||
let similarity = a.dot(b) / (a.dot(a).sqrt() * b.dot(b).sqrt());
|
||||
1. - similarity
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use ndarray::arr1;
|
||||
|
||||
#[test]
|
||||
fn test_euclidean_distance() {
|
||||
let a = arr1(&[
|
||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
|
||||
]);
|
||||
let b = arr1(&[
|
||||
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
|
||||
]);
|
||||
assert_eq!(euclidean_distance(&a, &b), 4.242640687119285);
|
||||
|
||||
let a = arr1(&[0.5; 20]);
|
||||
let b = arr1(&[0.5; 20]);
|
||||
assert_eq!(euclidean_distance(&a, &b), 0.);
|
||||
assert_eq!(euclidean_distance(&a, &b), 0.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cosine_distance() {
|
||||
let a = arr1(&[
|
||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
|
||||
]);
|
||||
let b = arr1(&[
|
||||
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
|
||||
]);
|
||||
assert_eq!(cosine_distance(&a, &b), 0.7705842661294382);
|
||||
|
||||
let a = arr1(&[0.5; 20]);
|
||||
let b = arr1(&[0.5; 20]);
|
||||
assert_eq!(cosine_distance(&a, &b), 0.);
|
||||
assert_eq!(cosine_distance(&a, &b), 0.);
|
||||
}
|
||||
}
|
253
src/lib.rs
253
src/lib.rs
|
@ -1,208 +1,67 @@
|
|||
//! # bliss audio library
|
||||
//!
|
||||
//! bliss is a library for making "smart" audio playlists.
|
||||
//!
|
||||
//! The core of the library is the `Song` object, which relates to a
|
||||
//! specific analyzed song and contains its path, title, analysis, and
|
||||
//! other metadata fields (album, genre...).
|
||||
//! Analyzing a song is as simple as running `Song::new("/path/to/song")`.
|
||||
//!
|
||||
//! The [analysis](Song::analysis) field of each song is an array of f32, which makes the
|
||||
//! comparison between songs easy, by just using euclidean distance (see
|
||||
//! [distance](Song::distance) for instance).
|
||||
//!
|
||||
//! Once several songs have been analyzed, making a playlist from one Song
|
||||
//! is as easy as computing distances between that song and the rest, and ordering
|
||||
//! the songs by distance, ascending.
|
||||
//!
|
||||
//! It is also convenient to make plug-ins for existing audio players.
|
||||
//! It should be as easy as implementing the necessary traits for [Library].
|
||||
//! A reference implementation for the MPD player is available
|
||||
//! [here](https://github.com/Polochon-street/blissify-rs)
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ## Analyze & compute the distance between two songs
|
||||
//! ```no_run
|
||||
//! use bliss_audio::{BlissResult, Song};
|
||||
//!
|
||||
//! fn main() -> BlissResult<()> {
|
||||
//! let song1 = Song::new("/path/to/song1")?;
|
||||
//! let song2 = Song::new("/path/to/song2")?;
|
||||
//!
|
||||
//! println!("Distance between song1 and song2 is {}", song1.distance(&song2));
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Make a playlist from a song
|
||||
//! ```no_run
|
||||
//! use bliss_audio::{BlissResult, Song};
|
||||
//! use noisy_float::prelude::n32;
|
||||
//!
|
||||
//! fn main() -> BlissResult<()> {
|
||||
//! let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"];
|
||||
//! let mut songs: Vec<Song> = paths
|
||||
//! .iter()
|
||||
//! .map(|path| Song::new(path))
|
||||
//! .collect::<BlissResult<Vec<Song>>>()?;
|
||||
//!
|
||||
//! // Assuming there is a first song
|
||||
//! let first_song = songs.first().unwrap().to_owned();
|
||||
//!
|
||||
//! songs.sort_by_cached_key(|song| n32(first_song.distance(&song)));
|
||||
//! println!(
|
||||
//! "Playlist is: {:?}",
|
||||
//! songs
|
||||
//! .iter()
|
||||
//! .map(|song| song.path.to_string_lossy().to_string())
|
||||
//! .collect::<Vec<String>>()
|
||||
//! );
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
#![cfg_attr(feature = "bench", feature(test))]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(missing_doc_code_examples)]
|
||||
pub mod bliss_lib;
|
||||
mod chroma;
|
||||
pub mod distance;
|
||||
mod library;
|
||||
mod misc;
|
||||
mod song;
|
||||
mod misc;
|
||||
mod temporal;
|
||||
mod timbral;
|
||||
mod utils;
|
||||
|
||||
extern crate crossbeam;
|
||||
extern crate num_cpus;
|
||||
#[cfg(feature = "serde")]
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
use thiserror::Error;
|
||||
use neon::{prelude::*, types::buffer::TypedArray};
|
||||
use song::Song;
|
||||
use bliss_lib::BlissResult;
|
||||
|
||||
pub use library::Library;
|
||||
pub use song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES};
|
||||
|
||||
const CHANNELS: u16 = 1;
|
||||
const SAMPLE_RATE: u32 = 22050;
|
||||
|
||||
#[derive(Error, Clone, Debug, PartialEq)]
|
||||
/// Umbrella type for bliss error types
|
||||
pub enum BlissError {
|
||||
#[error("error happened while decoding file – {0}")]
|
||||
/// An error happened while decoding an (audio) file
|
||||
DecodingError(String),
|
||||
#[error("error happened while analyzing file – {0}")]
|
||||
/// An error happened during the analysis of the samples by bliss
|
||||
AnalysisError(String),
|
||||
#[error("error happened with the music library provider - {0}")]
|
||||
/// An error happened with the music library provider.
|
||||
/// Useful to report errors when you implement the [Library] trait.
|
||||
ProviderError(String),
|
||||
#[neon::main]
|
||||
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||
cx.export_function("analyzeSync", analyze)?;
|
||||
cx.export_function("analyze", analyze_async)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// bliss error type
|
||||
pub type BlissResult<T> = Result<T, BlissError>;
|
||||
|
||||
/// Simple function to bulk analyze a set of songs represented by their
|
||||
/// absolute paths.
|
||||
///
|
||||
/// When making an extension for an audio player, prefer
|
||||
/// implementing the `Library` trait.
|
||||
#[doc(hidden)]
|
||||
pub fn bulk_analyse(paths: Vec<String>) -> Vec<BlissResult<Song>> {
|
||||
let mut songs = Vec::with_capacity(paths.len());
|
||||
let num_cpus = num_cpus::get();
|
||||
|
||||
crossbeam::scope(|s| {
|
||||
let mut handles = Vec::with_capacity(paths.len() / num_cpus);
|
||||
let mut chunk_number = paths.len() / num_cpus;
|
||||
if chunk_number == 0 {
|
||||
chunk_number = paths.len();
|
||||
}
|
||||
for chunk in paths.chunks(chunk_number) {
|
||||
handles.push(s.spawn(move |_| {
|
||||
let mut result = Vec::with_capacity(chunk.len());
|
||||
for path in chunk {
|
||||
let song = Song::new(&path);
|
||||
result.push(song);
|
||||
}
|
||||
result
|
||||
}));
|
||||
}
|
||||
|
||||
for handle in handles {
|
||||
songs.extend(handle.join().unwrap());
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
songs
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_send_song() {
|
||||
fn assert_send<T: Send>() {}
|
||||
assert_send::<Song>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_song() {
|
||||
fn assert_sync<T: Send>() {}
|
||||
assert_sync::<Song>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bulk_analyse() {
|
||||
let results = bulk_analyse(vec![
|
||||
String::from("data/s16_mono_22_5kHz.flac"),
|
||||
String::from("data/s16_mono_22_5kHz.flac"),
|
||||
String::from("nonexistent"),
|
||||
String::from("data/s16_stereo_22_5kHz.flac"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
String::from("nonexistent"),
|
||||
]);
|
||||
let mut errored_songs: Vec<String> = results
|
||||
.iter()
|
||||
.filter_map(|x| x.as_ref().err().map(|x| x.to_string()))
|
||||
.collect();
|
||||
errored_songs.sort_by(|a, b| a.cmp(b));
|
||||
|
||||
let mut analysed_songs: Vec<String> = results
|
||||
.iter()
|
||||
.filter_map(|x| {
|
||||
x.as_ref()
|
||||
.ok()
|
||||
.map(|x| x.path.to_str().unwrap().to_string())
|
||||
#[allow(deprecated)]
|
||||
fn analyze_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let path = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let promise = cx.task(move || {
|
||||
analyze_raw(&path)
|
||||
}).promise(|mut cx, result| {
|
||||
result
|
||||
.map(|(version_bytes, analysis_bytes)| {
|
||||
let mut buffer_handle = JsUint8Array::new(
|
||||
&mut cx,
|
||||
analysis_bytes.len() + version_bytes.len(),
|
||||
).unwrap();
|
||||
let buffer = buffer_handle.as_mut_slice(&mut cx);
|
||||
|
||||
buffer[0..version_bytes.len()].copy_from_slice(&version_bytes);
|
||||
buffer[version_bytes.len()..].copy_from_slice(&analysis_bytes);
|
||||
buffer_handle
|
||||
})
|
||||
.collect();
|
||||
analysed_songs.sort_by(|a, b| a.cmp(b));
|
||||
|
||||
assert_eq!(
|
||||
vec![
|
||||
String::from(
|
||||
"error happened while decoding file – while opening format: ffmpeg::Error(2: No such file or directory)."
|
||||
);
|
||||
8
|
||||
],
|
||||
errored_songs
|
||||
);
|
||||
assert_eq!(
|
||||
vec![
|
||||
String::from("data/s16_mono_22_5kHz.flac"),
|
||||
String::from("data/s16_mono_22_5kHz.flac"),
|
||||
String::from("data/s16_stereo_22_5kHz.flac"),
|
||||
],
|
||||
analysed_songs,
|
||||
);
|
||||
}
|
||||
.or_else(|e| cx.throw_error(e.to_string()))
|
||||
});
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
/// Returns a Uint8Array, with the first 2 bytes being the version (16-bit unsigned little endian)
|
||||
/// and the rest (currently 80 bytes) being the analysis data in little endian
|
||||
fn analyze(mut cx: FunctionContext) -> JsResult<JsUint8Array> {
|
||||
let path = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let (version_bytes, analysis_bytes) = analyze_raw(&path)
|
||||
.or_else(|e| cx.throw_error(e.to_string()))?;
|
||||
|
||||
let mut buffer_handle = JsUint8Array::new(
|
||||
&mut cx,
|
||||
analysis_bytes.len() + version_bytes.len(),
|
||||
)?;
|
||||
let buffer = buffer_handle.as_mut_slice(&mut cx);
|
||||
|
||||
buffer[0..version_bytes.len()].copy_from_slice(&version_bytes);
|
||||
buffer[version_bytes.len()..].copy_from_slice(&analysis_bytes);
|
||||
|
||||
Ok(buffer_handle)
|
||||
}
|
||||
|
||||
fn analyze_raw(path: &str) -> BlissResult<([u8; 2], [u8; 80])> {
|
||||
let song = Song::from_path(path)?;
|
||||
let version_bytes = song.features_version.to_le_bytes();
|
||||
let analysis_bytes = song.analysis.as_bytes();
|
||||
Ok((version_bytes, analysis_bytes))
|
||||
}
|
||||
|
|
464
src/library.rs
464
src/library.rs
|
@ -1,464 +0,0 @@
|
|||
//! Module containing the Library trait, useful to get started to implement
|
||||
//! a plug-in for an audio player.
|
||||
#[cfg(doc)]
|
||||
use crate::distance;
|
||||
use crate::distance::DistanceMetric;
|
||||
use crate::{BlissError, BlissResult, Song};
|
||||
use log::{debug, error, info};
|
||||
use noisy_float::prelude::*;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::thread;
|
||||
|
||||
/// Library trait to make creating plug-ins for existing audio players easier.
|
||||
pub trait Library {
|
||||
/// Return the absolute path of all the songs in an
|
||||
/// audio player's music library.
|
||||
fn get_songs_paths(&self) -> BlissResult<Vec<String>>;
|
||||
/// Store an analyzed Song object in some (cold) storage, e.g.
|
||||
/// a database, a file...
|
||||
fn store_song(&mut self, song: &Song) -> BlissResult<()>;
|
||||
/// Log and / or store that an error happened while trying to decode and
|
||||
/// analyze a song.
|
||||
fn store_error_song(&mut self, song_path: String, error: BlissError) -> BlissResult<()>;
|
||||
/// Retrieve a list of all the stored Songs.
|
||||
///
|
||||
/// This should work only after having run `analyze_library` at least
|
||||
/// once.
|
||||
fn get_stored_songs(&self) -> BlissResult<Vec<Song>>;
|
||||
|
||||
/// Return a list of songs that are similar to ``first_song``.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `first_song` - The song the playlist will be built from.
|
||||
/// * `playlist_length` - The playlist length. If there are not enough
|
||||
/// songs in the library, it will be truncated to the size of the library.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A vector of `playlist_length` Songs, including `first_song`, that you
|
||||
/// most likely want to plug in your audio player by using something like
|
||||
/// `ret.map(|song| song.path.to_owned()).collect::<Vec<String>>()`.
|
||||
fn playlist_from_song(
|
||||
&self,
|
||||
first_song: Song,
|
||||
playlist_length: usize,
|
||||
) -> BlissResult<Vec<Song>> {
|
||||
let mut songs = self.get_stored_songs()?;
|
||||
songs.sort_by_cached_key(|song| n32(first_song.distance(&song)));
|
||||
|
||||
let playlist = songs
|
||||
.into_iter()
|
||||
.take(playlist_length)
|
||||
.collect::<Vec<Song>>();
|
||||
debug!(
|
||||
"Playlist created: {}",
|
||||
playlist
|
||||
.iter()
|
||||
.map(|s| format!("{:?}", &s))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n"),
|
||||
);
|
||||
Ok(playlist)
|
||||
}
|
||||
|
||||
/// Return a list of songs that are similar to ``first_song``, using a
|
||||
/// custom distance metric.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `first_song` - The song the playlist will be built from.
|
||||
/// * `playlist_length` - The playlist length. If there are not enough
|
||||
/// songs in the library, it will be truncated to the size of the library.
|
||||
/// * `distance` - a user-supplied valid distance metric, either taken
|
||||
/// from the [distance](distance) module, or made from scratch.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A vector of `playlist_length` Songs, including `first_song`, that you
|
||||
/// most likely want to plug in your audio player by using something like
|
||||
/// `ret.map(|song| song.path.to_owned()).collect::<Vec<String>>()`.
|
||||
///
|
||||
/// # Custom distance example
|
||||
///
|
||||
/// ```
|
||||
/// use ndarray::Array1;
|
||||
///
|
||||
/// fn manhattan_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
||||
/// (a - b).mapv(|x| x.abs()).sum()
|
||||
/// }
|
||||
/// ```
|
||||
fn playlist_from_song_custom_distance(
|
||||
&self,
|
||||
first_song: Song,
|
||||
playlist_length: usize,
|
||||
distance: impl DistanceMetric,
|
||||
) -> BlissResult<Vec<Song>> {
|
||||
let mut songs = self.get_stored_songs()?;
|
||||
songs.sort_by_cached_key(|song| n32(first_song.custom_distance(&song, &distance)));
|
||||
|
||||
let playlist = songs
|
||||
.into_iter()
|
||||
.take(playlist_length)
|
||||
.collect::<Vec<Song>>();
|
||||
debug!(
|
||||
"Playlist created: {}",
|
||||
playlist
|
||||
.iter()
|
||||
.map(|s| format!("{:?}", &s))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n"),
|
||||
);
|
||||
Ok(playlist)
|
||||
}
|
||||
|
||||
/// Analyze and store songs in `paths`, using `store_song` and
|
||||
/// `store_error_song` implementations.
|
||||
///
|
||||
/// Note: this is mostly useful for updating a song library. For the first
|
||||
/// run, you probably want to use `analyze_library`.
|
||||
fn analyze_paths(&mut self, paths: Vec<String>) -> BlissResult<()> {
|
||||
if paths.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut songs = Vec::new();
|
||||
for path in paths {
|
||||
info!("Analyzing file '{}'", path);
|
||||
let song = Song::new(&path);
|
||||
songs.push((path.to_string(), song));
|
||||
}
|
||||
|
||||
for (path, song) in songs {
|
||||
// A storage fail should just warn the user, but not abort the whole process
|
||||
match song {
|
||||
Ok(song) => {
|
||||
self.store_song(&song).unwrap_or_else(|e| {
|
||||
error!("Error while storing song '{}': {}", song.path.display(), e)
|
||||
});
|
||||
info!(
|
||||
"Analyzed and stored song '{}' successfully.",
|
||||
song.path.display()
|
||||
)
|
||||
}
|
||||
Err(e) => {
|
||||
self.store_error_song(path.to_string(), e.to_owned())
|
||||
.unwrap_or_else(|e| {
|
||||
error!("Error while storing errored song '{}': {}", path, e)
|
||||
});
|
||||
error!(
|
||||
"Analysis of song '{}': {} failed. Error has been stored.",
|
||||
path, e
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Analyzes a song library, using `get_songs_paths`, `store_song` and
|
||||
/// `store_error_song` implementations.
|
||||
fn analyze_library(&mut self) -> BlissResult<()> {
|
||||
let paths = self
|
||||
.get_songs_paths()
|
||||
.map_err(|e| BlissError::ProviderError(e.to_string()))?;
|
||||
self.analyze_paths(paths)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::song::Analysis;
|
||||
use ndarray::Array1;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Default)]
|
||||
struct TestLibrary {
|
||||
internal_storage: Vec<Song>,
|
||||
failed_files: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl Library for TestLibrary {
|
||||
fn get_songs_paths(&self) -> BlissResult<Vec<String>> {
|
||||
Ok(vec![
|
||||
String::from("./data/white_noise.flac"),
|
||||
String::from("./data/s16_mono_22_5kHz.flac"),
|
||||
String::from("not-existing.foo"),
|
||||
String::from("definitely-not-existing.foo"),
|
||||
])
|
||||
}
|
||||
|
||||
fn store_song(&mut self, song: &Song) -> BlissResult<()> {
|
||||
self.internal_storage.push(song.to_owned());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store_error_song(&mut self, song_path: String, error: BlissError) -> BlissResult<()> {
|
||||
self.failed_files.push((song_path, error.to_string()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_stored_songs(&self) -> BlissResult<Vec<Song>> {
|
||||
Ok(self.internal_storage.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FailingLibrary;
|
||||
|
||||
impl Library for FailingLibrary {
|
||||
fn get_songs_paths(&self) -> BlissResult<Vec<String>> {
|
||||
Err(BlissError::ProviderError(String::from(
|
||||
"Could not get songs path",
|
||||
)))
|
||||
}
|
||||
|
||||
fn store_song(&mut self, _: &Song) -> BlissResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_stored_songs(&self) -> BlissResult<Vec<Song>> {
|
||||
Err(BlissError::ProviderError(String::from(
|
||||
"Could not get stored songs",
|
||||
)))
|
||||
}
|
||||
|
||||
fn store_error_song(&mut self, _: String, _: BlissError) -> BlissResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FailingStorage;
|
||||
|
||||
impl Library for FailingStorage {
|
||||
fn get_songs_paths(&self) -> BlissResult<Vec<String>> {
|
||||
Ok(vec![
|
||||
String::from("./data/white_noise.flac"),
|
||||
String::from("./data/s16_mono_22_5kHz.flac"),
|
||||
String::from("not-existing.foo"),
|
||||
String::from("definitely-not-existing.foo"),
|
||||
])
|
||||
}
|
||||
|
||||
fn store_song(&mut self, song: &Song) -> BlissResult<()> {
|
||||
Err(BlissError::ProviderError(format!(
|
||||
"Could not store song {}",
|
||||
song.path.display()
|
||||
)))
|
||||
}
|
||||
|
||||
fn get_stored_songs(&self) -> BlissResult<Vec<Song>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn store_error_song(&mut self, song_path: String, error: BlissError) -> BlissResult<()> {
|
||||
Err(BlissError::ProviderError(format!(
|
||||
"Could not store errored song: {}, with error: {}",
|
||||
song_path, error
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_library_fail() {
|
||||
let mut test_library = FailingLibrary {};
|
||||
assert_eq!(
|
||||
test_library.analyze_library(),
|
||||
Err(BlissError::ProviderError(String::from(
|
||||
"error happened with the music library provider - Could not get songs path"
|
||||
))),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_playlist_from_song_fail() {
|
||||
let test_library = FailingLibrary {};
|
||||
let song = Song {
|
||||
path: Path::new("path-to-first").to_path_buf(),
|
||||
analysis: Analysis::new([0.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
test_library.playlist_from_song(song, 10),
|
||||
Err(BlissError::ProviderError(String::from(
|
||||
"Could not get stored songs"
|
||||
))),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_library_fail_storage() {
|
||||
let mut test_library = FailingStorage {};
|
||||
|
||||
// A storage fail should just warn the user, but not abort the whole process
|
||||
assert!(test_library.analyze_library().is_ok())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_library() {
|
||||
let mut test_library = TestLibrary {
|
||||
internal_storage: vec![],
|
||||
failed_files: vec![],
|
||||
};
|
||||
test_library.analyze_library().unwrap();
|
||||
|
||||
let mut failed_files = test_library
|
||||
.failed_files
|
||||
.iter()
|
||||
.map(|x| x.0.to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
failed_files.sort();
|
||||
|
||||
assert_eq!(
|
||||
failed_files,
|
||||
vec![
|
||||
String::from("definitely-not-existing.foo"),
|
||||
String::from("not-existing.foo"),
|
||||
],
|
||||
);
|
||||
|
||||
let mut songs = test_library
|
||||
.internal_storage
|
||||
.iter()
|
||||
.map(|x| x.path.to_str().unwrap().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
songs.sort();
|
||||
|
||||
assert_eq!(
|
||||
songs,
|
||||
vec![
|
||||
String::from("./data/s16_mono_22_5kHz.flac"),
|
||||
String::from("./data/white_noise.flac"),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_playlist_from_song() {
|
||||
let mut test_library = TestLibrary::default();
|
||||
let first_song = Song {
|
||||
path: Path::new("path-to-first").to_path_buf(),
|
||||
analysis: Analysis::new([0.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let second_song = Song {
|
||||
path: Path::new("path-to-second").to_path_buf(),
|
||||
analysis: Analysis::new([0.1; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let third_song = Song {
|
||||
path: Path::new("path-to-third").to_path_buf(),
|
||||
analysis: Analysis::new([10.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let fourth_song = Song {
|
||||
path: Path::new("path-to-fourth").to_path_buf(),
|
||||
analysis: Analysis::new([20.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
test_library.internal_storage = vec![
|
||||
first_song.to_owned(),
|
||||
fourth_song.to_owned(),
|
||||
third_song.to_owned(),
|
||||
second_song.to_owned(),
|
||||
];
|
||||
assert_eq!(
|
||||
vec![first_song.to_owned(), second_song, third_song],
|
||||
test_library.playlist_from_song(first_song, 3).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_playlist_from_song_too_little_songs() {
|
||||
let mut test_library = TestLibrary::default();
|
||||
let first_song = Song {
|
||||
path: Path::new("path-to-first").to_path_buf(),
|
||||
analysis: Analysis::new([0.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let second_song = Song {
|
||||
path: Path::new("path-to-second").to_path_buf(),
|
||||
analysis: Analysis::new([0.1; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let third_song = Song {
|
||||
path: Path::new("path-to-third").to_path_buf(),
|
||||
analysis: Analysis::new([10.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
test_library.internal_storage = vec![
|
||||
first_song.to_owned(),
|
||||
second_song.to_owned(),
|
||||
third_song.to_owned(),
|
||||
];
|
||||
assert_eq!(
|
||||
vec![first_song.to_owned(), second_song, third_song],
|
||||
test_library.playlist_from_song(first_song, 200).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_empty_path() {
|
||||
let mut test_library = TestLibrary::default();
|
||||
assert!(test_library.analyze_paths(vec![]).is_ok());
|
||||
}
|
||||
|
||||
fn custom_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
||||
if a == b {
|
||||
return 0.;
|
||||
}
|
||||
1. / (a.first().unwrap() - b.first().unwrap()).abs()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_playlist_from_song_custom_distance() {
|
||||
let mut test_library = TestLibrary::default();
|
||||
let first_song = Song {
|
||||
path: Path::new("path-to-first").to_path_buf(),
|
||||
analysis: Analysis::new([0.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let second_song = Song {
|
||||
path: Path::new("path-to-second").to_path_buf(),
|
||||
analysis: Analysis::new([0.1; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let third_song = Song {
|
||||
path: Path::new("path-to-third").to_path_buf(),
|
||||
analysis: Analysis::new([10.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let fourth_song = Song {
|
||||
path: Path::new("path-to-fourth").to_path_buf(),
|
||||
analysis: Analysis::new([20.; 20]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
test_library.internal_storage = vec![
|
||||
first_song.to_owned(),
|
||||
fourth_song.to_owned(),
|
||||
third_song.to_owned(),
|
||||
second_song.to_owned(),
|
||||
];
|
||||
assert_eq!(
|
||||
vec![first_song.to_owned(), fourth_song, third_song],
|
||||
test_library
|
||||
.playlist_from_song_custom_distance(first_song, 3, custom_distance)
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
}
|
|
@ -63,14 +63,14 @@ impl Normalize for LoudnessDesc {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Song;
|
||||
use crate::bliss_lib::Song;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn test_loudness() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut loudness_desc = LoudnessDesc::default();
|
||||
for chunk in song.sample_array.chunks_exact(LoudnessDesc::WINDOW_SIZE) {
|
||||
for chunk in song.chunks_exact(LoudnessDesc::WINDOW_SIZE) {
|
||||
loudness_desc.do_(&chunk);
|
||||
}
|
||||
let expected_values = vec![0.271263, 0.2577181];
|
||||
|
|
611
src/song.rs
611
src/song.rs
|
@ -7,24 +7,18 @@
|
|||
//! For implementation of plug-ins for already existing audio players,
|
||||
//! a look at Library is instead recommended.
|
||||
|
||||
extern crate crossbeam;
|
||||
extern crate ffmpeg_next as ffmpeg;
|
||||
extern crate ndarray;
|
||||
extern crate ndarray_npy;
|
||||
|
||||
use super::CHANNELS;
|
||||
use crate::chroma::ChromaDesc;
|
||||
use crate::distance::{euclidean_distance, DistanceMetric};
|
||||
use crate::misc::LoudnessDesc;
|
||||
use crate::temporal::BPMDesc;
|
||||
use crate::timbral::{SpectralDesc, ZeroCrossingRateDesc};
|
||||
use crate::{BlissError, BlissResult, SAMPLE_RATE};
|
||||
use crate::bliss_lib::{BlissError, BlissResult, SAMPLE_RATE};
|
||||
use crate::bliss_lib::{CHANNELS, FEATURES_VERSION};
|
||||
use ::log::warn;
|
||||
use core::ops::Index;
|
||||
use crossbeam::thread;
|
||||
use ffmpeg_next::codec::threading::{Config, Type as ThreadingType};
|
||||
use ffmpeg_next::software::resampling::context::Context;
|
||||
use ffmpeg_next::util;
|
||||
use ffmpeg_next::util::channel_layout::ChannelLayout;
|
||||
use ffmpeg_next::util::error::Error;
|
||||
use ffmpeg_next::util::error::EINVAL;
|
||||
|
@ -32,14 +26,14 @@ use ffmpeg_next::util::format::sample::{Sample, Type};
|
|||
use ffmpeg_next::util::frame::audio::Audio;
|
||||
use ffmpeg_next::util::log;
|
||||
use ffmpeg_next::util::log::level::Level;
|
||||
use ffmpeg_next::{media, util};
|
||||
use ndarray::{arr1, Array1};
|
||||
use std::convert::TryInto;
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::thread as std_thread;
|
||||
use std::thread;
|
||||
use strum::{EnumCount, IntoEnumIterator};
|
||||
use strum_macros::{EnumCount, EnumIter};
|
||||
|
||||
|
@ -48,20 +42,12 @@ use strum_macros::{EnumCount, EnumIter};
|
|||
/// Simple object used to represent a Song, with its path, analysis, and
|
||||
/// other metadata (artist, genre...)
|
||||
pub struct Song {
|
||||
/// Song's provided file path
|
||||
pub path: PathBuf,
|
||||
/// Song's artist, read from the metadata
|
||||
pub artist: Option<String>,
|
||||
/// Song's title, read from the metadata
|
||||
pub title: Option<String>,
|
||||
/// Song's album name, read from the metadata
|
||||
pub album: Option<String>,
|
||||
/// Song's tracked number, read from the metadata
|
||||
pub track_number: Option<String>,
|
||||
/// Song's genre, read from the metadata (`""` if empty)
|
||||
pub genre: Option<String>,
|
||||
/// bliss analysis results
|
||||
pub analysis: Analysis,
|
||||
/// Version of the features the song was analyzed with.
|
||||
/// A simple integer that is bumped every time a breaking change
|
||||
/// is introduced in the features.
|
||||
pub features_version: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, EnumIter, EnumCount)]
|
||||
|
@ -72,7 +58,7 @@ pub struct Song {
|
|||
/// use bliss_audio::{AnalysisIndex, BlissResult, Song};
|
||||
///
|
||||
/// fn main() -> BlissResult<()> {
|
||||
/// let song = Song::new("path/to/song")?;
|
||||
/// let song = Song::from_path("path/to/song")?;
|
||||
/// println!("{}", song.analysis[AnalysisIndex::Tempo]);
|
||||
/// Ok(())
|
||||
/// }
|
||||
|
@ -125,7 +111,7 @@ pub const NUMBER_FEATURES: usize = AnalysisIndex::COUNT;
|
|||
/// on most of the features, except the chroma ones, which are documented
|
||||
/// directly in this code.
|
||||
pub struct Analysis {
|
||||
internal_analysis: [f32; NUMBER_FEATURES],
|
||||
pub(crate) internal_analysis: [f32; NUMBER_FEATURES],
|
||||
}
|
||||
|
||||
impl Index<AnalysisIndex> for Analysis {
|
||||
|
@ -140,10 +126,10 @@ impl fmt::Debug for Analysis {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut debug_struct = f.debug_struct("Analysis");
|
||||
for feature in AnalysisIndex::iter() {
|
||||
debug_struct.field(&format!("{:?}", feature), &self[feature]);
|
||||
debug_struct.field(&format!("{feature:?}"), &self[feature]);
|
||||
}
|
||||
debug_struct.finish()?;
|
||||
f.write_str(&format!(" /* {:?} */", &self.to_vec()))
|
||||
f.write_str(&format!(" /* {:?} */", &self.as_vec()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -162,7 +148,7 @@ impl Analysis {
|
|||
/// Return an ndarray `Array1` representing the analysis' features.
|
||||
///
|
||||
/// Particularly useful if you want to make a custom distance metric.
|
||||
pub fn to_arr1(&self) -> Array1<f32> {
|
||||
pub fn as_arr1(&self) -> Array1<f32> {
|
||||
arr1(&self.internal_analysis)
|
||||
}
|
||||
|
||||
|
@ -170,67 +156,46 @@ impl Analysis {
|
|||
///
|
||||
/// Particularly useful if you want iterate through the values to store
|
||||
/// them somewhere.
|
||||
pub fn to_vec(&self) -> Vec<f32> {
|
||||
pub fn as_vec(&self) -> Vec<f32> {
|
||||
self.internal_analysis.to_vec()
|
||||
}
|
||||
|
||||
/// Compute distance between two analysis using a user-provided distance
|
||||
/// metric. You most likely want to use `song.custom_distance` directly
|
||||
/// rather than this function.
|
||||
///
|
||||
/// For this function to be integrated properly with the rest
|
||||
/// of bliss' parts, it should be a valid distance metric, i.e.:
|
||||
/// 1. For X, Y real vectors, d(X, Y) = 0 ⇔ X = Y
|
||||
/// 2. For X, Y real vectors, d(X, Y) >= 0
|
||||
/// 3. For X, Y real vectors, d(X, Y) = d(Y, X)
|
||||
/// 4. For X, Y, Z real vectors d(X, Y) ≤ d(X + Z) + d(Z, Y)
|
||||
///
|
||||
/// Note that almost all distance metrics you will find obey these
|
||||
/// properties, so don't sweat it too much.
|
||||
pub fn custom_distance(&self, other: &Self, distance: impl DistanceMetric) -> f32 {
|
||||
distance(&self.to_arr1(), &other.to_arr1())
|
||||
/// Returns a little endian byte array representing the analysis' features.
|
||||
pub fn as_bytes(&self) -> [u8; 80] {
|
||||
let mut result = [0; 80];
|
||||
for (i, float) in self.internal_analysis.iter().enumerate() {
|
||||
let [a, b, c, d] = float.to_le_bytes();
|
||||
result[4*i] = a;
|
||||
result[4*i + 1] = b;
|
||||
result[4*i + 2] = c;
|
||||
result[4*i + 3] = d;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Creates an Analysis object from a little endian byte array
|
||||
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
||||
let floats = bytes
|
||||
.chunks(4)
|
||||
.map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
|
||||
.collect::<Vec<_>>();
|
||||
if floats.len() != NUMBER_FEATURES {
|
||||
return None;
|
||||
}
|
||||
match floats.try_into() {
|
||||
Ok(arr) => Some(Analysis { internal_analysis: arr }),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Song {
|
||||
#[allow(dead_code)]
|
||||
/// Compute the distance between the current song and any given
|
||||
/// Song.
|
||||
///
|
||||
/// The smaller the number, the closer the songs; usually more useful
|
||||
/// if compared between several songs
|
||||
/// (e.g. if song1.distance(song2) < song1.distance(song3), then song1 is
|
||||
/// closer to song2 than it is to song3.
|
||||
///
|
||||
/// Currently uses the euclidean distance, but this can change in an
|
||||
/// upcoming release if another metric performs better.
|
||||
pub fn distance(&self, other: &Self) -> f32 {
|
||||
self.analysis
|
||||
.custom_distance(&other.analysis, euclidean_distance)
|
||||
}
|
||||
|
||||
/// Compute distance between two songs using a user-provided distance
|
||||
/// metric.
|
||||
///
|
||||
/// For this function to be integrated properly with the rest
|
||||
/// of bliss' parts, it should be a valid distance metric, i.e.:
|
||||
/// 1. For X, Y real vectors, d(X, Y) = 0 ⇔ X = Y
|
||||
/// 2. For X, Y real vectors, d(X, Y) >= 0
|
||||
/// 3. For X, Y real vectors, d(X, Y) = d(Y, X)
|
||||
/// 4. For X, Y, Z real vectors d(X, Y) ≤ d(X + Z) + d(Z, Y)
|
||||
///
|
||||
/// Note that almost all distance metrics you will find obey these
|
||||
/// properties, so don't sweat it too much.
|
||||
pub fn custom_distance(&self, other: &Self, distance: impl DistanceMetric) -> f32 {
|
||||
self.analysis.custom_distance(&other.analysis, distance)
|
||||
}
|
||||
|
||||
/// Returns a decoded Song given a file path, or an error if the song
|
||||
/// Returns a decoded [Song] given a file path, or an error if the song
|
||||
/// could not be analyzed for some reason.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `path` - A string holding a valid file path to a valid audio file.
|
||||
/// * `path` - A [Path] holding a valid file path to a valid audio file.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
|
@ -241,22 +206,21 @@ impl Song {
|
|||
/// The error type returned should give a hint as to whether it was a
|
||||
/// decoding ([DecodingError](BlissError::DecodingError)) or an analysis
|
||||
/// ([AnalysisError](BlissError::AnalysisError)) error.
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> BlissResult<Self> {
|
||||
let raw_song = Song::decode(path.as_ref())?;
|
||||
pub fn from_path<P: AsRef<Path>>(path: P) -> BlissResult<Self> {
|
||||
let samples = Song::decode(path.as_ref())?;
|
||||
|
||||
Ok(Song {
|
||||
path: raw_song.path,
|
||||
artist: raw_song.artist,
|
||||
title: raw_song.title,
|
||||
album: raw_song.album,
|
||||
track_number: raw_song.track_number,
|
||||
genre: raw_song.genre,
|
||||
analysis: Song::analyse(raw_song.sample_array)?,
|
||||
analysis: Song::analyze(&samples)?,
|
||||
features_version: FEATURES_VERSION,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyse a song decoded in `sample_array`, with one channel @ 22050 Hz.
|
||||
* Analyze a song decoded in `sample_array`. This function should NOT
|
||||
* be used manually, unless you want to explore analyzing a sample array you
|
||||
* already decoded yourself. Most people will want to use
|
||||
* [Song::from_path](Song::from_path) instead to just analyze a file from
|
||||
* its path.
|
||||
*
|
||||
* The current implementation doesn't make use of it,
|
||||
* but the song can also be streamed wrt.
|
||||
|
@ -265,8 +229,21 @@ impl Song {
|
|||
*
|
||||
* Useful in the rare cases where the full song is not
|
||||
* completely available.
|
||||
*
|
||||
* If you *do* want to use this with a song already decoded by yourself,
|
||||
* the sample format of `sample_array` should be f32le, one channel, and
|
||||
* the sampling rate 22050 Hz. Anything other than that will yield aberrant
|
||||
* results.
|
||||
* To double-check that your sample array has the right format, you could run
|
||||
* `ffmpeg -i path_to_your_song.flac -ar 22050 -ac 1 -c:a pcm_f32le -f hash -hash addler32 -`,
|
||||
* which will give you the addler32 checksum of the sample array if the song
|
||||
* has been decoded properly. You can then compute the addler32 checksum of your sample
|
||||
* array (see `_test_decode` in the tests) and make sure both are the same.
|
||||
*
|
||||
* (Running `ffmpeg -i path_to_your_song.flac -ar 22050 -ac 1 -c:a pcm_f32le` will simply give
|
||||
* you the raw sample array as it should look like, if you're not into computing checksums)
|
||||
**/
|
||||
fn analyse(sample_array: Vec<f32>) -> BlissResult<Analysis> {
|
||||
pub fn analyze(sample_array: &[f32]) -> BlissResult<Analysis> {
|
||||
let largest_window = vec![
|
||||
BPMDesc::WINDOW_SIZE,
|
||||
ChromaDesc::WINDOW_SIZE,
|
||||
|
@ -282,36 +259,33 @@ impl Song {
|
|||
)));
|
||||
}
|
||||
|
||||
thread::scope(|s| {
|
||||
let child_tempo: thread::ScopedJoinHandle<'_, BlissResult<f32>> = s.spawn(|_| {
|
||||
thread::scope(|s| -> BlissResult<Analysis> {
|
||||
let child_tempo = s.spawn(|| {
|
||||
let mut tempo_desc = BPMDesc::new(SAMPLE_RATE)?;
|
||||
let windows = sample_array
|
||||
.windows(BPMDesc::WINDOW_SIZE)
|
||||
.step_by(BPMDesc::HOP_SIZE);
|
||||
|
||||
for window in windows {
|
||||
tempo_desc.do_(&window)?;
|
||||
tempo_desc.do_(window)?;
|
||||
}
|
||||
Ok(tempo_desc.get_value())
|
||||
});
|
||||
|
||||
let child_chroma: thread::ScopedJoinHandle<'_, BlissResult<Vec<f32>>> = s.spawn(|_| {
|
||||
let child_chroma = s.spawn(|| {
|
||||
let mut chroma_desc = ChromaDesc::new(SAMPLE_RATE, 12);
|
||||
chroma_desc.do_(&sample_array)?;
|
||||
chroma_desc.do_(sample_array)?;
|
||||
Ok(chroma_desc.get_values())
|
||||
});
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
let child_timbral: thread::ScopedJoinHandle<
|
||||
'_,
|
||||
BlissResult<(Vec<f32>, Vec<f32>, Vec<f32>)>,
|
||||
> = s.spawn(|_| {
|
||||
let child_timbral = s.spawn(|| {
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE)?;
|
||||
let windows = sample_array
|
||||
.windows(SpectralDesc::WINDOW_SIZE)
|
||||
.step_by(SpectralDesc::HOP_SIZE);
|
||||
for window in windows {
|
||||
spectral_desc.do_(&window)?;
|
||||
spectral_desc.do_(window)?;
|
||||
}
|
||||
let centroid = spectral_desc.get_centroid();
|
||||
let rolloff = spectral_desc.get_rolloff();
|
||||
|
@ -319,22 +293,21 @@ impl Song {
|
|||
Ok((centroid, rolloff, flatness))
|
||||
});
|
||||
|
||||
let child_zcr: thread::ScopedJoinHandle<'_, BlissResult<f32>> = s.spawn(|_| {
|
||||
let child_zcr = s.spawn(|| {
|
||||
let mut zcr_desc = ZeroCrossingRateDesc::default();
|
||||
zcr_desc.do_(&sample_array);
|
||||
zcr_desc.do_(sample_array);
|
||||
Ok(zcr_desc.get_value())
|
||||
});
|
||||
|
||||
let child_loudness: thread::ScopedJoinHandle<'_, BlissResult<Vec<f32>>> =
|
||||
s.spawn(|_| {
|
||||
let mut loudness_desc = LoudnessDesc::default();
|
||||
let windows = sample_array.chunks(LoudnessDesc::WINDOW_SIZE);
|
||||
let child_loudness = s.spawn(|| {
|
||||
let mut loudness_desc = LoudnessDesc::default();
|
||||
let windows = sample_array.chunks(LoudnessDesc::WINDOW_SIZE);
|
||||
|
||||
for window in windows {
|
||||
loudness_desc.do_(&window);
|
||||
}
|
||||
Ok(loudness_desc.get_value())
|
||||
});
|
||||
for window in windows {
|
||||
loudness_desc.do_(window);
|
||||
}
|
||||
Ok(loudness_desc.get_value())
|
||||
});
|
||||
|
||||
// Non-streaming approach for that one
|
||||
let tempo = child_tempo.join().unwrap()?;
|
||||
|
@ -358,33 +331,53 @@ impl Song {
|
|||
})?;
|
||||
Ok(Analysis::new(array))
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn decode(path: &Path) -> BlissResult<InternalSong> {
|
||||
ffmpeg::init()
|
||||
.map_err(|e| BlissError::DecodingError(format!("ffmpeg init error: {:?}.", e)))?;
|
||||
pub(crate) fn decode(path: &Path) -> BlissResult<Vec<f32>> {
|
||||
ffmpeg::init().map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"ffmpeg init error while decoding file '{}': {:?}.",
|
||||
path.display(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
log::set_level(Level::Quiet);
|
||||
let mut song = InternalSong {
|
||||
path: path.into(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut format = ffmpeg::format::input(&path)
|
||||
.map_err(|e| BlissError::DecodingError(format!("while opening format: {:?}.", e)))?;
|
||||
let (mut codec, stream, expected_sample_number) = {
|
||||
let stream = format
|
||||
.streams()
|
||||
.find(|s| s.codec().medium() == ffmpeg::media::Type::Audio)
|
||||
.ok_or_else(|| BlissError::DecodingError(String::from("No audio stream found.")))?;
|
||||
stream.codec().set_threading(Config {
|
||||
|
||||
let mut ictx = ffmpeg::format::input(&path).map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"while opening format for file '{}': {:?}.",
|
||||
path.display(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
let (mut decoder, stream, expected_sample_number) = {
|
||||
let input = ictx.streams().best(media::Type::Audio).ok_or_else(|| {
|
||||
BlissError::DecodingError(format!(
|
||||
"No audio stream found for file '{}'.",
|
||||
path.display()
|
||||
))
|
||||
})?;
|
||||
let mut context = ffmpeg::codec::context::Context::from_parameters(input.parameters())
|
||||
.map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"Could not load the codec context for file '{}': {:?}",
|
||||
path.display(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
context.set_threading(Config {
|
||||
kind: ThreadingType::Frame,
|
||||
count: 0,
|
||||
safe: true,
|
||||
// safe: true,
|
||||
});
|
||||
let codec =
|
||||
stream.codec().decoder().audio().map_err(|e| {
|
||||
BlissError::DecodingError(format!("when finding codec: {:?}.", e))
|
||||
})?;
|
||||
let decoder = context.decoder().audio().map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"when finding decoder for file '{}': {:?}.",
|
||||
path.display(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
|
||||
// Add SAMPLE_RATE to have one second margin to avoid reallocating if
|
||||
// the duration is slightly more than estimated
|
||||
// TODO>1.0 another way to get the exact number of samples is to decode
|
||||
|
@ -392,96 +385,68 @@ impl Song {
|
|||
// allocate the array with that number, and decode again. Check
|
||||
// what's faster between reallocating, and just have one second
|
||||
// leeway.
|
||||
let expected_sample_number = (SAMPLE_RATE as f32 * stream.duration() as f32
|
||||
/ stream.time_base().denominator() as f32)
|
||||
let expected_sample_number = (SAMPLE_RATE as f32 * input.duration() as f32
|
||||
/ input.time_base().denominator() as f32)
|
||||
.ceil()
|
||||
+ SAMPLE_RATE as f32;
|
||||
(codec, stream.index(), expected_sample_number)
|
||||
(decoder, input.index(), expected_sample_number)
|
||||
};
|
||||
let sample_array: Vec<f32> = Vec::with_capacity(expected_sample_number as usize);
|
||||
if let Some(title) = format.metadata().get("title") {
|
||||
song.title = match title {
|
||||
"" => None,
|
||||
t => Some(t.to_string()),
|
||||
};
|
||||
};
|
||||
if let Some(artist) = format.metadata().get("artist") {
|
||||
song.artist = match artist {
|
||||
"" => None,
|
||||
a => Some(a.to_string()),
|
||||
};
|
||||
};
|
||||
if let Some(album) = format.metadata().get("album") {
|
||||
song.album = match album {
|
||||
"" => None,
|
||||
a => Some(a.to_string()),
|
||||
};
|
||||
};
|
||||
if let Some(genre) = format.metadata().get("genre") {
|
||||
song.genre = match genre {
|
||||
"" => None,
|
||||
g => Some(g.to_string()),
|
||||
};
|
||||
};
|
||||
if let Some(track_number) = format.metadata().get("track") {
|
||||
song.track_number = match track_number {
|
||||
"" => None,
|
||||
t => Some(t.to_string()),
|
||||
};
|
||||
};
|
||||
let in_channel_layout = {
|
||||
if codec.channel_layout() == ChannelLayout::empty() {
|
||||
ChannelLayout::default(codec.channels().into())
|
||||
|
||||
let (empty_in_channel_layout, in_channel_layout) = {
|
||||
if decoder.channel_layout() == ChannelLayout::empty() {
|
||||
(true, ChannelLayout::default(decoder.channels().into()))
|
||||
} else {
|
||||
codec.channel_layout()
|
||||
(false, decoder.channel_layout())
|
||||
}
|
||||
};
|
||||
codec.set_channel_layout(in_channel_layout);
|
||||
let resample_context = ffmpeg::software::resampling::context::Context::get(
|
||||
codec.format(),
|
||||
in_channel_layout,
|
||||
codec.rate(),
|
||||
Sample::F32(Type::Packed),
|
||||
ffmpeg::util::channel_layout::ChannelLayout::MONO,
|
||||
SAMPLE_RATE,
|
||||
)
|
||||
.map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"while trying to allocate resampling context: {:?}",
|
||||
e
|
||||
))
|
||||
})?;
|
||||
decoder.set_channel_layout(in_channel_layout);
|
||||
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let child = std_thread::spawn(move || resample_frame(rx, resample_context, sample_array));
|
||||
for (s, packet) in format.packets() {
|
||||
let in_codec_format = decoder.format();
|
||||
let in_codec_rate = decoder.rate();
|
||||
let child = thread::spawn(move || {
|
||||
resample_frame(
|
||||
rx,
|
||||
in_codec_format,
|
||||
in_channel_layout,
|
||||
in_codec_rate,
|
||||
sample_array,
|
||||
empty_in_channel_layout,
|
||||
)
|
||||
});
|
||||
for (s, packet) in ictx.packets() {
|
||||
if s.index() != stream {
|
||||
continue;
|
||||
}
|
||||
match codec.send_packet(&packet) {
|
||||
match decoder.send_packet(&packet) {
|
||||
Ok(_) => (),
|
||||
Err(Error::Other { errno: EINVAL }) => {
|
||||
return Err(BlissError::DecodingError(String::from(
|
||||
"wrong codec opened.",
|
||||
return Err(BlissError::DecodingError(format!(
|
||||
"wrong codec opened for file '{}.",
|
||||
path.display(),
|
||||
)))
|
||||
}
|
||||
Err(Error::Eof) => {
|
||||
warn!("Premature EOF reached while decoding.");
|
||||
warn!(
|
||||
"Premature EOF reached while decoding file '{}'.",
|
||||
path.display()
|
||||
);
|
||||
drop(tx);
|
||||
song.sample_array = child.join().unwrap()?;
|
||||
return Ok(song);
|
||||
return Ok(child.join().unwrap()?);
|
||||
}
|
||||
Err(e) => warn!("decoding error: {}", e),
|
||||
Err(e) => warn!("error while decoding file '{}': {}", path.display(), e),
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut decoded = ffmpeg::frame::Audio::empty();
|
||||
match codec.receive_frame(&mut decoded) {
|
||||
match decoder.receive_frame(&mut decoded) {
|
||||
Ok(_) => {
|
||||
tx.send(decoded).map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"while sending decoded frame to the resampling thread: {:?}",
|
||||
e
|
||||
"while sending decoded frame to the resampling thread for file '{}': {:?}",
|
||||
path.display(),
|
||||
e,
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
@ -492,29 +457,33 @@ impl Song {
|
|||
|
||||
// Flush the stream
|
||||
let packet = ffmpeg::codec::packet::Packet::empty();
|
||||
match codec.send_packet(&packet) {
|
||||
match decoder.send_packet(&packet) {
|
||||
Ok(_) => (),
|
||||
Err(Error::Other { errno: EINVAL }) => {
|
||||
return Err(BlissError::DecodingError(String::from(
|
||||
"wrong codec opened.",
|
||||
return Err(BlissError::DecodingError(format!(
|
||||
"wrong codec opened for file '{}'.",
|
||||
path.display()
|
||||
)))
|
||||
}
|
||||
Err(Error::Eof) => {
|
||||
warn!("Premature EOF reached while decoding.");
|
||||
warn!(
|
||||
"Premature EOF reached while decoding file '{}'.",
|
||||
path.display()
|
||||
);
|
||||
drop(tx);
|
||||
song.sample_array = child.join().unwrap()?;
|
||||
return Ok(song);
|
||||
return Ok(child.join().unwrap()?);
|
||||
}
|
||||
Err(e) => warn!("decoding error: {}", e),
|
||||
Err(e) => warn!("error while decoding {}: {}", path.display(), e),
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut decoded = ffmpeg::frame::Audio::empty();
|
||||
match codec.receive_frame(&mut decoded) {
|
||||
match decoder.receive_frame(&mut decoded) {
|
||||
Ok(_) => {
|
||||
tx.send(decoded).map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"while sending decoded frame to the resampling thread: {:?}",
|
||||
"while sending decoded frame to the resampling thread for file '{}': {:?}",
|
||||
path.display(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
|
@ -524,42 +493,64 @@ impl Song {
|
|||
}
|
||||
|
||||
drop(tx);
|
||||
song.sample_array = child.join().unwrap()?;
|
||||
Ok(song)
|
||||
Ok(child.join().unwrap()?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct InternalSong {
|
||||
pub path: PathBuf,
|
||||
pub artist: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub album: Option<String>,
|
||||
pub track_number: Option<String>,
|
||||
pub genre: Option<String>,
|
||||
pub sample_array: Vec<f32>,
|
||||
}
|
||||
|
||||
fn resample_frame(
|
||||
rx: Receiver<Audio>,
|
||||
mut resample_context: Context,
|
||||
in_codec_format: Sample,
|
||||
in_channel_layout: ChannelLayout,
|
||||
in_rate: u32,
|
||||
mut sample_array: Vec<f32>,
|
||||
empty_in_channel_layout: bool,
|
||||
) -> BlissResult<Vec<f32>> {
|
||||
let mut resample_context = ffmpeg::software::resampling::context::Context::get(
|
||||
in_codec_format,
|
||||
in_channel_layout,
|
||||
in_rate,
|
||||
Sample::F32(Type::Packed),
|
||||
ffmpeg::util::channel_layout::ChannelLayout::MONO,
|
||||
SAMPLE_RATE,
|
||||
)
|
||||
.map_err(|e| {
|
||||
BlissError::DecodingError(format!(
|
||||
"while trying to allocate resampling context: {e:?}",
|
||||
))
|
||||
})?;
|
||||
|
||||
let mut resampled = ffmpeg::frame::Audio::empty();
|
||||
for decoded in rx.iter() {
|
||||
let mut something_happened = false;
|
||||
for mut decoded in rx.iter() {
|
||||
// If the decoded layout is empty, it means we forced the
|
||||
// "in_channel_layout" to something default, not that
|
||||
// the format is wrong.
|
||||
if empty_in_channel_layout && decoded.channel_layout() == ChannelLayout::empty() {
|
||||
decoded.set_channel_layout(in_channel_layout);
|
||||
} else if in_codec_format != decoded.format()
|
||||
|| (in_channel_layout != decoded.channel_layout())
|
||||
|| in_rate != decoded.rate()
|
||||
{
|
||||
warn!("received decoded packet with wrong format; file might be corrupted.");
|
||||
continue;
|
||||
}
|
||||
something_happened = true;
|
||||
resampled = ffmpeg::frame::Audio::empty();
|
||||
resample_context
|
||||
.run(&decoded, &mut resampled)
|
||||
.map_err(|e| {
|
||||
BlissError::DecodingError(format!("while trying to resample song: {:?}", e))
|
||||
BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
|
||||
})?;
|
||||
push_to_sample_array(&resampled, &mut sample_array);
|
||||
}
|
||||
if !something_happened {
|
||||
return Ok(sample_array);
|
||||
}
|
||||
// TODO when ffmpeg-next will be active again: shouldn't we allocate
|
||||
// `resampled` again?
|
||||
loop {
|
||||
match resample_context.flush(&mut resampled).map_err(|e| {
|
||||
BlissError::DecodingError(format!("while trying to resample song: {:?}", e))
|
||||
BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
|
||||
})? {
|
||||
Some(_) => {
|
||||
push_to_sample_array(&resampled, &mut sample_array);
|
||||
|
@ -600,18 +591,19 @@ fn push_to_sample_array(frame: &ffmpeg::frame::Audio, sample_array: &mut Vec<f32
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ripemd160::{Digest, Ripemd160};
|
||||
use adler32::RollingAdler32;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn test_analysis_too_small() {
|
||||
let error = Song::analyse(vec![0.]).unwrap_err();
|
||||
let error = Song::analyze(&[0.]).unwrap_err();
|
||||
assert_eq!(
|
||||
error,
|
||||
BlissError::AnalysisError(String::from("empty or too short song."))
|
||||
);
|
||||
|
||||
let error = Song::analyse(vec![]).unwrap_err();
|
||||
let error = Song::analyze(&[]).unwrap_err();
|
||||
assert_eq!(
|
||||
error,
|
||||
BlissError::AnalysisError(String::from("empty or too short song."))
|
||||
|
@ -619,8 +611,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyse() {
|
||||
let song = Song::new(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
fn test_analyze() {
|
||||
let song = Song::from_path(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let expected_analysis = vec![
|
||||
0.3846389,
|
||||
-0.849141,
|
||||
|
@ -643,59 +635,34 @@ mod tests {
|
|||
-0.9820945,
|
||||
-0.95968974,
|
||||
];
|
||||
for (x, y) in song.analysis.to_vec().iter().zip(expected_analysis) {
|
||||
for (x, y) in song.analysis.as_vec().iter().zip(expected_analysis) {
|
||||
assert!(0.01 > (x - y).abs());
|
||||
}
|
||||
assert_eq!(FEATURES_VERSION, song.features_version);
|
||||
}
|
||||
|
||||
fn _test_decode(path: &Path, expected_hash: &[u8]) {
|
||||
let song = Song::decode(path).unwrap();
|
||||
let mut hasher = Ripemd160::new();
|
||||
for sample in song.sample_array.iter() {
|
||||
hasher.update(sample.to_le_bytes().to_vec());
|
||||
fn _test_decode(path: &Path, expected_hash: u32) {
|
||||
let samples = Song::decode(path).unwrap();
|
||||
let mut hasher = RollingAdler32::new();
|
||||
for sample in samples.iter() {
|
||||
hasher.update_buffer(&sample.to_le_bytes());
|
||||
}
|
||||
|
||||
assert_eq!(expected_hash, hasher.finalize().as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tags() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
assert_eq!(song.artist, Some(String::from("David TMX")));
|
||||
assert_eq!(song.title, Some(String::from("Renaissance")));
|
||||
assert_eq!(song.album, Some(String::from("Renaissance")));
|
||||
assert_eq!(song.track_number, Some(String::from("02")));
|
||||
assert_eq!(song.genre, Some(String::from("Pop")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_tags() {
|
||||
let song = Song::decode(Path::new("data/no_tags.flac")).unwrap();
|
||||
assert_eq!(song.artist, None);
|
||||
assert_eq!(song.title, None);
|
||||
assert_eq!(song.album, None);
|
||||
assert_eq!(song.track_number, None);
|
||||
assert_eq!(song.genre, None);
|
||||
assert_eq!(expected_hash, hasher.hash());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resample_multi() {
|
||||
let path = Path::new("data/s32_stereo_44_1_kHz.flac");
|
||||
let expected_hash = [
|
||||
0xc5, 0xf8, 0x23, 0xce, 0x63, 0x2c, 0xf4, 0xa0, 0x72, 0x66, 0xbb, 0x49, 0xad, 0x84,
|
||||
0xb6, 0xea, 0x48, 0x48, 0x9c, 0x50,
|
||||
];
|
||||
_test_decode(&path, &expected_hash);
|
||||
let expected_hash = 0xbbcba1cf;
|
||||
_test_decode(&path, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resample_stereo() {
|
||||
let path = Path::new("data/s16_stereo_22_5kHz.flac");
|
||||
let expected_hash = [
|
||||
0x24, 0xed, 0x45, 0x58, 0x06, 0xbf, 0xfb, 0x05, 0x57, 0x5f, 0xdc, 0x4d, 0xb4, 0x9b,
|
||||
0xa5, 0x2b, 0x05, 0x56, 0x10, 0x4f,
|
||||
];
|
||||
_test_decode(&path, &expected_hash);
|
||||
let expected_hash = 0x1d7b2d6d;
|
||||
_test_decode(&path, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -703,12 +670,9 @@ mod tests {
|
|||
let path = Path::new("data/s16_mono_22_5kHz.flac");
|
||||
// Obtained through
|
||||
// ffmpeg -i data/s16_mono_22_5kHz.flac -ar 22050 -ac 1 -c:a pcm_f32le
|
||||
// -f hash -hash ripemd160 -
|
||||
let expected_hash = [
|
||||
0x9d, 0x95, 0xa5, 0xf2, 0xd2, 0x9c, 0x68, 0xe8, 0x8a, 0x70, 0xcd, 0xf3, 0x54, 0x2c,
|
||||
0x5b, 0x45, 0x98, 0xb4, 0xf3, 0xb4,
|
||||
];
|
||||
_test_decode(&path, &expected_hash);
|
||||
// -f hash -hash addler32 -
|
||||
let expected_hash = 0x5e01930b;
|
||||
_test_decode(&path, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -716,12 +680,9 @@ mod tests {
|
|||
let path = Path::new("data/s32_stereo_44_1_kHz.mp3");
|
||||
// Obtained through
|
||||
// ffmpeg -i data/s16_mono_22_5kHz.mp3 -ar 22050 -ac 1 -c:a pcm_f32le
|
||||
// -f hash -hash ripemd160 -
|
||||
let expected_hash = [
|
||||
0x28, 0x25, 0x6b, 0x7b, 0x6e, 0x37, 0x1c, 0xcf, 0xc7, 0x06, 0xdf, 0x62, 0x8c, 0x0e,
|
||||
0x91, 0xf7, 0xd6, 0x1f, 0xac, 0x5b,
|
||||
];
|
||||
_test_decode(&path, &expected_hash);
|
||||
// -f hash -hash addler32 -
|
||||
let expected_hash = 0x69ca6906;
|
||||
_test_decode(&path, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -733,54 +694,23 @@ mod tests {
|
|||
#[test]
|
||||
fn test_decode_right_capacity_vec() {
|
||||
let path = Path::new("data/s16_mono_22_5kHz.flac");
|
||||
let song = Song::decode(&path).unwrap();
|
||||
let sample_array = song.sample_array;
|
||||
let samples = Song::decode(&path).unwrap();
|
||||
assert_eq!(
|
||||
sample_array.len() + SAMPLE_RATE as usize,
|
||||
sample_array.capacity()
|
||||
samples.len() + SAMPLE_RATE as usize,
|
||||
samples.capacity()
|
||||
);
|
||||
|
||||
let path = Path::new("data/s32_stereo_44_1_kHz.flac");
|
||||
let song = Song::decode(&path).unwrap();
|
||||
let sample_array = song.sample_array;
|
||||
let samples = Song::decode(&path).unwrap();
|
||||
assert_eq!(
|
||||
sample_array.len() + SAMPLE_RATE as usize,
|
||||
sample_array.capacity()
|
||||
samples.len() + SAMPLE_RATE as usize,
|
||||
samples.capacity()
|
||||
);
|
||||
|
||||
let path = Path::new("data/capacity_fix.ogg");
|
||||
let song = Song::decode(&path).unwrap();
|
||||
let sample_array = song.sample_array;
|
||||
assert!(sample_array.len() as f32 / sample_array.capacity() as f32 > 0.90);
|
||||
assert!(sample_array.len() as f32 / (sample_array.capacity() as f32) < 1.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analysis_distance() {
|
||||
let mut a = Song::default();
|
||||
a.analysis = Analysis::new([
|
||||
0.16391512, 0.11326739, 0.96868552, 0.8353934, 0.49867523, 0.76532606, 0.63448005,
|
||||
0.82506196, 0.71457147, 0.62395476, 0.69680329, 0.9855766, 0.41369333, 0.13900452,
|
||||
0.68001012, 0.11029723, 0.97192943, 0.57727861, 0.07994821, 0.88993185,
|
||||
]);
|
||||
|
||||
let mut b = Song::default();
|
||||
b.analysis = Analysis::new([
|
||||
0.5075758, 0.36440256, 0.28888011, 0.43032829, 0.62387977, 0.61894916, 0.99676086,
|
||||
0.11913155, 0.00640396, 0.15943407, 0.33829514, 0.34947174, 0.82927523, 0.18987604,
|
||||
0.54437275, 0.22076826, 0.91232151, 0.29233168, 0.32846024, 0.04522147,
|
||||
]);
|
||||
assert_eq!(a.distance(&b), 1.9469079)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analysis_distance_indiscernible() {
|
||||
let mut a = Song::default();
|
||||
a.analysis = Analysis::new([
|
||||
1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
||||
20.,
|
||||
]);
|
||||
assert_eq!(a.distance(&a), 0.)
|
||||
let samples = Song::decode(&path).unwrap();
|
||||
assert!(samples.len() as f32 / samples.capacity() as f32 > 0.90);
|
||||
assert!(samples.len() as f32 / (samples.capacity() as f32) < 1.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -788,25 +718,33 @@ mod tests {
|
|||
assert_eq!(
|
||||
Song::decode(Path::new("nonexistent")).unwrap_err(),
|
||||
BlissError::DecodingError(String::from(
|
||||
"while opening format: ffmpeg::Error(2: No such file or directory)."
|
||||
"while opening format for file 'nonexistent': ffmpeg::Error(2: No such file or directory)."
|
||||
)),
|
||||
);
|
||||
assert_eq!(
|
||||
Song::decode(Path::new("data/picture.png")).unwrap_err(),
|
||||
BlissError::DecodingError(String::from("No audio stream found.")),
|
||||
BlissError::DecodingError(String::from(
|
||||
"No audio stream found for file 'data/picture.png'."
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_analysis() {
|
||||
let song = Song::new("data/s16_mono_22_5kHz.flac").unwrap();
|
||||
let song = Song::from_path("data/s16_mono_22_5kHz.flac").unwrap();
|
||||
assert_eq!(song.analysis[AnalysisIndex::Tempo], 0.3846389);
|
||||
assert_eq!(song.analysis[AnalysisIndex::Chroma10], -0.95968974);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_wav() {
|
||||
let expected_hash = 0xde831e82;
|
||||
_test_decode(Path::new("data/piano.wav"), expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_debug_analysis() {
|
||||
let song = Song::new("data/s16_mono_22_5kHz.flac").unwrap();
|
||||
let song = Song::from_path("data/s16_mono_22_5kHz.flac").unwrap();
|
||||
assert_eq!(
|
||||
"Analysis { Tempo: 0.3846389, Zcr: -0.849141, MeanSpectralCentroid: \
|
||||
-0.75481045, StdDeviationSpectralCentroid: -0.8790748, MeanSpectralR\
|
||||
|
@ -823,41 +761,4 @@ mod tests {
|
|||
format!("{:?}", song.analysis),
|
||||
);
|
||||
}
|
||||
|
||||
fn dummy_distance(_: &Array1<f32>, _: &Array1<f32>) -> f32 {
|
||||
0.
|
||||
}
|
||||
#[test]
|
||||
fn test_custom_distance() {
|
||||
let mut a = Song::default();
|
||||
a.analysis = Analysis::new([
|
||||
0.16391512, 0.11326739, 0.96868552, 0.8353934, 0.49867523, 0.76532606, 0.63448005,
|
||||
0.82506196, 0.71457147, 0.62395476, 0.69680329, 0.9855766, 0.41369333, 0.13900452,
|
||||
0.68001012, 0.11029723, 0.97192943, 0.57727861, 0.07994821, 0.88993185,
|
||||
]);
|
||||
|
||||
let mut b = Song::default();
|
||||
b.analysis = Analysis::new([
|
||||
0.5075758, 0.36440256, 0.28888011, 0.43032829, 0.62387977, 0.61894916, 0.99676086,
|
||||
0.11913155, 0.00640396, 0.15943407, 0.33829514, 0.34947174, 0.82927523, 0.18987604,
|
||||
0.54437275, 0.22076826, 0.91232151, 0.29233168, 0.32846024, 0.04522147,
|
||||
]);
|
||||
assert_eq!(a.custom_distance(&b, dummy_distance), 0.);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "bench", test))]
|
||||
mod bench {
|
||||
extern crate test;
|
||||
use crate::Song;
|
||||
use std::path::Path;
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn bench_resample_multi(b: &mut Bencher) {
|
||||
let path = Path::new("./data/s32_stereo_44_1_kHz.flac");
|
||||
b.iter(|| {
|
||||
Song::decode(&path).unwrap();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//! of a given Song.
|
||||
|
||||
use crate::utils::Normalize;
|
||||
use crate::{BlissError, BlissResult};
|
||||
use crate::bliss_lib::{BlissError, BlissResult};
|
||||
use bliss_audio_aubio_rs::{OnsetMode, Tempo};
|
||||
use log::warn;
|
||||
use ndarray::arr1;
|
||||
|
@ -48,10 +48,7 @@ impl BPMDesc {
|
|||
sample_rate,
|
||||
)
|
||||
.map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while loading aubio tempo object: {}",
|
||||
e.to_string()
|
||||
))
|
||||
BlissError::AnalysisError(format!("error while loading aubio tempo object: {e}"))
|
||||
})?,
|
||||
bpms: Vec::new(),
|
||||
})
|
||||
|
@ -59,10 +56,7 @@ impl BPMDesc {
|
|||
|
||||
pub fn do_(&mut self, chunk: &[f32]) -> BlissResult<()> {
|
||||
let result = self.aubio_obj.do_result(chunk).map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"aubio error while computing tempo {}",
|
||||
e.to_string()
|
||||
))
|
||||
BlissError::AnalysisError(format!("aubio error while computing tempo {e}"))
|
||||
})?;
|
||||
|
||||
if result > 0. {
|
||||
|
@ -100,14 +94,14 @@ impl Normalize for BPMDesc {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Song, SAMPLE_RATE};
|
||||
use crate::bliss_lib::{Song, SAMPLE_RATE};
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn test_tempo_real() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut tempo_desc = BPMDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(BPMDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(BPMDesc::HOP_SIZE) {
|
||||
tempo_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
assert!(0.01 > (0.378605 - tempo_desc.get_value()).abs());
|
||||
|
|
|
@ -9,7 +9,7 @@ use bliss_audio_aubio_rs::{bin_to_freq, PVoc, SpecDesc, SpecShape};
|
|||
use ndarray::{arr1, Axis};
|
||||
|
||||
use super::utils::{geometric_mean, mean, number_crossings, Normalize};
|
||||
use crate::{BlissError, BlissResult, SAMPLE_RATE};
|
||||
use crate::bliss_lib::{BlissError, BlissResult, SAMPLE_RATE};
|
||||
|
||||
/**
|
||||
* General object holding all the spectral descriptor.
|
||||
|
@ -125,23 +125,20 @@ impl SpectralDesc {
|
|||
centroid_aubio_desc: SpecDesc::new(SpecShape::Centroid, SpectralDesc::WINDOW_SIZE)
|
||||
.map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while loading aubio centroid object: {}",
|
||||
e.to_string()
|
||||
"error while loading aubio centroid object: {e}",
|
||||
))
|
||||
})?,
|
||||
rolloff_aubio_desc: SpecDesc::new(SpecShape::Rolloff, SpectralDesc::WINDOW_SIZE)
|
||||
.map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while loading aubio rolloff object: {}",
|
||||
e.to_string()
|
||||
"error while loading aubio rolloff object: {e}",
|
||||
))
|
||||
})?,
|
||||
phase_vocoder: PVoc::new(SpectralDesc::WINDOW_SIZE, SpectralDesc::HOP_SIZE).map_err(
|
||||
|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while loading aubio pvoc object: {}",
|
||||
e.to_string()
|
||||
))
|
||||
BlissError::AnalysisError(
|
||||
format!("error while loading aubio pvoc object: {e}",),
|
||||
)
|
||||
},
|
||||
)?,
|
||||
values_centroid: Vec::new(),
|
||||
|
@ -163,10 +160,7 @@ impl SpectralDesc {
|
|||
self.phase_vocoder
|
||||
.do_(chunk, fftgrain.as_mut_slice())
|
||||
.map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while processing aubio pv object: {}",
|
||||
e.to_string()
|
||||
))
|
||||
BlissError::AnalysisError(format!("error while processing aubio pv object: {e}"))
|
||||
})?;
|
||||
|
||||
let bin = self
|
||||
|
@ -174,8 +168,7 @@ impl SpectralDesc {
|
|||
.do_result(fftgrain.as_slice())
|
||||
.map_err(|e| {
|
||||
BlissError::AnalysisError(format!(
|
||||
"error while processing aubio centroid object: {}",
|
||||
e.to_string()
|
||||
"error while processing aubio centroid object: {e}",
|
||||
))
|
||||
})?;
|
||||
|
||||
|
@ -204,12 +197,12 @@ impl SpectralDesc {
|
|||
self.values_rolloff.push(freq);
|
||||
|
||||
let cvec: CVec = fftgrain.as_slice().into();
|
||||
let geo_mean = geometric_mean(&cvec.norm());
|
||||
let geo_mean = geometric_mean(cvec.norm());
|
||||
if geo_mean == 0.0 {
|
||||
self.values_flatness.push(0.0);
|
||||
return Ok(());
|
||||
}
|
||||
let flatness = geo_mean / mean(&cvec.norm());
|
||||
let flatness = geo_mean / mean(cvec.norm());
|
||||
self.values_flatness.push(flatness);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -265,7 +258,7 @@ impl Normalize for ZeroCrossingRateDesc {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Song;
|
||||
use crate::bliss_lib::Song;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
|
@ -290,7 +283,7 @@ mod tests {
|
|||
fn test_zcr() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut zcr_desc = ZeroCrossingRateDesc::default();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
zcr_desc.do_(&chunk);
|
||||
}
|
||||
assert!(0.001 > (-0.85036 - zcr_desc.get_value()).abs());
|
||||
|
@ -310,13 +303,14 @@ mod tests {
|
|||
assert!(0.0000001 > (expected - actual).abs());
|
||||
}
|
||||
|
||||
let song = Song::decode(Path::new("data/white_noise.flac")).unwrap();
|
||||
let song = Song::decode(Path::new("data/white_noise.mp3")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(22050).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
println!("{:?}", spectral_desc.get_flatness());
|
||||
// White noise - as close to 1 as possible
|
||||
let expected_values = vec![0.6706717, -0.9685736];
|
||||
let expected_values = vec![0.5785303, -0.9426308];
|
||||
for (expected, actual) in expected_values
|
||||
.iter()
|
||||
.zip(spectral_desc.get_flatness().iter())
|
||||
|
@ -329,7 +323,7 @@ mod tests {
|
|||
fn test_spectral_flatness() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
// Spectral flatness mean value computed here with phase vocoder before normalization: 0.111949615
|
||||
|
@ -359,7 +353,7 @@ mod tests {
|
|||
|
||||
let song = Song::decode(Path::new("data/tone_11080Hz.flac")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
let expected_values = vec![0.9967681, -0.99615175];
|
||||
|
@ -375,7 +369,7 @@ mod tests {
|
|||
fn test_spectral_roll_off() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
let expected_values = vec![-0.6326486, -0.7260933];
|
||||
|
@ -393,7 +387,7 @@ mod tests {
|
|||
fn test_spectral_centroid() {
|
||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
// Spectral centroid mean value computed here with phase vocoder before normalization: 1354.2273
|
||||
|
@ -422,7 +416,7 @@ mod tests {
|
|||
}
|
||||
let song = Song::decode(Path::new("data/tone_11080Hz.flac")).unwrap();
|
||||
let mut spectral_desc = SpectralDesc::new(SAMPLE_RATE).unwrap();
|
||||
for chunk in song.sample_array.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
for chunk in song.chunks_exact(SpectralDesc::HOP_SIZE) {
|
||||
spectral_desc.do_(&chunk).unwrap();
|
||||
}
|
||||
let expected_values = vec![0.97266, -0.9609926];
|
||||
|
|
18
src/utils.rs
18
src/utils.rs
|
@ -29,7 +29,7 @@ pub(crate) fn stft(signal: &[f32], window_length: usize, hop_length: usize) -> A
|
|||
(signal.len() as f32 / hop_length as f32).ceil() as usize,
|
||||
window_length / 2 + 1,
|
||||
));
|
||||
let signal = reflect_pad(&signal, window_length / 2);
|
||||
let signal = reflect_pad(signal, window_length / 2);
|
||||
|
||||
// Periodic, so window_size + 1
|
||||
let mut hann_window = Array::zeros(window_length + 1);
|
||||
|
@ -45,7 +45,7 @@ pub(crate) fn stft(signal: &[f32], window_length: usize, hop_length: usize) -> A
|
|||
.step_by(hop_length)
|
||||
.zip(stft.rows_mut())
|
||||
{
|
||||
let mut signal = (arr1(&window) * &hann_window).mapv(|x| Complex::new(x, 0.));
|
||||
let mut signal = (arr1(window) * &hann_window).mapv(|x| Complex::new(x, 0.));
|
||||
match signal.as_slice_mut() {
|
||||
Some(s) => fft.process(s),
|
||||
None => {
|
||||
|
@ -63,7 +63,7 @@ pub(crate) fn stft(signal: &[f32], window_length: usize, hop_length: usize) -> A
|
|||
}
|
||||
|
||||
pub(crate) fn mean<T: Clone + Into<f32>>(input: &[T]) -> f32 {
|
||||
input.iter().map(|x| x.clone().into() as f32).sum::<f32>() / input.len() as f32
|
||||
input.iter().map(|x| x.clone().into()).sum::<f32>() / input.len() as f32
|
||||
}
|
||||
|
||||
pub(crate) trait Normalize {
|
||||
|
@ -101,8 +101,7 @@ pub(crate) fn geometric_mean(input: &[f32]) -> f32 {
|
|||
let mut exponents: i32 = 0;
|
||||
let mut mantissas: f64 = 1.;
|
||||
for ch in input.chunks_exact(8) {
|
||||
let mut m;
|
||||
m = (ch[0] as f64 * ch[1] as f64) * (ch[2] as f64 * ch[3] as f64);
|
||||
let mut m = (ch[0] as f64 * ch[1] as f64) * (ch[2] as f64 * ch[3] as f64);
|
||||
m *= 3.273390607896142e150; // 2^500 : avoid underflows and denormals
|
||||
m *= (ch[4] as f64 * ch[5] as f64) * (ch[6] as f64 * ch[7] as f64);
|
||||
if m == 0. {
|
||||
|
@ -113,8 +112,7 @@ pub(crate) fn geometric_mean(input: &[f32]) -> f32 {
|
|||
}
|
||||
|
||||
let n = input.len() as u32;
|
||||
((((mantissas as f32).log2() + exponents as f32) as f32) / n as f32 - (1023. + 500.) / 8.)
|
||||
.exp2()
|
||||
(((mantissas as f32).log2() + exponents as f32) / n as f32 - (1023. + 500.) / 8.).exp2()
|
||||
}
|
||||
|
||||
pub(crate) fn hz_to_octs_inplace(
|
||||
|
@ -122,7 +120,7 @@ pub(crate) fn hz_to_octs_inplace(
|
|||
tuning: f64,
|
||||
bins_per_octave: u32,
|
||||
) -> &mut Array1<f64> {
|
||||
let a440 = 440.0 * (2_f64.powf(tuning / f64::from(bins_per_octave)) as f64);
|
||||
let a440 = 440.0 * 2_f64.powf(tuning / f64::from(bins_per_octave));
|
||||
|
||||
*frequencies /= a440 / 16.;
|
||||
frequencies.mapv_inplace(f64::log2);
|
||||
|
@ -167,7 +165,7 @@ pub(crate) fn convolve(input: &Array1<f64>, kernel: &Array1<f64>) -> Array1<f64>
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Song;
|
||||
use crate::bliss_lib::Song;
|
||||
use ndarray::Array2;
|
||||
use ndarray::{arr1, Array};
|
||||
use ndarray_npy::ReadNpyExt;
|
||||
|
@ -500,7 +498,7 @@ mod tests {
|
|||
|
||||
let song = Song::decode(Path::new("data/piano.flac")).unwrap();
|
||||
|
||||
let stft = stft(&song.sample_array, 2048, 512);
|
||||
let stft = stft(&song, 2048, 512);
|
||||
|
||||
assert!(!stft.is_empty() && !expected_stft.is_empty());
|
||||
for (expected, actual) in expected_stft.iter().zip(stft.iter()) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user