@@ -4504,7 +4504,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
4504
4504
local API_VERSION = ' v1'
4505
4505
local OPEN_AI_URL = ORIGIN .. " /" .. API_VERSION
4506
4506
local COMPLETION_URL = OPEN_AI_URL .. " /chat/completions"
4507
- local CHAT_URL = OPEN_AI_URL .. " /chat"
4508
4507
openai_api_key = openai_api_key or " invalid key"
4509
4508
local openai = {}
4510
4509
local function call_openai (url , body , callback_or_timeout )
@@ -4521,7 +4520,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
4521
4520
jsresult = result
4522
4521
end
4523
4522
end
4524
- print (" openai returned: " .. result )
4525
4523
if type (callback_or_timeout ) == " function" then
4526
4524
callback_or_timeout (success , jsresult )
4527
4525
else
@@ -4549,17 +4547,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
4549
4547
}
4550
4548
return call_openai (COMPLETION_URL , body , callback_or_timeout )
4551
4549
end
4552
-
4553
- function openai .create_chat (model , prompt , temperature , max_tokens , callback_or_timeout )
4554
- callback_or_timeout = callback_or_timeout or 5.0
4555
- local body = {
4556
- model = model ,
4557
- prompt = prompt ,
4558
- temperature = temperature ,
4559
- max_tokens = max_tokens
4560
- }
4561
- return call_openai (CHAT_URL , body , callback_or_timeout )
4562
- end
4563
4550
return openai
4564
4551
end
4565
4552
package.preload [" library.utils" ] = package.preload [" library.utils" ] or function ()
@@ -4892,7 +4879,7 @@ function plugindef()
4892
4879
Check the pricing at the OpenAI site.
4893
4880
]]
4894
4881
finaleplugin .HashURL = " https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/lyrics_openai_hyphenation.hash"
4895
- return " Lyrics Hyphenation" , " Lyrics Hyphenation" ,
4882
+ return " Lyrics Hyphenation... " , " Lyrics Hyphenation" ,
4896
4883
" Add or correct lyrics hypenation using your OpenAI account."
4897
4884
end
4898
4885
local mixin = require (" library.mixin" )
@@ -4907,10 +4894,11 @@ local config =
4907
4894
Hyphenate the following text, delimiting words with spaces and syllables with hyphens.
4908
4895
If a word has multiple options for hyphenation, choose the one with the most syllables.
4909
4896
If words are already hyphenated, correct any mistakes found.
4910
- Exclude Text:
4911
- ^font(...)
4912
- ^size(...)
4913
- ^nfx(...)
4897
+ Do not modify text with the following patterns (where [TEXT_PLACEHOLDER] is any sequence of characters):
4898
+ ^font([TEXT_PLACEHOLDER])
4899
+ ^Font([TEXT_PLACEHOLDER])
4900
+ ^size([TEXT_PLACEHOLDER])
4901
+ ^nfx([TEXT_PLACEHOLDER])
4914
4902
Special Processing:
4915
4903
Do not modify line endings.
4916
4904
Identify the language. If it is a language that does not use spaces, nevertheless separate each word with a space and each pronounced syllable inside each word with a hyphen.
@@ -4919,10 +4907,11 @@ Input:
4919
4907
remove_hyphens_prompt = [[
4920
4908
Remove hyphens from the following text that has been used for musical text underlay.
4921
4909
If a word should be hyphenated according to non-musical usage, leave those hyphens in place.
4922
- Exclude Text:
4923
- ^font(...)
4924
- ^size(...)
4925
- ^nfx(...)
4910
+ Do not modify text with the following patterns (where [TEXT_PLACEHOLDER] is any sequence of characters):
4911
+ ^font([TEXT_PLACEHOLDER])
4912
+ ^Font([TEXT_PLACEHOLDER])
4913
+ ^size([TEXT_PLACEHOLDER])
4914
+ ^nfx([TEXT_PLACEHOLDER])
4926
4915
Special Processing:
4927
4916
Do not remove any punctuation other than hyphens.
4928
4917
Do not modify line endings.
@@ -4946,8 +4935,10 @@ local lyrics_prefs =
4946
4935
}
4947
4936
config .use_edit_control = config .use_edit_control and (finenv .UI ():IsOnMac () or finale .FCCtrlEditText )
4948
4937
local use_edit_text
4938
+ local use_active_lyric = finale .FCActiveLyric ~= nil
4949
4939
https_session = nil
4950
4940
update_automatically = true
4941
+ global_timer_id = 1
4951
4942
local function fixup_line_endings (input_str )
4952
4943
local replacement = " \r "
4953
4944
if finenv :UI ():IsOnWindows () then
@@ -4967,7 +4958,26 @@ local function fixup_line_endings(input_str)
4967
4958
end
4968
4959
return result
4969
4960
end
4970
- local function update_dlg_text (lyrics_box , itemno , type )
4961
+ local function update_to_active_lyric (edit_type , popup )
4962
+ if not use_active_lyric then return end
4963
+ local selected_text = finale .FCString ()
4964
+ popup :GetText (selected_text )
4965
+ name = selected_text .LuaString
4966
+ finenv .StartNewUndoBlock (" Update Current Lyric to " .. name .. " " .. edit_type :GetInteger (), false )
4967
+ local active_lyric = finale .FCActiveLyric ()
4968
+ if active_lyric :Load () then
4969
+ if active_lyric .BlockType ~= popup :GetSelectedItem () + 1 or active_lyric .TextBlockID ~= edit_type :GetInteger () then
4970
+ active_lyric .BlockType = popup :GetSelectedItem () + 1
4971
+ active_lyric .TextBlockID = edit_type :GetInteger ()
4972
+ active_lyric .Syllable = 1
4973
+ active_lyric :Save ()
4974
+ end
4975
+ end
4976
+ finenv .EndUndoBlock (true )
4977
+ end
4978
+ local function update_dlg_text (lyrics_box , edit_type , popup )
4979
+ local itemno = edit_type :GetInteger ()
4980
+ local type = popup :GetSelectedItem () + 1
4971
4981
local lyrics_instance = lyrics_classes [type ]()
4972
4982
if lyrics_instance :Load (itemno ) then
4973
4983
local fcstr = lyrics_instance :CreateString ()
@@ -4994,11 +5004,17 @@ local function update_dlg_text(lyrics_box, itemno, type)
4994
5004
lyrics_box :SetText (" " )
4995
5005
end
4996
5006
end
5007
+ update_to_active_lyric (edit_type , popup )
4997
5008
end
4998
- local function update_document (lyrics_box , itemno , type , name )
5009
+ local function update_document (lyrics_box , edit_type , popup )
4999
5010
if https_session then
5000
5011
return
5001
5012
end
5013
+ local itemno = edit_type :GetInteger ()
5014
+ local type = popup :GetSelectedItem () + 1
5015
+ local selected_text = finale .FCString ()
5016
+ popup :GetText (selected_text )
5017
+ name = selected_text .LuaString
5002
5018
finenv .StartNewUndoBlock (" Update " .. name .. " " .. itemno .. " Lyrics" , false )
5003
5019
local lyrics_instance = lyrics_classes [type ]()
5004
5020
local loaded = lyrics_instance :Load (itemno )
@@ -5045,7 +5061,7 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
5045
5061
if auto_update then
5046
5062
local selected_text = finale .FCString ()
5047
5063
popup :GetText (selected_text )
5048
- update_document (lyrics_box , edit_type : GetInteger () , popup : GetSelectedItem () + 1 , selected_text . LuaString )
5064
+ update_document (lyrics_box , edit_type , popup )
5049
5065
end
5050
5066
else
5051
5067
finenv .UI ():AlertError (result , " OpenAI" )
@@ -5059,7 +5075,7 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
5059
5075
local lyrics_text = finale .FCString ()
5060
5076
lyrics_box :GetText (lyrics_text )
5061
5077
else
5062
- update_dlg_text (lyrics_box , edit_type : GetInteger () , popup : GetSelectedItem () + 1 )
5078
+ update_dlg_text (lyrics_box , edit_type , popup )
5063
5079
lyrics_text .LuaString = lyrics_box .LuaString
5064
5080
end
5065
5081
lyrics_text :TrimWhitespace ()
@@ -5074,6 +5090,22 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
5074
5090
https_session = openai .create_completion (config .api_model , prompt , config .temperature , callback )
5075
5091
end
5076
5092
end
5093
+ local function update_from_active_lyric (lyrics_box , edit_type , popup )
5094
+ if not use_active_lyric then return end
5095
+ local active_lyric = finale .FCActiveLyric ()
5096
+ if active_lyric :Load () then
5097
+ if active_lyric .BlockType ~= popup :GetSelectedItem () + 1 or active_lyric .TextBlockID ~= edit_type :GetInteger () then
5098
+ if update_automatically and use_edit_text then
5099
+ local selected_text = finale .FCString ()
5100
+ popup :GetText (selected_text )
5101
+ update_document (lyrics_box , edit_type , popup )
5102
+ end
5103
+ popup :SetSelectedItem (active_lyric .BlockType - 1 )
5104
+ edit_type :SetInteger (active_lyric .TextBlockID )
5105
+ update_dlg_text (lyrics_box , edit_type , popup )
5106
+ end
5107
+ end
5108
+ end
5077
5109
local function create_dialog_box ()
5078
5110
dlg = mixin .FCXCustomLuaWindow ()
5079
5111
:SetTitle (" Lyrics OpenAI Hyphenator" )
@@ -5089,7 +5121,7 @@ local function create_dialog_box()
5089
5121
:SetWidth (25 )
5090
5122
:SetInteger (1 )
5091
5123
local lyrics_box
5092
- local yoff = 35
5124
+ local yoff = 45
5093
5125
if config .use_edit_control then
5094
5126
if use_edit_text then
5095
5127
lyrics_box = dlg :CreateEditText (10 , yoff )
@@ -5122,7 +5154,7 @@ local function create_dialog_box()
5122
5154
dlg :RegisterHandleControlEvent (update , function (control )
5123
5155
local selected_text = finale .FCString ()
5124
5156
popup :GetText (selected_text )
5125
- update_document (lyrics_box , lyric_num : GetInteger () , popup : GetSelectedItem () + 1 , selected_text . LuaString )
5157
+ update_document (lyrics_box , lyric_num , popup )
5126
5158
end )
5127
5159
dlg :RegisterHandleControlEvent (auto_update , function (control )
5128
5160
update_automatically = control :GetCheck () ~= 0
@@ -5133,20 +5165,38 @@ local function create_dialog_box()
5133
5165
5134
5166
5135
5167
dlg .OkButtonCanClose = true
5168
+ if use_active_lyric then
5169
+ dlg :SetTimer (global_timer_id , 100 )
5170
+ end
5136
5171
end )
5172
+ if use_active_lyric then
5173
+ dlg :RegisterHandleTimer (function (dialog , timer_id )
5174
+ if timer_id ~= global_timer_id then return end
5175
+ update_from_active_lyric (lyrics_box , lyric_num , popup )
5176
+ end )
5177
+ end
5137
5178
dlg :RegisterHandleControlEvent (popup , function (control )
5138
- update_dlg_text (lyrics_box , lyric_num : GetInteger () , popup : GetSelectedItem () + 1 )
5179
+ update_dlg_text (lyrics_box , lyric_num , popup )
5139
5180
end )
5140
5181
dlg :RegisterHandleControlEvent (lyric_num , function (control )
5141
- update_dlg_text (lyrics_box , lyric_num : GetInteger () , popup : GetSelectedItem () + 1 )
5182
+ update_dlg_text (lyrics_box , lyric_num , popup )
5142
5183
end )
5143
5184
dlg :RegisterHandleControlEvent (hyphenate , function (control )
5144
5185
hyphenate_dlg_text (lyrics_box , popup , lyric_num , update_automatically , false )
5145
5186
end )
5146
5187
dlg :RegisterHandleControlEvent (dehyphenate , function (control )
5147
5188
hyphenate_dlg_text (lyrics_box , popup , lyric_num , update_automatically , true )
5148
5189
end )
5149
- update_dlg_text (lyrics_box , lyric_num :GetInteger (), popup :GetSelectedItem () + 1 )
5190
+ dlg :RegisterCloseWindow (function ()
5191
+ if finenv .UI ():IsOnMac () and use_active_lyric then
5192
+ finenv .RetainLuaState = false
5193
+ end
5194
+ end )
5195
+ if use_active_lyric then
5196
+ update_from_active_lyric (lyrics_box , lyric_num , popup )
5197
+ else
5198
+ update_dlg_text (lyrics_box , lyric_num , popup )
5199
+ end
5150
5200
return dlg
5151
5201
end
5152
5202
local function openai_hyphenation ()
0 commit comments