Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;**********************************;
- ; CHALLENGE-SPECIFIC ALAISES ;
- ;**********************************;
- alias FancyEncodeExample {
- FancyEncode Sed posuere condimentum turpis, vel cursus felis ultricies sit amet. Proin auctor massa nec neque hendrerit eleifend. Pellentesque ullamcorper tellus sit amet sagittis aliquet. Integer auctor egestas turpis, quis sollicitudin erat rutrum sit amet. Aliquam suscipit, dolor ac porttitor convallis, lorem turpis pulvinar massa, quis pharetra lorem lorem varius erat. Donec lacus mi, porttitor a tortor et, congue gravida purus. Duis sed sem lorem. Cras sollicitudin leo eu lectus commodo, aliquam accumsan libero mollis. Aliquam imperdiet vulputate eros tempus mollis. Cras vel tincidunt sapien. Aliquam sit amet dictum enim. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Quisque et diam et magna fermentum suscipit. Vestibulum et dolor nec risus laoreet volutpat. Nulla vitae mauris non turpis aliquet lobortis. Sed efficitur euismod dolor, sed rhoncus sapien scelerisque ut. Proin posuere, metus et dignissim interdum, orci dui suscipit turpis, ve nullam.
- }
- alias FancyEncode {
- if ($0) {
- bset -tc &FancyEncodeInput 1 $1-
- }
- FancyEncoder &FancyEncodeInput
- }
- alias FancyDecode {
- return $bvar($FancyDecoder, 1-).text
- }
- ;***********************;
- ; UTILITY ALIASES ;
- ;***********************;
- ; Aliases used by this script that are not script-specific
- ; /badd -tac &bvar values...
- ; Appends the values to the bvar
- ; Switches are the same as /bset
- alias -l badd {
- if ($isid) {
- return
- }
- elseif (-* iswm $1) {
- bset $1-2 $calc($bvar($2,0) +1) $3-
- }
- else {
- bset $1 $calc($bvar($1,0) +1) $2-
- }
- }
- ; $ModifyColor(RGBValue, Modifier)[.add]
- ; $ModifyColor(RedValue, GreenValue, BlueValue, Modifier)[.add]
- ;
- ; Modifies the input color by multiplying the colors R,G,B values by the modifier
- ; If the .ad prop is specified, the modifier is added instead
- ;
- ; Returns the color as an integer
- alias -l ModifyColor {
- ; If there's only two inputs the first is an RGBValue the second is the modifider
- ; 1. convert the color from $1 to R,G,B and store each value
- ; 2. set the modifier to $2
- ; Otherwise
- ; 1. Store the R($1), G($2), B($3) values
- ; 2. set the modifier to $4
- if ($0 == 2) {
- var %Color = $rgb($1), %Red = $gettok(%Color, 1, 44), %Green = $gettok(%Color, 2, 44), %Blue = $gettok(%Color, 3, 44), %Mod = $2
- }
- else {
- var %Red = $1, %Green = $2, %Blue = $3, %Mod = $4
- }
- ; If the prop is 'add' add the midifier to the R,G,B values and roudn to the closest whole number
- if ($prop == add) {
- var %Red = $round($calc(%Mod + %Red), 0), %Green = $round($calc(%Mod + %Green), 0), %Blue = $round($calc(%Mod + %Blue), 0)
- }
- ; otherwise multuple the R, G, B values by the modifier, ane round to the closest whole number
- else {
- var %Red = $round($calc(%Mod * %Red), 0), %Green = $round($calc(%Mod * %Green), 0), %Blue = $round($calc(%Mod * %Blue), 0)
- }
- ; Cap the R, G, B values at 255
- if (%Red > 255) {
- %Red = 255
- }
- if (%Green > 255) {
- %Green = 255
- }
- if (%Blue > 255) {
- %Blue = 255
- }
- ;; Conver the R, G, B values to a color and return the result
- return $rgb(%Red, %Green, %Blue)
- }
- ; $BlendColors(RGBValueA, RGBValueB, Percentage)[.rgb]
- ; $BlendColors(RedA, GreenA, BlueA, RedB, GreenB, BlueB, Percentage)[.rgb]
- ;
- ; Mixes the input colors where the specified percentage is the amount of the first color to use
- ; Percentage = 1.0 = All of first color, none of the second
- ; Percentage = 0.5 = Equal parts first and second color
- ; Percentage = 0.0 = None of first color, All of second color
- ;
- ; If .rgb is specified the color is returned in R,G,B format
- ; Otherwise the color is returned as an integer
- alias -l BlendColors {
- ; Default mixing percentage
- var %Percentage = .5, %Red, %Green, %Blue
- ; If 2-3 inputs were given
- ; Assume the input colors are integer values
- if ($0 isnum 2-3) {
- ; If the first input contains a comma(,):
- ; assume the input is formatted as <R,G,B>
- ;
- ; Otherwise
- ; Assume its an integer color value and convert to R,B,G format
- if ($chr(44) isin $1) {
- %ColorA = $1
- }
- else {
- %ColorA = $rgb($1)
- }
- ; If the second input contains a comma(,):
- ; assume the input is formatted as <R,G,B>
- ;
- ; Otherwise
- ; Assume its an integer color value and convert to R,B,G format
- if ($chr(44) isin $2) {
- %ColorB = $2
- }
- else {
- %ColorB = $rgb($2)
- }
- ; Extract the Red, Green and blue values from the two colors
- var %RedA = $gettok(%ColorA, 1, 44), %GreenA = $gettok(%ColorA, 2, 44), %BlueA = $gettok(%ColorA, 3, 44)
- var %RedB = $gettok(%ColorB, 1, 44), %GreenB = $gettok(%ColorB, 2, 44), %BlueB = $gettok(%ColorB, 3, 44)
- ; If a 3rd input is specified, use it for the percentage
- if ($0 == 3) {
- %Percentage = $3
- }
- }
- ; Otherwise assume the RGB values are given for each color
- else {
- var %RedA = $1, %GreenA = $2, %BlueA = $3
- var %RedB = $4, %GreenB = $5, %BlueB = $6
- ; if a 7th input is given, assume its the mix percentage
- if ($0 == 7) {
- %Percentage = $7
- }
- }
- ; Mix colors
- var %Red = $round($calc((%RedA * %Percentage) + (%RedB * (1 - %Percentage))), 0)
- var %Green = $round($calc((%GreenA * %Percentage) + (%GreenB * (1 - %Percentage))), 0)
- var %Blue = $round($calc((%BlueA * %Percentage) + (%BlueB * (1 - %Percentage))), 0)
- ; Cap each color value at 255
- if (%Red > 255) {
- %Red = 255
- }
- if (%Green > 255) {
- %Green = 255
- }
- if (%Blue > 255) {
- %Blue = 255
- }
- ; Return as R,G,B format
- if ($prop == rgb) {
- return $+(%Red, $chr(44), %Green, $chr(44), %Blue)
- }
- ; Return as integer
- return $rgb(%Red, %Green, %Blue)
- }
- ;**********************;
- ; EVENT HANDLING ;
- ;**********************;
- ; When text is entered into the Main window's editbox
- ; if ctrlenter was pressed or the lead character is not the command character
- ; encode the input text
- on *:INPUT:@FancyEncode:{
- if ($ctrlenter) || ($left($1,1) !== / && $left($1,1) !== $comchar) {
- FancyEncode $1-
- halt
- }
- }
- ; Cleanup on load, start, when either window closes, mirc exits or the script is unloaded
- on *:LOAD:{
- cleanup
- echo -s * Optimal Input
- echo -s * 50 to 1000 input characters, the more the merrier
- echo -s * More diverse inputs are recommended
- echo -s *
- echo -s * If you have a MIDI-capable device you can play the sound
- echo -s * file of the encode via right clicking the @FancyEncode
- echo -s * window
- }
- on *:START:cleanup
- on *:CLOSE:@FancyEncode:cleanup
- on *:CLOSE:@FancyEncodeBuffer:cleanup
- on *:EXIT:cleanup
- on *:UNLOAD:cleanup
- alias -l cleanup {
- if ($timer(FancyEncodeEBSelect)) {
- .timerFancyEncodeEBSelect off
- }
- if ($window(@FancyEncodeBuffer)) {
- close -@ @FancyEncodeBuffer
- }
- if ($window(@FancyEncode)) {
- close -@ @FancyEncode
- }
- if ($inmidi.fname === $scriptdirFancyEncode.mid) {
- splay stop
- }
- if ($isfile($scriptdirFancyEncode.mid)) {
- .remove $qt($scriptdirFancyEncode.mid)
- }
- }
- ;************************;
- ; MAIN WINDOW MENU ;
- ;************************;
- menu @FancyEncode {
- $submenu($FancyEncodeMenu($1))
- }
- ; Generates the Main window menu based on current state
- alias -l FancyEncodeMenu {
- ; First menu item
- ; If
- ; There's no data encoded(determined by checking if the first pixel of the center line)
- ; OR a sound being played that isn't a MIDI
- ; OR a MIDI is being played that isn't FancyEncode.mid
- ; OR the FancyEncode.mid file doesn't exist
- ; return a disabled 'Play' menu item
- ;
- ; elseif
- ; the MIDI being played is the FancyEncode.mid
- ; return a 'Stop' menu item
- ;
- ; else
- ; return a 'Play' menu item
- if ($1 == 1) {
- if ($getdot(@FancyEncode, 10, 264) == 0) {
- return $style(2) Play:FancyEncodeToggleSound
- }
- elseif ($insong || $inwave) {
- return $style(2) Play:FancyEncodeToggleSound
- }
- elseif ($inmidi && $inmidi.fname !== $scriptdirFancyEncode.mid) {
- return $style(2) Play:FancyEncodeToggleSound
- }
- elseif (!$isfile($scriptdirFancyEncode.mid)) {
- return $style(2) Play:FancyEncodeToggleSound
- }
- elseif ($inmidi) {
- return Stop: FancyEncodeToggleSound
- }
- return Play: FancyEncodeToggleSound
- }
- ; Second Menu Item - Menu Item Seperator
- elseif ($1 == 2) {
- return -
- }
- ; Third Menu Item
- ; if
- ; There's no data encoded(determined by checking if the first pixel of the center line)
- ; return a disabled 'Save' menu item
- ; else
- ; return a 'Save' menu item
- elseif ($1 == 3) {
- if (!$getdot(@FancyEncode, 10, 264)) {
- return Style(2) $+ Save MIDI: FancyEncodeSaveMidi
- }
- return Save MIDI: FancyEncodeSaveMidi
- }
- }
- ; Toggles the playing sound when called
- ; If the currently playing sound is the FancyEncode.mid file, stop the playing
- ; elseif there's a sound being played that isn't FancyEncode.mid do nothing
- ; elseif the FancyEncode.mid file exists, play it
- alias -l FancyEncodeToggleSound {
- if ($inmidi.fname == $scriptdirFancyEncode.mid) {
- splay stop
- }
- elseif ($insong || $inwave || $inmidi) {
- return
- }
- elseif ($isfile($scriptdirFancyEncode.mid)) {
- var %Start = 10
- var %Max = $calc($window(@FancyEncodeBuffer).bw - 10)
- var %Next
- var %Cur = %Start
- var %Block = $calc(%Max - 10)
- while (%Block > 0) {
- %Next = $calc(%Cur + %Block)
- if (%Next > %Max) {
- %Cur = %Max
- break
- }
- elseif ($getdot(@FancyEncodeBuffer, %Next, 264)) {
- %Cur = %Next
- }
- else {
- %Block = $int($calc(%Block / 2))
- }
- }
- .timerFancyEncodePlayPosition -m 1 100 FancyEncodePlayPosition %Cur
- splay -m $qt($scriptdirFancyEncode.mid)
- }
- }
- ; Saves the temporily generated midi
- ; If data was encoded and the FancyEncode.mid file exists
- ; copy the temporary MIDI to a user-specified directory
- alias -l FancyEncodeSaveMidi {
- if ($getdot(@FancyEncode, 10, 264) && $isfile($scriptdirFancyEncode.mid)) {
- copy $qt($scriptdirFancyEncode.mid) $qt($$sfile($envvar(userprofile) $+ FancyEncode $asctime(yyyy-mm-dd HH.nn.ss) $+ .mid, Save Midi, Save))
- }
- }
- ;***************;
- ; ENCODER ;
- ;***************;
- ; /FancyEncoder &InputBvar
- ; Encodes the bytes contained in &InputBvar
- alias FancyEncoder {
- if ($inmidi.fname == $scriptdirFancyEncode.mid) {
- splay stop
- }
- ; Variable setup
- var %WinW 1164
- var %WinH 557
- var %EBText Input text here and press enter to encode
- var %OptionsText Right-Click for Options
- var %ColorsList 255,0,0 255,127,0 255,255,0 0,255,0 0,0,255 75,0,130 139,0,255
- var %NoteList 48 50 52 53 55 57 59 60 62 64 65 67 69 71 72 74 76 77 79 81 83
- var %StartTime $ticks,%7BitLength,%InputLength,%ColorBlock,%ColorsCount $numtok(%ColorsList, 32),%InputIndex 0,%BitBuffer,%7BitIndex 0,%7BitValue,%NoteCount $numtok(%NoteList, 32),%NoteChordAdds 84 86 88 89 91 93 95,%NoteDurationBlock 21,%NoteDuration,%NoteA,%NoteB,%NoteC,%ColorIndex,%ColorEnd,%ColorPercent,%ColorRed,%ColorGreen,%ColorBlue,%XCoord,%LineLength,%YCoord,%Color
- ;***************************;
- ; BUFFER WINDOW SETUP ;
- ;***************************;
- ; Setup buffer window
- if (!$window(@FancyEncodeBuffer)) {
- window -nBCdfhpz @FancyEncodeBuffer -1 -1 %WinW %WinH
- }
- ; fill with black background
- drawrect -fr @FancyEncodeBuffer 0 1 0 0 %WinW %WinH
- ; draw "Right-Click for Options" text to the Buffer window
- drawtext -r @FancyEncodeBuffer $rgb(0,128,255) Courier 15 $calc(%WinW - 5 - $width(%OptionsText, Courier, 15)) 537 %OptionsText
- ;*************************;
- ; MAIN WINDOW SETUP ;
- ;*************************;
- ; If the main window doesn't exist
- ; 1. Create the window
- ; 2. Start a timer to fill the editbox with default text
- ;
- ; Otherwise activate the window
- if (!$window(@FancyEncode)) {
- window -BCdefk0pz +bt @FancyEncode -1 -1 %WinW %WinH
- .timerFancyEncodeEBSelect 1 0 if ($window(@FancyEncode) && $!editbox(@FancyEncode) == $!null) $({) editbox -b1e $+ $calc(1+ $len(%EBText)) @FancyEncode %EBText $(})
- }
- else {
- window -a @FancyEncode
- }
- ;; Fill main window with black background
- drawrect -fr @FancyEncode 0 1 0 0 %WinW %WinH
- ;; Nothing to encode
- ;; Draw "Nothing to Encode" mess to main window and exit processing
- if ($0 !== 1 || $left($1,1) !== & || !$bvar($1,0)) {
- drawtext -r @FancyEncode $rgb(0,128,255) Arial 20 10 10 Nothing to Encode
- return
- }
- ;; Otherwise, draw "Encoding..." to main window
- drawtext -r @FancyEncode $rgb(0,128,255) Arial 20 10 10 Encoding...
- ;**********************;
- ; ENCODING SETUP ;
- ;**********************;
- ; Number of bytes to be encoded
- %InputLength = $bvar($1, 0)
- ; Setup the MIDI:
- ; LeftByte[1-4] = "MThd" (File-Header)
- ; LeftByte[5-8] = 6 as uint32 (File-Header's Data Length; always 6)
- ; LeftByte[9-10] = 0 as uint16 (Format; 0 for single track)
- ; LeftByte[11-12] = 1 as uint16 (Number of Tracks)
- ; LeftByte[13-14] = 128 as uint16 (MIDI ticks per quarter note)
- ;
- ; LeftByte(15-18] = "MTrk" (Track-Header)
- ; LeftByte[19-22] = 0 as uint32 (Track-Length; filled in after encoding)
- ;
- ; All bytes beyond LeftByte[22] are event-related and formated as: <delay_after_last_event><event>[<params...>]
- ;
- ; LeftByte[23-25] = <delay:0><SET_INSTRUMENT(192) + Channel(0))><Instrument:1 (piano)>
- bset -c &FancyEncodeMidi 1 77 84 104 100 0 0 0 6 0 0 0 1 0 128 77 84 114 107 0 0 0 0 0 192 1
- ; Number of 7bit values that will be created from the 8bit input values; more info under "ENCODING: Byte conversion"
- %7BitLength = $ceil($calc(%InputLength * 8 / 7))
- ; Color Block size
- %ColorBlock = %7BitLength / %ColorsCount
- ;**********************;
- ; ENCODING START ;
- ;**********************;
- ; The encoder processes the input into 7bit values as needed. This is done to:
- ; ...add variance between the input and what is displayed
- ; ...add variance between similar but not exact inputs
- ; ...make the resulting MIDI less chaotic
- ; Loop while their are bytes from the input, or bits in the bit buffer to be consumed
- while (%InputIndex < %InputLength || %BitBuffer !== $null) {
- ;*********************************;
- ; ENCODING: Byte conversion ;
- ;*********************************;
- ; The 8bit input values are converted to 7bit equivulants as such
- ; 1. When the bit buffer variable has less than 7 bits (characters):
- ; 1. The next 8bit byte from the input is read
- ; 2. The byte is converted to binary represented as text
- ; 3. The binary text is appended to bit buffer
- ; 4. If, after appending the binary text, the bit buffer has less than 7 bits:
- ; The end of the input has been reached so the bit buffer is right-padded with 0s
- ; to make it 7 characters long
- ;
- ; 2. The first 7 bits (characters) of the bit buffer are converted to an integer and stored
- ; 3. The first 7 bits (characters) of the bit buffer are removed
- ; if the bit buffer has less than 7 'bits'
- ; 1. Read the next byte from the input and convert it to binary represented as text
- ; 2. If the bit buffer still doesn't have 7 bytes:
- ; The end of the input has been reached so right-pad the bit buffer with 0s
- if ($len(%BitBuffer) < 7) {
- inc %InputIndex
- %BitBuffer = %BitBuffer $+ $base($bvar($1, %InputIndex, 1), 10, 2, 8)
- if ($len(%BitBuffer) < 7) {
- %BitBuffer = %BitBuffer $+ $str(0, $calc(7 - $v1))
- }
- }
- ; Increase the current-value-index
- inc %7BitIndex
- ; Convert the first 7 'bits' in the bit buffer to an integer
- ; then remove them from the bit buffer
- %7bitValue = $base($left(%BitBuffer, 7), 2, 10)
- %BitBuffer = $mid(%BitBuffer, 8-)
- ;*************************************;
- ; ENCODING: MIDI Note Additions ;
- ;*************************************;
- ; Calculate the chord duration
- ; 1. Divide the value by the number of notes
- ; 2. Floor the value
- ; 3. Add 1 to the value
- ; 4. Multiple by the Note duration block size
- ; 5. If the duration ended up being larger than 127 ticks, cap it to 127
- ; This limits the resulting MIDI to 1/4 notes or shorter. Any duration larger than this causes the MIDI
- ; to be unplayable. If anyone has information on how to fix this, let me know
- %NoteDuration = $calc(($floor($calc(%7BitValue / %NoteCount)) +1) * %NoteDurationBlock)
- if (%NoteDur > 127) {
- %NoteDur = 127
- }
- ; Retrieve each note for the chord
- ; 1. Get the note index by: retrieving the remainder of value / number of quotes and then adding one
- ; 2. Use that index to get the note from the lists of applicable notes and store the result
- ; 3. repeat steps 1&2 for the 2nd and 3rd chord note but add 2 and 4, respectively to the 7bit value prior to division
- %NoteA = $gettok(%NoteList %NoteChordAdds, $calc(%7BitValue % %NoteCount +1), 32)
- %NoteB = $gettok(%NoteList %NoteChordAdds, $calc((%7BitValue + 2) % %NoteCount +1), 32)
- %NoteC = $gettok(%NoteList %NoteChordAdds, $calc((%7BitValue + 4) % %NoteCount +1), 32)
- ; Add the note_on event for the chord to the MIDI bvar:
- ; <delay> <NOTE_ON(144) + Channel(0)> <note> <volume>
- badd &FancyEncodeMidi 0 144 %NoteA 90
- badd &FancyEncodeMidi 0 144 %NoteB 90
- badd &FancyEncodeMidi 0 144 %NoteC 90
- ; Add the notes_off event for the chord to the MIDI bvar:
- ; <delay> <NOTE_OFF(128) + Channel(0)> <note> <volume>
- badd &FancyEncodeMidi %NoteDuration 128 %NoteA 90
- badd &FancyEncodeMidi 0 128 %NoteB 90
- badd &FancyEncodeMidi 0 128 %NoteC 90
- ;************************************;
- ; INTERMISSION: Get Color Base ;
- ;************************************;
- ; Get the start-color percentage(as a decimal):
- ; 1. Divide the value by the color block size and get the remainder
- ; 2. Divide the remainder by the color block size
- ; 3. subtract from 1
- ;
- ; 1.0 = All of start color, None of end color
- ; 0.0 = None of start color, all of end color
- %ColorPercent = $calc(1 - ( %7BitIndex % %ColorBlock )/ %ColorBlock )
- ; If this is the last value to be processed
- ; Use the first color
- if (%7BitIndex == %7BitLength) {
- %ColorPercent = 0
- }
- ; Otherwise calculate the color index by dividing the bit index by the color block size, the rounding up
- %ColorIndex = $ceil($calc(%7BitIndex / %ColorBlock))
- ; If the color index is at the end of the list use the first color in the list as the end color
- ; otherwise use the next color in the list
- if (%ColorIndex == %ColorsCount) {
- %ColorEnd = $gettok(%ColorsList, 1, 32)
- }
- else {
- %ColorEnd = $gettok(%ColorsList, $calc(%ColorIndex + 1), 32)
- }
- ; Blend the two colors then extract the R,G,B values
- %Color = $BlendColors($gettok(%ColorsList, %ColorIndex, 32), %ColorEnd, %ColorPercent).rgb
- %ColorRed = $gettok(%Color, 1, 44)
- %ColorGreen = $gettok(%Color, 2, 44)
- %ColorBlue = $gettok(%Color, 3, 44)
- ;****************************;
- ; ENCODING: Draw Lines ;
- ;****************************;
- ; Calculate the x coord
- ; 1. subtrack 1 from bit index: this is an offset to insure the bottom lines align with those on top
- ; 2. divide by 2 and round down: done to account for alternating between top and bottom
- ; 3. multiple by 2; done because the line for each value is 2px wide
- ; 4. add 10; left-side offset so there's space between the left-side and the start of lines
- %XCoord = $calc($floor($calc((%7BitIndex - 1) /2)) *2 + 10)
- ; Draw the center-line
- ; The color is modified to brighten it compared to the top & bottom half colors
- ; The line's 2nd x-coord is 1 pixel longer than expected due to drawline drawing TO the coordinate but not including it
- drawline -fnr @FancyEncodeBuffer $ModifyColor(%ColorRed, %ColorGreen, %ColorBlue, 92).add 1 %XCoord 264 $calc(%XCoord +2) 264
- ; if the 7bit value is not zero or null
- if (%7BitValue) {
- ; calculate the line length by multiplying the 7bit value by 2
- %LineLength = $calc(%7BitValue * 2)
- ; if the index is a multiple of two
- ; 1. The line is a bottom line so it should start just below center-line
- ; 2. Modify the base color to be darker
- if (2 // %7BitIndex) {
- %YCoord = 265
- %Color = $ModifyColor(%ColorRed, %ColorGreen, %ColorBlue, .5)
- }
- ; If the index is not a multiple of two
- ; 1. The line is a top-line, so subtrack the length from where it should stop at
- ; 2. Convert the RGB directly to a color
- else {
- %YCoord = $calc(264 - %LineLength)
- %Color = $rgb(%ColorRed, %ColorGreen, %ColorBlue)
- }
- ; Draw the first line as %Color indicates to the buffer window
- ; Then draw the second line after modifying the color to be slightly darker
- drawrect -nr @FancyEncodeBuffer %Color 1 %XCoord %YCoord 2 %LineLength
- drawrect -nr @FancyEncodeBuffer $ModifyColor(%Color, .7).multiply 1 $calc(%XCoord +1) %YCoord 1 %LineLength
- }
- }
- ;******************************;
- ; ENCODING: MIDI Wrap up ;
- ;******************************;
- ; Add the "track finished" event to the MIDI
- badd &FancyEncodeMidi 1 255 47 0
- ; Fill in the track length by:
- ; 1. Subtracting 22 from the MIDI bvar length
- ; 2. Using longip to get a 32bit uint formatted as x.x.x.x
- ; 3. Replace the .'s in the longip result with space
- ; 4. Insert the bytes into the bvar
- bset &FancyEncodeMidi 19 $replace($longip($calc($bvar(&FancyEncodeMidi, 0) - 22)), ., $chr(32))
- ; If a FancyEncode.mid exists, remove it
- if ($isfile($scriptdirFancyEncode.mid)) {
- .remove $qt($scriptdirFancyEncode.mid)
- }
- ; Write the midi to file
- bwrite $qt($scriptdirFancyEncode.mid) -1 &FancyEncodeMidi 1 -1
- ;******************************;
- ; ENCODING: Draw Wrap up ;
- ;******************************;
- ; Copy the buffer window's contents to the main window
- drawcopy @FancyEncodeBuffer 0 0 %WinW %WinH @FancyEncode 0 0
- ; Generate the info text
- %StartTime = $ticks - %StartTime
- var %InfoText = Time: $bytes(%StartTime,b) $+ ms - Bytes In: $bvar(&FancyEncodeInput, 0) - Bytes Out: %7BitIndex
- ; Draw the info text to the main window
- drawtext -r @FancyEncode $rgb(0,128,255) Courier 15 5 537 %InfoText
- }
- ;***************;
- ; DECODER ;
- ;***************;
- ; $FancyDecoder
- ; Returns a bvar containing the decoded text
- alias FancyDecoder {
- ;; cleanup any existing bvars
- bunset &FancyDecodeOutput
- ; variable setup
- var %buffer, %x = 10
- ; Loop while the center dot of the Buffer window is NOT black
- while ($getdot(@FancyEncodeBuffer, %x, 264)) {
- ; Get the line values for the top and bottom line
- ; Convert to a binary value represented as a string of 0's and 1's
- ; Append the strings to the buffer
- %Buffer = %Buffer $+ $base($LineValue(%x, -1),10, 2, 7) $+ $base($LineValue(%x, 1), 10, 2, 7)
- ; Loop over the bit buffer while there's atleast 7 'bits'
- ; Grab the first 7 bits, convert to an integer and append the value to the output bvar
- ; Remove the 7bits from the buffer
- while ($len(%Buffer) > 7) {
- badd &FancyDecodeOutput $base($left(%Buffer, 8), 2, 10)
- %Buffer = $mid(%Buffer, 9-)
- }
- ; Move to the next set of lines
- inc %x 2
- }
- ; return the output bvar
- return &FancyDecodeOutput
- }
- ; $LineValue(@xcoord, @modifier)
- ; Returns the value the line represents
- ;
- ; @XCoord : The Xcoord of @FancyEncode of the line to get
- ;
- ; @Modifier
- ; -1: top line
- ; 1: Bottom line
- alias -l LineValue {
- ; Calculate the current y position and block size and store the start and max positions
- var %Cur = 264
- var %Block = 254 * $2
- var %Next
- var %Start = %Cur + $2
- var %Max = 264 + %Block
- ; If start y position is black there's nothing to decode so return 0
- if (!$getdot(@FancyEncodeBuffer, $1, $calc(%Cur + $2))) {
- return 0
- }
- ; loop while the block size is NOT 0
- while (%Block !== 0) {
- ; Calculate the next y position to validate
- %Next = %Cur + %Block
- ; If the next y-position is out of the max bounds, return the max-bound as the result
- if (%Start < %Max && %next > %Max) || (%Start > %Max && %Next < %Max) {
- %Cur = %Max
- break
- }
- ; If the next y-position is not black set the current position to it
- elseif ($getdot(@FancyEncodeBuffer, $1, %Next)) {
- %Cur = %Next
- }
- ; Otherwise divide the block in half and try again
- else {
- %Block = $int($calc(%Block / 2))
- }
- }
- ; Return the line-length divided by 2
- return $abs($calc((264 - %Cur )/2))
- }
- ;********************;
- ; MIDI PLAYING ;
- ;********************;
- alias -l FancyEncodePlayPosition {
- if (!$window(@FancyEncode) || !$window(@FancyEncodeBuffer)) {
- cleanup
- }
- if ($inmidi.fname !== $scriptdirFancyEncode.mid) {
- drawcopy @FancyEncodeBuffer 0 0 $window(@FancyEncodeBuffer).bw $calc($window(@FancyEncodeBuffer).bh - 25) @FancyEncode 0 0
- }
- else {
- var %Percent, %XCoord, %NextXCoord
- %Percent = $calc($inmidi.pos / $inmidi.length)
- if (!%Percent) {
- %Percent = 0
- }
- elseif (%Percent > 1) {
- %Percent = 1
- }
- %XCoord = 9
- if ($0 > 1) {
- %XCoord = $2
- }
- %NextXCoord = $round($calc( ($1 -10) * %Percent + 10), 0)
- if (%NextXCoord > $1) {
- %NextXCoord = $v2
- }
- while (%XCoord < %NextXCoord) {
- inc %XCoord
- drawline -rn @FancyEncode $rgb(8,16,16) 1 %XCoord $calc(263 - $LineValue(%XCoord, -1) * 2) %XCoord 264
- drawline -rn @FancyEncode $rgb(8,8,8) 1 %XCoord 265 %XCoord $calc(265 + $LineValue(%XCoord, 1) * 2)
- drawdot -rn @FancyEncode $rgb(32,32,32) 1 %XCoord 264
- }
- drawdot @FancyEncode
- .timerFancyEncodePlayPosition -m 1 100 FancyEncodePlayPosition $1 %NextXCoord
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement