-
Notifications
You must be signed in to change notification settings - Fork 0
/
snd-splitter.scm
142 lines (107 loc) · 3.91 KB
/
snd-splitter.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;;; Splitting recording to parts separated by silences
;; Copyright (C) 2003, 2005 Brailcom, o.p.s.
;; Author: Milan Zamazal <pdm@brailcom.org>
;; COPYRIGHT NOTICE
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
(define-module (fvoxedit snd-splitter)
:use-module (srfi srfi-1)
:use-module (ice-9 format)
:use-module (guile-user) ; snd functions
:export (*noise-level* *chunk-length* *min-silence-length*
*file-name-format*))
;;; Configuration variables and functions
(define *noise-level* 0.02) ; 0.0 .. 1.0
(define *chunk-length* 0.005) ; seconds
(define *min-silence-length* 0.5) ; seconds
(define *file-name-format* "~4,'0D")
(define (handle-silence beg end)
(add-mark (inexact->exact (/ (+ beg end) 2))))
;;; Internal functions
(define (seconds->samples* secs)
(* 2 (seconds->samples secs)))
(define (samples->seconds* secs)
(/ (samples->seconds secs) 2))
(define (next-chunk-value reader)
(let ((nsamples (seconds->samples* *chunk-length*))
(i 0)
(sum 0))
(while (and (< i nsamples) (not (sample-reader-at-end? reader)))
(set! sum (+ sum (abs (next-sample reader))))
(set! i (+ i 1)))
(if (sample-reader-at-end? reader)
#f
(/ sum i))))
(define (next-chunk-state reader)
(let ((chunk-value (next-chunk-value reader)))
(cond
((or (not chunk-value) (c-g?))
#f)
((> chunk-value *noise-level*)
'sound)
(else
'silence))))
(define (skip-silence reader)
(let ((position (sample-reader-position reader)))
(case (next-chunk-state reader)
((silence)
(skip-silence reader))
((sound)
position)
(else
#f))))
(define (next-silence reader)
(let ((beg (sample-reader-position reader))
(end (skip-silence reader)))
(cond
((not end)
#f)
((>= (- (samples->seconds* end) (samples->seconds* beg))
*min-silence-length*)
(list beg end))
(else
(next-silence reader)))))
(define (find-process-silences)
(let ((reader (make-sample-reader 0)))
(letrec ((process (lambda ()
(let ((silence (next-silence reader)))
(if silence
(begin
(apply handle-silence silence)
(process)))))))
(skip-silence reader)
(process))))
;;; User interface
(define (com-find-silences)
(delete-marks)
(find-process-silences)
#f)
(define (com-save-parts)
(let ((i 1)
(marks* (map mark-sample (caar (marks)))))
(for-each
(lambda (beg end)
(let ((region (make-region beg end (selected-sound) #t)))
(save-region region :file (format #f *file-name-format* i)
:data-format (data-format) :header-type (header-type))
(forget-region region)
(set! i (+ i 1))))
(cons 0 marks*) (append marks* (list (frames)))))
(set! (selection-member?) #f)
#f)
(define (com-adjust-parameters)
(report-in-minibuffer "Not yet implemented. Set guile variables instead.")
#f)
(if (not (provided? 'splitter))
(let ((m (add-to-main-menu "Splitter")))
(add-to-menu m "Identify silences" com-find-silences)
(add-to-menu m "Save parts" com-save-parts)))