From 19cade82f4052ed19bf4f720c0fc9d7e8e6a3ca9 Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Mon, 11 Dec 2017 21:48:59 +0700 Subject: [PATCH 1/4] Support for bms-on, transitional format to convert BMS to bmson --- iBMSC/Audio.vb | 20 ++++++++++++- iBMSC/MainWindow.vb | 9 ++++-- iBMSC/PanelEvents.vb | 67 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/iBMSC/Audio.vb b/iBMSC/Audio.vb index be10122fe..8d8d009ed 100644 --- a/iBMSC/Audio.vb +++ b/iBMSC/Audio.vb @@ -7,6 +7,15 @@ Module Audio Dim Output As WasapiOut Dim Source As IWaveSource + Public Structure BMSONSliceResult + Public Found As Boolean + Public Start As Double + Public Length As Double + Public Overrides Function ToString() As String + Return "Slice result " & Found & " - " & Start & " => " & Length + End Function + End Structure + Public Sub Initialize() Output = New WasapiOut() CodecFactory.Instance.Register("ogg", New CodecFactoryEntry(Function(s) @@ -36,7 +45,7 @@ Module Audio Return filename End Function - Public Sub Play(ByVal filename As String) + Public Sub Play(ByVal filename As String, ByVal slice As BMSONSliceResult) If Source IsNot Nothing Then Output.Stop() @@ -55,10 +64,19 @@ Module Audio End If Source = CodecFactory.Instance.GetCodec(fn) + If slice.Found Then + Source.Position = Source.WaveFormat.MillisecondsToBytes(Conversion.Int(slice.Start)) + ' I cannot set the length of the source file :( + End If Output.Initialize(Source) Output.Play() End Sub + Public Sub Play(ByVal filename As String) + Dim slice As BMSONSliceResult + Play(filename, slice) + End Sub + Public Sub StopPlaying() Output.Stop() End Sub diff --git a/iBMSC/MainWindow.vb b/iBMSC/MainWindow.vb index cc0651a0c..ae14ba6c5 100644 --- a/iBMSC/MainWindow.vb +++ b/iBMSC/MainWindow.vb @@ -2,8 +2,6 @@ Imports iBMSC.Editor Public Class MainWindow - - 'Public Structure MARGINS ' Public Left As Integer ' Public Right As Integer @@ -486,10 +484,15 @@ Public Class MainWindow End Sub Private Sub PreviewNote(ByVal xFileLocation As String, ByVal bStop As Boolean) + Dim Slice As BMSONSliceResult + Audio.Play(xFileLocation, Slice) + End Sub + + Private Sub PreviewNote(ByVal xFileLocation As String, ByVal bStop As Boolean, ByVal Slice As BMSONSliceResult) If bStop Then Audio.StopPlaying() End If - Audio.Play(xFileLocation) + Audio.Play(xFileLocation, Slice) End Sub Private Sub AddNote(note As Note, diff --git a/iBMSC/PanelEvents.vb b/iBMSC/PanelEvents.vb index 54e5bcc1e..18468ef92 100644 --- a/iBMSC/PanelEvents.vb +++ b/iBMSC/PanelEvents.vb @@ -439,6 +439,54 @@ MoveToColumn: If xTargetColumn = -1 Then Exit Select Return NoteIndex End Function + Private Function GetBMSONSliceTime(NoteIndex As Integer, ContinueI2 As Integer, StartI2 As Integer) As BMSONSliceResult + Dim currentMS = 0.0# + Dim currentBPM = Notes(0).Value / 10000 + Dim currentBPMVPosition = 0.0# + Dim startTime = 0.0# + Dim sliceStart = 0.0# + Dim sliceEnd = 0.0# + Dim started = False + Dim found As Integer = -1 + Dim result As BMSONSliceResult + For i = 1 To UBound(Notes) + If Notes(i).ColumnIndex = niBPM Then + currentMS += (Notes(i).VPosition - currentBPMVPosition) / currentBPM * 1250 + currentBPM = Notes(i).Value / 10000 + currentBPMVPosition = Notes(i).VPosition + End If + + If i = NoteIndex Then + If found = -1 Then + result.Found = False + Return result + End If + sliceStart = currentMS + (Notes(i).VPosition - currentBPMVPosition) / currentBPM * 1250 + started = True + ElseIf started And (Notes(i).Value \ 10000 = StartI2 Or Notes(i).Value \ 10000 = ContinueI2) Then + sliceEnd = currentMS + (Notes(i).VPosition - currentBPMVPosition) / currentBPM * 1250 + If sliceEnd > sliceStart Then + result.Found = True + result.Start = sliceStart - startTime + result.Length = sliceEnd - sliceStart + Return result + End If + ElseIf Notes(i).Value \ 10000 = StartI2 And Not started Then + found = i + startTime = currentMS + (Notes(i).VPosition - currentBPMVPosition) / currentBPM * 1250 + End If + 'K(i).TimeOffset = currentMS + (K(i).VPosition - currentBPMVPosition) / currentBPM * 1250 + Next + If found Then + result.Found = True + result.Start = sliceStart - startTime + result.Length = 0 + Return result + End If + result.Found = False + Return result + End Function + Private Sub PanelPreviewNoteIndex(NoteIndex As Integer) 'Play wav If ClickStopPreview Then PreviewNote("", True) @@ -449,9 +497,24 @@ MoveToColumn: If xTargetColumn = -1 Then Exit Select If xI2 >= 1296 Then xI2 = 1295 If Not hWAV(xI2) = "" Then ' AndAlso Path.GetExtension(hWAV(xI2)).ToLower = ".wav" Then - Dim xFileLocation As String = IIf(ExcludeFileName(FileName) = "", InitPath, ExcludeFileName(FileName)) & "\" & hWAV(xI2) + Dim xFileName As String = hWAV(xI2) + Dim xFileLocation As String = IIf(ExcludeFileName(FileName) = "", InitPath, ExcludeFileName(FileName)) & "\" & xFileName + Dim Slice As BMSONSliceResult + If xFileName.Length = 4 And xFileName.Substring(0, 2) = "C-" Then + Dim TargetI2 = C36to10(xFileName.Substring(2)) + Dim TargetFileName As String = hWAV(TargetI2) + If Not TargetFileName = "" Then + Dim TargetFileLocation = IIf(ExcludeFileName(FileName) = "", InitPath, ExcludeFileName(FileName)) & "\" & TargetFileName + Slice = GetBMSONSliceTime(NoteIndex, xI2, TargetI2) + If slice.Found Then + xFileLocation = TargetFileLocation + End If + End If + End If If Not ClickStopPreview Then PreviewNote("", True) - PreviewNote(xFileLocation, False) + Console.WriteLine(xFileLocation) + Console.WriteLine(Slice) + PreviewNote(xFileLocation, False, Slice) End If End If End Sub From f9f63f607bc7e2ffeff6db8c8cbd22f4f7fb1c08 Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Tue, 12 Dec 2017 00:42:08 +0700 Subject: [PATCH 2/4] :lipstick: Display BMS-on note labels --- iBMSC/PanelDraw.vb | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/iBMSC/PanelDraw.vb b/iBMSC/PanelDraw.vb index 187229b28..ff4c2971c 100644 --- a/iBMSC/PanelDraw.vb +++ b/iBMSC/PanelDraw.vb @@ -436,6 +436,23 @@ Partial Public Class MainWindow End If End Sub + Private Function GetNoteLabel(sNote As Note) + Dim xLabel As String = C10to36(sNote.Value \ 10000) + Dim xContinue As Boolean = False + Dim xWAV As String = hWAV(sNote.Value \ 10000) + If xWAV <> "" AndAlso xWAV.Length = 4 AndAlso xWAV.Substring(0, 2) = "C-" Then + xLabel = xWAV.Substring(2) + xContinue = True + End If + If ShowFileName AndAlso hWAV(C36to10(xLabel)) <> "" Then + xLabel = Path.GetFileNameWithoutExtension(hWAV(C36to10(xLabel))) + End If + If xContinue Then + xLabel = "C:" & xLabel + End If + Return xLabel + End Function + ''' ''' Draw a note in a buffer. ''' @@ -450,10 +467,7 @@ Partial Public Class MainWindow Dim xAlpha As Single = 1.0F If sNote.Hidden Then xAlpha = vo.kOpacity - Dim xLabel As String = C10to36(sNote.Value \ 10000) - If ShowFileName AndAlso hWAV(C36to10(xLabel)) <> "" Then - xLabel = Path.GetFileNameWithoutExtension(hWAV(C36to10(xLabel))) - End If + Dim xLabel As String = GetNoteLabel(sNote) Dim xPen As Pen Dim xBrush As Drawing2D.LinearGradientBrush @@ -555,9 +569,7 @@ Partial Public Class MainWindow Dim xAlpha As Single = 1.0F If sNote.Hidden Then xAlpha = vo.kOpacity - Dim xLabel As String = C10to36(sNote.Value \ 10000) - If ShowFileName AndAlso hWAV(C36to10(xLabel)) <> "" Then xLabel = Path.GetFileNameWithoutExtension(hWAV(C36to10(xLabel))) - + Dim xLabel As String = GetNoteLabel(sNote) Dim xPen1 As Pen Dim xBrush As Drawing2D.LinearGradientBrush Dim xBrush2 As SolidBrush From 6e4aa4024cddbe273fea21a04f802137ff3a5ef7 Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Tue, 12 Dec 2017 00:56:46 +0700 Subject: [PATCH 3/4] :lipstick: Change C: prefix to > prefix --- iBMSC/PanelDraw.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iBMSC/PanelDraw.vb b/iBMSC/PanelDraw.vb index ff4c2971c..280b15205 100644 --- a/iBMSC/PanelDraw.vb +++ b/iBMSC/PanelDraw.vb @@ -448,7 +448,7 @@ Partial Public Class MainWindow xLabel = Path.GetFileNameWithoutExtension(hWAV(C36to10(xLabel))) End If If xContinue Then - xLabel = "C:" & xLabel + xLabel = ">" & xLabel End If Return xLabel End Function From f91525171c2b869dfebfab09ea8ce1ed265909da Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Tue, 12 Dec 2017 14:48:35 +0700 Subject: [PATCH 4/4] :sparkles: Trim the slice --- iBMSC/Audio.vb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/iBMSC/Audio.vb b/iBMSC/Audio.vb index 8d8d009ed..833027d97 100644 --- a/iBMSC/Audio.vb +++ b/iBMSC/Audio.vb @@ -16,6 +16,28 @@ Module Audio End Function End Structure + ' https://github.com/filoe/cscore/blob/master/CSCore.Test/utils/TrimmedWaveSource.cs + Public Class TrimmedWaveSource + Inherits WaveAggregatorBase + Private mSource As IWaveSource + Private mMaxLength As Integer + Private mRead As Integer + Public Sub New(Source As IWaveSource, MaxLength As Integer) + MyBase.New(Source) + mSource = Source + mMaxLength = MaxLength + End Sub + Public Overrides Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer + Dim ReadBytes = MyBase.Read(buffer, offset, count) + mRead = mRead + ReadBytes + If mRead > mMaxLength Then + ReadBytes = ReadBytes - (mRead - mMaxLength) + mRead = mMaxLength + End If + Return ReadBytes + End Function + End Class + Public Sub Initialize() Output = New WasapiOut() CodecFactory.Instance.Register("ogg", New CodecFactoryEntry(Function(s) @@ -66,7 +88,9 @@ Module Audio Source = CodecFactory.Instance.GetCodec(fn) If slice.Found Then Source.Position = Source.WaveFormat.MillisecondsToBytes(Conversion.Int(slice.Start)) - ' I cannot set the length of the source file :( + If slice.Length > 0 Then + Source = New TrimmedWaveSource(Source, Source.WaveFormat.MillisecondsToBytes(Conversion.Int(slice.Length))) + End If End If Output.Initialize(Source) Output.Play()