Commit 2ef1c0f4 authored by Matteo's avatar Matteo
Browse files

update

parent 94b229e9
......@@ -667,6 +667,31 @@ files = [
{file = "llvmlite-0.39.1.tar.gz", hash = "sha256:b43abd7c82e805261c425d50335be9a6c4f84264e34d6d6e475207300005d572"},
]
[[package]]
name = "markdown-it-py"
version = "2.2.0"
description = "Python port of markdown-it. Markdown parsing, done right!"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "markdown-it-py-2.2.0.tar.gz", hash = "sha256:7c9a5e412688bc771c67432cbfebcdd686c93ce6484913dccf06cb5a0bea35a1"},
{file = "markdown_it_py-2.2.0-py3-none-any.whl", hash = "sha256:5a35f8d1870171d9acc47b99612dc146129b631baf04970128b568f190d0cc30"},
]
[package.dependencies]
mdurl = ">=0.1,<1.0"
[package.extras]
benchmarking = ["psutil", "pytest", "pytest-benchmark"]
code-style = ["pre-commit (>=3.0,<4.0)"]
compare = ["commonmark (>=0.9,<1.0)", "markdown (>=3.4,<4.0)", "mistletoe (>=1.0,<2.0)", "mistune (>=2.0,<3.0)", "panflute (>=2.3,<3.0)"]
linkify = ["linkify-it-py (>=1,<3)"]
plugins = ["mdit-py-plugins"]
profiling = ["gprof2dot"]
rtd = ["attrs", "myst-parser", "pyyaml", "sphinx", "sphinx-copybutton", "sphinx-design", "sphinx_book_theme"]
testing = ["coverage", "pytest", "pytest-cov", "pytest-regressions"]
[[package]]
name = "markupsafe"
version = "2.1.2"
......@@ -727,16 +752,28 @@ files = [
{file = "MarkupSafe-2.1.2.tar.gz", hash = "sha256:abcabc8c2b26036d62d4c746381a6f7cf60aafcc653198ad678306986b09450d"},
]
[[package]]
name = "mdurl"
version = "0.1.2"
description = "Markdown URL utilities"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"},
{file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"},
]
[[package]]
name = "mpai-cae-arp"
version = "0.2.1"
version = "0.2.3"
description = "The MPAI CAE-ARP software API"
category = "main"
optional = false
python-versions = ">=3.10,<4.0"
files = [
{file = "mpai_cae_arp-0.2.1-py3-none-any.whl", hash = "sha256:20bea510346e620d202acf340d055d5906b332738fbf4eed8ce637b52c0b3b85"},
{file = "mpai_cae_arp-0.2.1.tar.gz", hash = "sha256:9a36a913ca91bb5ecedd44d3edb9230e1af46b8eaed4650ba957b6144bd3eba9"},
{file = "mpai_cae_arp-0.2.3-py3-none-any.whl", hash = "sha256:d2d1225310474582e5697499fffa1140cf2d28fc2cb4fdad402446d146aa6e9e"},
{file = "mpai_cae_arp-0.2.3.tar.gz", hash = "sha256:d5cc6df1708678dfe2dddf76a124a6d13eebd4da2360d4d88963c4be43221236"},
]
[package.dependencies]
......@@ -1078,7 +1115,7 @@ email = ["email-validator (>=1.0.3)"]
name = "pygments"
version = "2.14.0"
description = "Pygments is a syntax highlighting package written in Python."
category = "dev"
category = "main"
optional = false
python-versions = ">=3.6"
files = [
......@@ -1253,6 +1290,25 @@ idna = {version = "*", optional = true, markers = "extra == \"idna2008\""}
[package.extras]
idna2008 = ["idna"]
[[package]]
name = "rich"
version = "13.3.3"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
category = "main"
optional = false
python-versions = ">=3.7.0"
files = [
{file = "rich-13.3.3-py3-none-any.whl", hash = "sha256:540c7d6d26a1178e8e8b37e9ba44573a3cd1464ff6348b99ee7061b95d1c6333"},
{file = "rich-13.3.3.tar.gz", hash = "sha256:dc84400a9d842b3a9c5ff74addd8eb798d155f36c1c91303888e0a66850d2a15"},
]
[package.dependencies]
markdown-it-py = ">=2.2.0,<3.0.0"
pygments = ">=2.13.0,<3.0.0"
[package.extras]
jupyter = ["ipywidgets (>=7.5.1,<9)"]
[[package]]
name = "ruff"
version = "0.0.261"
......@@ -1936,4 +1992,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "0c3763c54b9876a9c5ca29c18660439e1f2b09f42cd2cb009114d52a89d94e9f"
content-hash = "dfd9dd13f5970323abae96a9b68c21a666a0befb4d9111bf1c12bdfb832ee332"
......@@ -8,8 +8,9 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
mpai-cae-arp = "^0.2.1"
mpai-cae-arp = "^0.2.3"
numpy = "1.23.3"
rich = "^13.3.3"
[tool.poetry.group.docs.dependencies]
......
import argparse
import os
import time
from rich.console import Console
import segment_finder as sf
from mpai_cae_arp.types.irregularity import IrregularityFile
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--working-directory", "-w", help="Working directory", required=True)
parser.add_argument("--files-name", "-f", nargs="+", help="Files name", required=True)
return parser.parse_args()
def exit_with_error(error_message: str, console) -> None:
console.print(f"[red bold]Error: {error_message}")
quit(os.EX_USAGE)
def main():
args = get_args()
console = Console()
console.print("[bold]Welcome to ARP Audio Analyzer!")
working_directory = args.working_directory
files_name = args.files_name
with console.status("Reading input files...", spinner="dots"):
time.sleep(1)
audio_src = os.path.join(working_directory, "PreservationAudioFile", f"{files_name[0]}.wav")
video_src = os.path.join(working_directory, "PreservationAudioVisualFile", f"{files_name[0]}.mov")
console.log(audio_src)
console.log(video_src)
audio_exists = os.path.exists(audio_src)
video_exists = os.path.exists(video_src)
match audio_exists, video_exists:
case True, True:
console.print("[green]Input files found!")
case False, True:
exit_with_error("Audio file not found!", console)
case True, False:
exit_with_error("Video file not found!", console)
case False, False:
exit_with_error("Input files not found!", console)
# create irregularity file 1
with console.status("Creating irregularity file 1...", spinner="dots"):
irreg1 = sf.create_irreg_file(audio_src, video_src)
# create irregularity file 2
with console.status("Creating irregularity file 2...", spinner="dots"):
video_irreg_1 = {}
irreg2 = sf.merge_irreg_files(irreg1, IrregularityFile.from_json(video_irreg_1))
with console.status("Extracting audio irregularities...", spinner="bouncingBall"):
sf.extract_audio_irregularities(audio_src, irreg2, working_directory)
# classify audio irregularities
console.print("[green bold]Success!")
if __name__ == "__main__":
print(sf.create_irreg_file("test.wav", "test.mp4"))
\ No newline at end of file
main()
import os
import tempfile
from uuid import uuid4
import numpy as np
from mpai_cae_arp.audio import AudioWave, Noise
from mpai_cae_arp.files import File, FileType
from mpai_cae_arp.types.irregularity import Irregularity, IrregularityFile, Source
from mpai_cae_arp.time import frames_to_seconds
from mpai_cae_arp.time import frames_to_seconds, seconds_to_frames
temp_dir = tempfile.gettempdir()
TMP_CHANNELS_MAP = os.path.join(temp_dir, "channels_map.json")
def calculate_offset(audio: AudioWave, video: AudioWave) -> float:
"""
......@@ -29,30 +34,67 @@ def calculate_offset(audio: AudioWave, video: AudioWave) -> float:
return lags[lag_idx] / audio.samplerate
def foo(audio_src) -> list[Irregularity]:
input = AudioWave.from_file(audio_src)
def get_irregularities_from_audio(audio_src: AudioWave) -> list[Irregularity]:
input_channels: list[AudioWave] = []
for channel in input.channels:
input_channels.append(input.get_channel(channel))
for channel in audio_src.channels:
input_channels.append(audio_src.get_channel(channel))
channels_map = {}
irreg_list: list[Irregularity] = []
for audio in input_channels:
for idx, audio in enumerate(input_channels):
for _, noise_list in audio.get_silence_slices([
Noise("A", -50, -63),
Noise("B", -63, -69),
Noise("C", -69, -72)],
length=500).items():
for start, _ in noise_list:
id = uuid4()
irreg_list.append(
Irregularity(
uuid=uuid4(),
uuid=id,
source=Source.AUDIO,
time_label=frames_to_seconds(start, audio.samplerate)
)
)
channels_map[id] = idx
File(TMP_CHANNELS_MAP, FileType.JSON).write_content(channels_map)
return irreg_list
def create_irreg_file(audio_src, video_src) -> IrregularityFile:
offset = calculate_offset(AudioWave.from_file(audio_src), video_src)
return IrregularityFile(foo(audio_src), offset=offset)
def create_irreg_file(audio_src: str, video_src: str) -> IrregularityFile:
audio = AudioWave.from_file(audio_src, bufferize=True)
offset = calculate_offset(audio, video_src)
return IrregularityFile(get_irregularities_from_audio(audio), offset=offset)
def merge_irreg_files(
file1: IrregularityFile,
file2: IrregularityFile) -> IrregularityFile:
new_file = IrregularityFile(
irregularities=file1.irregularities + file2.irregularities,
offset=np.argmax([file1.offset, file2.offset]))
new_file.irregularities.sort(key=lambda x: x.time_label)
return new_file
def extract_audio_irregularities(
audio: AudioWave,
irreg_file: IrregularityFile,
path: str) -> None:
channels_map = File(TMP_CHANNELS_MAP, FileType.JSON).get_content()
for irreg in irreg_file.irregularities:
if irreg.source == Source.AUDIO:
chunk = audio.get_channel(channels_map[irreg.irregularity_ID])[
seconds_to_frames(
irreg.time_label, audio.samplerate
):seconds_to_frames(
irreg.time_label, audio.samplerate)+audio.samplerate//2]
chunk.save(f"{path}/AudioBlocks/{irreg.irregularity_ID}.wav")
os.remove(TMP_CHANNELS_MAP)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment