-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: Support tssln #172
Merged
Merged
Add: Support tssln #172
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@jsr:registry=https://npm.jsr.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@jsr:registry=https://npm.jsr.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ kotlin { | |
implementation(npm("uuid", "8.3.2")) | ||
implementation(npm("midi-file", "1.2.4")) | ||
implementation(npm("js-yaml", "4.1.0")) | ||
implementation( | ||
npm("@sevenc-nanashi/valuetree-ts", "npm:@jsr/[email protected]"), | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.external | ||
|
||
import org.khronos.webgl.Uint8Array | ||
|
||
external class ValueTree { | ||
var type: String | ||
var attributes: dynamic | ||
var children: Array<ValueTree> | ||
} | ||
|
||
@JsModule("@sevenc-nanashi/valuetree-ts") | ||
@JsNonModule | ||
external object ValueTreeTs { | ||
fun parseValueTree(text: Uint8Array): ValueTree | ||
fun dumpValueTree(tree: ValueTree): Uint8Array | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package core.io | ||
|
||
import core.exception.IllegalFileException | ||
import core.external.ValueTree | ||
import core.model.ExportResult | ||
import core.model.FeatureConfig | ||
import core.model.ImportParams | ||
import core.util.readAsArrayBuffer | ||
import org.w3c.files.File | ||
import core.external.ValueTreeTs | ||
import core.model.Format | ||
import core.model.ImportWarning | ||
import core.model.Note | ||
import core.model.TICKS_IN_BEAT | ||
import core.model.Tempo | ||
import core.model.TimeSignature | ||
import core.model.Track | ||
import org.khronos.webgl.Uint8Array | ||
import core.util.nameWithoutExtension | ||
import kotlin.math.floor | ||
|
||
object Tssln { | ||
suspend fun parse(file: File, params: ImportParams): core.model.Project { | ||
val blob = file.readAsArrayBuffer() | ||
val valueTree = ValueTreeTs.parseValueTree( | ||
Uint8Array(blob), | ||
) | ||
|
||
if (valueTree.type != "TSSolution") { | ||
throw IllegalFileException.IllegalTsslnFile() | ||
} | ||
|
||
val trackTrees = valueTree.children.first { it.type == "Tracks" }.children.filter { it.attributes.Type == 0 } | ||
|
||
val masterTrackResult = parseMasterTrack(trackTrees.first()) | ||
|
||
|
||
val tracks = parseTracks(trackTrees, params) | ||
|
||
|
||
val warnings = mutableListOf<ImportWarning>() | ||
|
||
return core.model.Project( | ||
format = format, | ||
inputFiles = listOf(file), | ||
name = file.nameWithoutExtension, | ||
tracks = tracks, | ||
tempos = masterTrackResult.first, | ||
timeSignatures = masterTrackResult.second, | ||
measurePrefix = 1, | ||
importWarnings = warnings, | ||
) | ||
} | ||
|
||
private fun parseTracks(trackTrees: List<ValueTree>, params: ImportParams): List<core.model.Track> { | ||
return trackTrees.mapIndexed { trackIndex, trackTree -> | ||
val trackName = trackTree.attributes.Name as String | ||
val pluginData = trackTree.attributes.PluginData as Uint8Array | ||
val pluginDataNode = ValueTreeTs.parseValueTree(pluginData) | ||
|
||
if (pluginDataNode.type != "StateInformation") { | ||
throw IllegalFileException.IllegalTsslnFile() | ||
} | ||
|
||
val songNode = pluginDataNode.children.first { it.type == "Song" } | ||
val scoreNode = songNode.children.first { it.type == "Score" } | ||
|
||
val notes = mutableListOf<Note>() | ||
|
||
for ((noteIndex, noteNode) in scoreNode.children.withIndex()) { | ||
if (noteNode.type != "Note") { | ||
continue | ||
} | ||
|
||
val pitchStep = noteNode.attributes.PitchStep as Int | ||
val pitchOctave = noteNode.attributes.PitchOctave as Int | ||
val rawLyric = noteNode.attributes.Lyric as String | ||
val lyric = phonemePartPattern.replace(rawLyric, "") | ||
|
||
val phoneme = (noteNode.attributes.Phoneme as String).replace(",", "") | ||
|
||
val tickOn = (noteNode.attributes.Clock as Int) | ||
val tickOff = tickOn + (noteNode.attributes.Duration as Int) | ||
|
||
notes.add( | ||
Note( | ||
id = noteIndex, | ||
key = pitchOctave * 12 + pitchStep + 12, | ||
lyric = lyric, | ||
phoneme = phoneme, | ||
tickOn = (tickOn / TICK_RATE).toLong(), | ||
tickOff = (tickOff / TICK_RATE).toLong(), | ||
), | ||
) | ||
|
||
} | ||
|
||
Track( | ||
id = trackIndex, | ||
name = trackName, | ||
notes = notes, | ||
) | ||
} | ||
} | ||
|
||
suspend fun generate(project: core.model.Project, features: List<FeatureConfig>): ExportResult { | ||
throw NotImplementedError("Not implemented") | ||
} | ||
|
||
private fun parseMasterTrack(trackTree: ValueTree): Pair<List<Tempo>, List<TimeSignature>> { | ||
val pluginData = trackTree.attributes.PluginData as Uint8Array | ||
val pluginDataNode = ValueTreeTs.parseValueTree(pluginData) | ||
if (pluginDataNode.type != "StateInformation") { | ||
throw IllegalFileException.IllegalTsslnFile() | ||
} | ||
|
||
val songNode = pluginDataNode.children.first { it.type == "Song" } | ||
|
||
val tempoNode = songNode.children.first { it.type == "Tempo" } | ||
|
||
val tempos = tempoNode.children.map { | ||
Tempo( | ||
((it.attributes.Clock as Int) / TICK_RATE).toLong(), | ||
it.attributes.Tempo as Double, | ||
) | ||
} | ||
|
||
val timeSignaturesNode = songNode.children.first { it.type == "Beat" } | ||
|
||
val timeSignatures = mutableListOf<TimeSignature>() | ||
|
||
var currentBeatIndex = 0 | ||
var currentMeasureIndex = 0 | ||
|
||
for (timeSignatureNode in timeSignaturesNode.children.sortedBy { | ||
it.attributes.Clock as Int | ||
}) { | ||
val numerator = timeSignatureNode.attributes.Beats as Int | ||
val denominator = timeSignatureNode.attributes.BeatType as Int | ||
val clock = timeSignatureNode.attributes.Clock as Int | ||
val beatIndex = floor(clock / TICK_RATE / TICKS_IN_BEAT).toInt() | ||
|
||
val beatNum = beatIndex - currentBeatIndex | ||
val beatLength = numerator / denominator * 4 | ||
|
||
if (beatNum < 0) { | ||
throw IllegalFileException.IllegalTsslnFile() | ||
} | ||
|
||
val measureIndex = currentMeasureIndex + beatNum / beatLength | ||
|
||
currentBeatIndex = beatIndex | ||
currentMeasureIndex = measureIndex | ||
|
||
timeSignatures.add( | ||
TimeSignature( | ||
measureIndex, | ||
numerator, | ||
denominator, | ||
), | ||
) | ||
} | ||
|
||
return Pair(tempos, timeSignatures) | ||
} | ||
|
||
private val TICK_RATE = 2.0 | ||
|
||
private val phonemePartPattern = Regex("""\[.+?\]""") | ||
|
||
private val format = Format.Tssln | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
kotlin.code.style=official | ||
kotlin.js.compiler=ir | ||
kotlin.daemon.jvmargs=-Xmx4G | ||
kotlin.js.yarn=false | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,6 +280,11 @@ | |
"@jridgewell/resolve-uri" "^3.1.0" | ||
"@jridgewell/sourcemap-codec" "^1.4.14" | ||
|
||
"@jsr/sevenc-nanashi__binaryseeker@^1.0.0": | ||
version "1.0.0" | ||
resolved "https://npm.jsr.io/~/11/@jsr/sevenc-nanashi__binaryseeker/1.0.0.tgz#4C4A478A37281DB5CF13018A6DAB5532C34C1995" | ||
integrity sha512-aaVVAuCXw4YwZVeqOd4IzqvzVD8iA4ODl0Ta31m87sjGm3mQxo5/Whg+LDFcFekUUve78JKXgjWSX1DckwbO0Q== | ||
|
||
"@leichtgewicht/ip-codec@^2.0.1": | ||
version "2.0.4" | ||
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b" | ||
|
@@ -467,6 +472,13 @@ | |
estree-walker "^1.0.1" | ||
picomatch "^2.2.2" | ||
|
||
"@sevenc-nanashi/valuetree-ts@npm:@jsr/[email protected]": | ||
version "0.1.2" | ||
resolved "https://npm.jsr.io/~/11/@jsr/sevenc-nanashi__valuetree-ts/0.1.2.tgz#AFACC4F569FB0BD59D55A1EC5E45C30324F6F0A9" | ||
integrity sha512-PacPrHHNWb3V5ao1YvrWZ/xzggMTYEwyzTfDbM4cnHN5gorcKuH8iEgz2NrL7WQuelH2QbTQkH5HyB4qHpR40Q== | ||
dependencies: | ||
"@jsr/sevenc-nanashi__binaryseeker" "^1.0.0" | ||
|
||
"@socket.io/component-emitter@~3.1.0": | ||
version "3.1.0" | ||
resolved "https://registry.yarnpkg.com/@socket.io/component-emitter/-/component-emitter-3.1.0.tgz#96116f2a912e0c02817345b3c10751069920d553" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to quit yarn to use jsr packages (Kotlin/JS does not recognize .npmrc with yarn)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay let's just use npm. I'm always uncomfortable with the yarn.lock file as well 😂
Can we remove those yarn-specific files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deleted
yarn.lock
but it spawns again somehow...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow it started to work, so I'm reverting this change.