fordon_greeman / voidfiles

Branch: 2079ec5
Switch branches/tags
main
theidioticdev add emacs 2079ec5 21h ago
114 lines · 4.1 KB Raw Blame History
1 ;;; emms-info-native.el --- Native Emacs Lisp info method for EMMS -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2020-2023 Free Software Foundation, Inc.
4
5 ;; Author: Petteri Hintsanen <petterih@iki.fi>
6
7 ;; This file is part of EMMS.
8
9 ;; EMMS is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; EMMS is distributed in the hope that it will be useful, but WITHOUT
15 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 ;; License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with EMMS; see the file COPYING. If not, write to the Free
21 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 ;; MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; This file provides a native emms-info-method for EMMS. Here
27 ;; "native" means a pure Emacs Lisp implementation instead of one
28 ;; relying on external tools or libraries like
29 ;; `emms-info-native-ogginfo' or `emms-info-libtag'.
30 ;;
31 ;; To use this method, add `emms-info-native' to
32 ;; `emms-info-functions'.
33 ;;
34 ;; The following file formats are supported:
35 ;;
36 ;; - Vorbis: Ogg Vorbis I Profile, filename extension `.ogg',
37 ;; elementary streams only.
38 ;;
39 ;; - Opus: Ogg Opus profile, filename extension `.opus', elementary
40 ;; streams only.
41 ;;
42 ;; - FLAC streams in native encapsulation format, filename extension
43 ;; `.flac'.
44 ;;
45 ;; - MP3 files with extension `.mp3' and ID3v2 tags. All ID3v2
46 ;; versions should work, but many features like compression and
47 ;; encryption are not supported.
48 ;;
49 ;; - SPC files with extension `.spc' and ID666 tags. This is an audio
50 ;; file based on a memory dump from an SPC700, a special audio chip
51 ;; found within Super Nintendos.
52 ;;
53 ;; Format detection is based solely on filename extension, which is
54 ;; matched case-insensitively.
55
56 ;;; Code:
57
58 (require 'emms-info)
59 (require 'emms-info-native-flac)
60 (require 'emms-info-native-ogg)
61 (require 'emms-info-native-mp3)
62 (require 'emms-info-native-spc)
63
64 (defun emms-info-native (track)
65 "Set info fields for TRACK.
66 Supports Ogg Vorbis/Opus, FLAC, MP3 and SPC files."
67 (condition-case env
68 (let* ((filename
69 (emms-track-name track))
70 (info-fields
71 (emms-info-native--decode-info-fields filename)))
72 (dolist (field info-fields)
73 (let ((name (intern (concat "info-" (car field))))
74 (value (cdr field)))
75 (when (stringp value)
76 (setq value (string-trim-right value)))
77 (emms-track-set track name value))))
78 (error (message "emms-info-native error processing %s: %s"
79 (emms-track-name track) env))))
80
81 (defun emms-info-native--decode-info-fields (filename)
82 "Decode info fields from FILENAME.
83 Return a list of (FIELD . VALUE) cons cells, where FIELD is an
84 info field and VALUE is the corresponding info value. Both are
85 strings."
86 (let ((stream-type (emms-info-native--find-stream-type filename)))
87 (cond ((or (eq stream-type 'vorbis) (eq stream-type 'opus))
88 (emms-info-native-ogg-decode-metadata filename stream-type))
89 ((eq stream-type 'flac)
90 (emms-info-native-flac-decode-metadata filename))
91 ((eq stream-type 'mp3)
92 (emms-info-native-mp3-decode-metadata filename))
93 ((eq stream-type 'spc)
94 (emms-info-native-spc-decode-id666 filename))
95 (t nil))))
96
97 (defun emms-info-native--find-stream-type (filename)
98 "Deduce the stream type from FILENAME.
99 This is a naive implementation that relies solely on filename
100 extension.
101
102 Return one of `vorbis', `opus', `flac', `mp3' or `spc', or nil if
103 the stream type cannot be deduced."
104 (let ((case-fold-search t))
105 (cond ((string-match ".ogg$" filename) 'vorbis)
106 ((string-match ".opus$" filename) 'opus)
107 ((string-match ".flac$" filename) 'flac)
108 ((string-match ".mp3$" filename) 'mp3)
109 ((string-match ".spc$" filename) 'spc)
110 (t nil))))
111
112 (provide 'emms-info-native)
113
114 ;;; emms-info-native.el ends here