-
Notifications
You must be signed in to change notification settings - Fork 0
/
DifferanceDatabaseQueryImpl.scala
135 lines (112 loc) · 5 KB
/
DifferanceDatabaseQueryImpl.scala
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
/*
* DifferanceDatabaseQueryImpl.scala
* (WritingMachine)
*
* Copyright (c) 2011 Hanns Holger Rutz. All rights reserved.
*
* This software 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, june 1991 of the License, or (at your option) any later version.
*
* This software 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 (gpl.txt) along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* For further information, please contact Hanns Holger Rutz at
* contact@sciss.de
*/
package de.sciss.grapheme
package impl
import de.sciss.strugatzki.{FeatureCorrelation, Span => SSpan}
import java.io.File
import de.sciss.synth
object DifferanceDatabaseQueryImpl {
var verbose = false
def apply( db: Database ) : DifferanceDatabaseQuery = new DifferanceDatabaseQueryImpl( db )
}
class DifferanceDatabaseQueryImpl private ( db: Database ) extends AbstractDifferanceDatabaseQuery {
import GraphemeUtil._
import DifferanceDatabaseQuery._
private val identifier = "database-query-impl"
val matchDurationMotion = Motion.coin(
1.0/33,
Motion.linexp( Motion.walk( 0, 1, 0.1 ), 0, 1, 0.4, 4.0 ), // Motion.exprand( 0.4, 4.0 )
Motion.exprand( 4.0, 16.0 )
)
// val matchDeviationMotion = Motion.linrand( 0.2, 0.5 )
val spectralMotion = Motion.linrand( 0.25, 0.75 )
val stretchDeviationMotion = Motion.walk( 0.111, 0.333, 0.05 ) // Motion.linrand( 0.2, 0.5 )
val rankMotion = Motion.linrand( 0, 11 )
val maxBoostMotion = Motion.constant( 30 ) // 18
val minSpacingMotion = Motion.constant( 0.0 ) // 0.5
val minPhraseDur = 10.0
def findMatch( rank: Int, phrase: Phrase, punchIn: Span, punchOut: Span,
minPunch: Long, maxPunch: Long, weight: Double )( implicit tx: Tx ) : FutureResult[ Match ] = {
import synth._
val maxBoost = maxBoostMotion.step.dbamp.toFloat
val minSpc = secondsToFrames( minSpacingMotion.step )
val dirFut = db.asStrugatziDatabase
val metaFut = phrase.asStrugatzkiInput
dirFut.flatMap { dir =>
metaFut.flatMap { metaInput =>
findMatchIn( dir, metaInput, maxBoost, minSpc, rank, phrase, punchIn, punchOut,
minPunch, maxPunch, weight )
}
}
}
private def findMatchIn( dir: File, metaInput: File, maxBoost: Float, minSpacing: Long, rank: Int, phrase: Phrase,
punchIn: Span, punchOut: Span,
minPunch: Long, maxPunch: Long, weight: Double ) : FutureResult[ Match ] = {
import FeatureCorrelation.{Match => _, _} // otherwise Match shadows DifferanceDatabaseQuery.Match
val res = FutureResult.event[ Match ]()
val set = SettingsBuilder()
set.databaseFolder = dir
set.normalize = true
set.maxBoost = maxBoost
set.metaInput = metaInput
set.minSpacing = minSpacing
set.numMatches = max( 2, rank + 1 )
set.numPerFile = max( 2, rank + 1 )
set.punchIn = Punch( SSpan( punchIn.start, punchIn.stop ), weight.toFloat )
set.punchOut = Some( Punch( SSpan( punchOut.start, punchOut.stop ), weight.toFloat ))
set.minPunch = minPunch
set.maxPunch = maxPunch
val setb = set.build
if( verbose ) println( "----CORRELATION----" )
if( verbose ) println( setb )
val process = apply( setb ) {
case Aborted =>
println( "DifferanceDatabaseQuery : Ouch. Aborted. Need to handle this case!" )
res.set( failureMatch )
case Failure( e ) =>
println( "DifferanceDatabaseQuery : Ouch. Failure. Need to handle this case!" )
e.printStackTrace()
res.set( failureMatch )
case Success( coll ) =>
val idx0 = min( coll.size - 1, rank )
val idx = if( idx0 == 0 && coll( idx0 ).sim.isNaN ) idx0 + 1 else idx0
res.set( if( idx < 0 || idx >= coll.size ) {
println( "DifferanceDatabaseQuery : Ouch. No matches. Need to handle this case!" )
failureMatch
} else {
val m = coll( idx )
Match( db, Span( m.punch.start, m.punch.stop ), m.boostIn, m.boostOut )
})
case Progress( p ) =>
}
process.start()
res
}
private def failureMatch = {
atomic( identifier + " : failure match" ) { tx1 =>
Match( db, Span( 0L, min( db.length( tx1 ), secondsToFrames( 1.0 ))), 1f, 1f )
}
}
}