Skip to content

Commit 132083f

Browse files
authoredApr 5, 2017
Merge pull request #2 from JonLatane/gh-pages
Improve JS support
2 parents f337dd1 + 221abdc commit 132083f

File tree

11 files changed

+1232
-50
lines changed

11 files changed

+1232
-50
lines changed
 

‎dist/web/libharmony-0.2.js

+1,097-36
Large diffs are not rendered by default.

‎dist/web/libharmony-0.2.meta.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎index.html

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>libharmony</title>
6+
<!-- Bootstrap -->
7+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
9+
</head>
10+
<body>
11+
<div class="jumbotron">
12+
<div class="container">
13+
<h1>libharmony</h1>
14+
<h3>musical computation made easy</h3>
15+
</div>
16+
</div>
17+
18+
<div class="container">
19+
<p>
20+
Harmony is a DSL for music. It's written in Kotlin and supports JVM and JS compilation targets.
21+
Harmony is licensed under the LGPL, so use it in your proprietary project and acknowledge us! Also
22+
feel free to <a href="https://github.com/jonlatane/libharmony">fork or contribute</a>.
23+
</p>
24+
<p>
25+
Here's an example of what the Harmony library is capable of (you'll need a modern browser here):
26+
</p>
27+
<div class="row">
28+
<div class="col-sm-6">
29+
<h4>Input</h4>
30+
<p>
31+
<input type="text" value="C"/>
32+
</p>
33+
<p>
34+
<input type="text" value="E"/>
35+
</p>
36+
<p>
37+
<input type="text" value="G"/>
38+
</p>
39+
<p>
40+
<input type="text" value=""/>
41+
</p>
42+
<p>
43+
<input type="text" value=""/>
44+
</p>
45+
<p>
46+
<input type="text" value=""/>
47+
</p>
48+
<p>
49+
<input type="text" value=""/>
50+
</p>
51+
</div>
52+
<div class="col-sm-6">
53+
<h4>Output</h4>
54+
<div id="example-results"></div>
55+
</div>
56+
</div>
57+
</div>
58+
59+
<script type="text/javascript" src="dist/web/kotlin.js"></script>
60+
<script type="text/javascript" src="dist/web/libharmony-0.2.js"></script>
61+
<script type="text/javascript">
62+
var libharmony = window["libharmony-0.2"].com.jonlatane.libharmony;
63+
var resultDiv = document.getElementById('example-results');
64+
var blah;
65+
function getChord() {
66+
let notes = Array.from(document.querySelectorAll('input')).
67+
filter((input) => input.value).
68+
map((input) => input.value);
69+
let result = null;
70+
try {
71+
result = libharmony.getChordsInCMajor(notes);
72+
} catch(e) {
73+
resultDiv.innerHTML = '<div class="alert alert-danger">Invalid input</div>';
74+
}
75+
if(result) {
76+
blah = result;
77+
let scores = Object.keys(result.bucketsByHash).sort((a,b) => b-a);
78+
let resultHTML = '';
79+
scores.forEach((score) => {
80+
let size = 1 + 3*(score/50);
81+
result.bucketsByHash[score].entries[0][1].array.forEach((chord) => {
82+
resultHTML += `<p style="font-size: ${size}em">${chord}</p>`;
83+
});
84+
});
85+
resultDiv.innerHTML = resultHTML;
86+
}
87+
}
88+
Array.from(document.querySelectorAll('input')).forEach((input) => input.onkeyup = getChord);
89+
getChord();
90+
</script>
91+
</body>
92+
</html>

‎javascript/build.gradle

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
buildscript {
22
ext.kotlin_version = '1.0.7'
3+
ext.kotlin_js_version = '1.0.7'
34

45
repositories {
56
mavenCentral()
@@ -11,8 +12,14 @@ buildscript {
1112

1213
apply plugin: 'kotlin2js'
1314

15+
repositories {
16+
maven {
17+
url 'https://dl.bintray.com/kotlin/kotlin-dev/'
18+
}
19+
}
20+
1421
dependencies {
15-
compile "org.jetbrains.kotlin:kotlin-js-library:$kotlin_version"
22+
compile "org.jetbrains.kotlin:kotlin-js-library:$kotlin_js_version"
1623
}
1724

1825
compileKotlin2Js {
@@ -32,7 +39,7 @@ compileKotlin2Js {
3239
into "${rootProject.projectDir}/dist/web"
3340
include { fileTreeElement ->
3441
def path = fileTreeElement.path
35-
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
42+
path.endsWith('.js') && (path.startsWith('META-INF/resources/') || !path.startsWith('META-INF/'))
3643
}
3744
}
3845
}

‎javascript/src/main/kotlin/com/jonlatane/libharmony/KotlinJSCompat.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package com.jonlatane.libharmony
55

66
fun String.toInt() = parseInt(this)
77

8-
@native fun Char.isDigit(): Boolean {
8+
fun Char.isDigit(): Boolean {
99
try {
1010
parseInt(this.toString())
1111
return true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jonlatane.libharmony
2+
3+
/**
4+
* Things that are handy to have in JS for testing purposes.
5+
*
6+
* Created by jonlatane on 4/3/17.
7+
*/
8+
@JsName("getChordsInCMajor")
9+
fun getChordsInCMajor(vararg notes: String): Map<Int, List<String>> {
10+
val pitches = notes.toList().map {Pitch(it)}.toTypedArray()
11+
val chord = Chord(*pitches)
12+
val data = Keys.CMajor.getRootLikelihoodsAndNames(chord)
13+
return data
14+
}

‎jvm/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ jar {
2525
sourceSets {
2626
main.kotlin.srcDirs += 'src/main/kotlin'
2727
main.kotlin.srcDirs += '../shared/src/main/kotlin'
28+
test.kotlin.srcDirs += 'src/main/kotlin'
29+
test.kotlin.srcDirs += 'src/test/kotlin'
30+
test.kotlin.srcDirs += '../shared/src/test/kotlin'
2831
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.jonlatane.libharmony
22

3+
/**
4+
* Kotlin's JVM standard library does not include a JSName annotation.
5+
*/
36
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION,
47
AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
5-
annotation class native
8+
annotation class JsName(val name: String)

‎shared/src/main/kotlin/com/jonlatane/libharmony/Chord.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import com.jonlatane.libharmony.Modulus.Companion.TWELVETONE
2323
* *
2424
* @getCharacteristic and @guessCharacteristic, for guessing the characteristic of a chord
2525
*/
26-
@native open class Chord(vararg elements: Pitch) : PitchSet(Modulus.TWELVETONE, *elements) {
26+
open class Chord(vararg elements: Pitch) : PitchSet(Modulus.TWELVETONE, *elements) {
2727
var root: Pitch = elements.elementAtOrElse(0, {Pitch(0)})
2828
override fun toString(): String {
2929
return super.toString() + " / $root"
@@ -123,7 +123,7 @@ import com.jonlatane.libharmony.Modulus.Companion.TWELVETONE
123123
* *
124124
* @return
125125
*/
126-
@native fun guessCharacteristic(c: Chord, root: Int): Pair<String, Int> {
126+
fun guessCharacteristic(c: Chord, root: Int): Pair<String, Int> {
127127
var name = ""
128128
var certainty = 0
129129

‎shared/src/main/kotlin/com/jonlatane/libharmony/Key.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.jonlatane.libharmony.Modulus.Companion.TWELVETONE
1212
1313
* @author Jon
1414
*/
15-
@native class Key(root: Pitch) : Scale(root) {
15+
class Key(root: Pitch) : Scale(root) {
1616
constructor(scale: Scale) : this(scale.root) {
1717
addAll(scale)
1818
}
@@ -41,7 +41,7 @@ import com.jonlatane.libharmony.Modulus.Companion.TWELVETONE
4141
* *
4242
* @return
4343
*/
44-
@native fun getNoteName(tone: Int): String {
44+
fun getNoteName(tone: Int): String {
4545
val i: Int = tone % modulus!!
4646
var result = ""
4747

@@ -101,15 +101,15 @@ import com.jonlatane.libharmony.Modulus.Companion.TWELVETONE
101101
return result
102102
}
103103

104-
@native fun getRootLikelihoodsAndNames(c: Chord): Map<Int, List<String>> {
104+
fun getRootLikelihoodsAndNames(c: Chord): Map<Int, List<String>> {
105105
val result = mutableMapOf<Int, MutableList<String>>()
106106
for (n in 0..11) {
107107
val data = Chord.guessCharacteristic(c, n)
108108
val name = data.first
109-
var score = data.second
110-
if (n == c.root.tone) {
111-
score += 1000
112-
}
109+
val score = data.second
110+
//if (n == c.root.tone) {
111+
// score += 1000
112+
//}
113113
var bucket: MutableList<String>? = result[score]
114114
if (bucket == null) {
115115
bucket = mutableListOf<String>()

‎shared/src/main/kotlin/com/jonlatane/libharmony/Pitch.kt

+2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import com.jonlatane.libharmony.Pitch.Companion.getTone
1414
*
1515
* Created by jonlatane on 12/17/16.
1616
*/
17+
@JsName("Pitch")
1718
data class Pitch(
1819
val tone: Int,
1920
val enharmonic: String? = null
2021
) {
2122
/**
2223
* Convenience constructor from enharmonic only based on [getTone]
2324
*/
25+
@JsName("PitchFromEnharmonic")
2426
constructor(enharmonic: String) : this(getTone(enharmonic), enharmonic)
2527

2628
init {

0 commit comments

Comments
 (0)
Please sign in to comment.