VoiceRecordImpl.mesa:
basic code to add voice to windows of both text and voice, also the DictationMachine button which transfers input from a current window to a newly created dictation window
 Ades, April 29, 1986 4:41:03 pm PDT
DIRECTORY
BasicTime USING [Pulses, MicrosecondsToPulses, PulsesToMicroseconds, GetClockPulses],
DictationOps USING [ToggleDictationMenu],
Menus USING [MenuProc],
MessageWindow USING [Append, Blink],
Process USING [Detach, Priority, priorityRealTime, SetPriority, Pause, MsecToTicks],
Rope USING [ROPE, Concat],
SoundList USING [SoundListFromIntervalSpecs],
TextEdit USING [GetCharProp, PutCharProp],
TextNode USING [Ref],
TiogaButtons USING [TextNodeRef],
TiogaExtraOps USING [RemoveTextKey, PutTextKey, GetTextKey],
TiogaOps USING [SelectionGrain, GetSelection, SetSelection, CallWithLocks, NoSelection, SaveSelA, FirstChild, ViewerDoc, InsertChar, RestoreSelA, Root, SetLooks],
TiogaOpsDefs USING [Location, Ref],
ViewerClasses USING [Viewer],
ViewerOps USING [FetchProp],
VoiceEditOps USING [DescribeSelection, ReplaceSelectionWithSavedInterval],
VoiceInText USING [ApplyToLockedChars, DeleteVoiceFromChar, thrushHandle],
VoiceMarkers USING [LockedAddCharMark],
VoicePlayBack USING [PlayBackInProgress, CancelPlayBack, RedrawViewer],
VoiceRope USING [VoiceRope, Record, DescribeRope],
VoiceViewers USING [Selection, SelectionRec, VoiceViewerInfo, soundRopeCharsPerSecond, soundRopeCharLength, SoundList, SoundInterval, SoundIntervalRec, SetViewerContents, BuildVoiceViewer],
VoiceRecord;
VoiceRecordImpl: CEDAR MONITOR IMPORTS BasicTime, DictationOps, MessageWindow, Process, Rope, SoundList, TextEdit, TiogaButtons, TiogaExtraOps, TiogaOps, ViewerOps, VoiceEditOps, VoiceInText, VoiceMarkers, VoicePlayBack, VoiceRope, VoiceViewers EXPORTS VoiceRecord = BEGIN
recordingInProgress: BOOLEANFALSE;
whilst a recording is being made, the following are its state variables
addingIntoTextViewer: BOOLEAN;
the selection where the voice is to be added, as a TiogaOps node: a text key gives the position within the node but that is implicitly represented by the atom $recordingMark
targetNode: TiogaOpsDefs.Ref;
the selection where the voice is to be added, as a VoiceViewers selection
targetSoundSelection: VoiceViewers.Selection;
cueInsertPosition: INT; -- where the little arrows will go
how long has the recording been going on, in terms of sound characters generated
recordingTimeInSoundChars: INT;
clock for timing recording
nextWakeUp: BasicTime.Pulses;
timerState: {off, abort, on};
the rope which is created as the result of this recording process
recordedRope: VoiceRope.VoiceRope;
timerOff, recordingDone: CONDITION;
RecordingInProgress: PUBLIC PROC RETURNS [BOOLEAN] = {
RETURN [recordingInProgress] };
AddVoiceProc: PUBLIC ENTRY Menus.MenuProc = {
the procedure behind all AddVoice buttons
p: PROCESS;
IF recordingInProgress THEN
{ MessageWindow.Append["Already recording", TRUE];
MessageWindow.Blink[];
RETURN
};
IF VoicePlayBack.PlayBackInProgress[] THEN
{ MessageWindow.Append["Cancel playback before trying to record", TRUE];
MessageWindow.Blink[];
RETURN
}; -- PlayBackInProgress is only a hint: to be sure we'll do a cancel to avoid race conditions
VoicePlayBack.CancelPlayBack[];
IF ~PrepareSelection[] THEN RETURN;
recordingInProgress ← TRUE;
timerState ← on;
recordedRope ← NIL;
recordingTimeInSoundChars ← 0;
fork processes (a) to call VoiceRope.Record and (b) to time the recording. (a) is detached but will return when a VoiceRope.Stop occurs. It will then place the ID of the Rope in the appropriate state variable, where the routine called as a result of hitting the stop key can get it
p ← FORK RecordNewRope[];
TRUSTED {Process.Detach[p]};
p ← FORK RecordingTimer[];
TRUSTED {Process.Detach[p]}
};
PrepareSelection: INTERNAL PROC RETURNS [succeeded: BOOLEANFALSE] = {
this procedure locks the selection and fills in the TiogaOps target info above. If the selection is a voice selection then targetSoundSelection is set up. If the selection is text then a text key is placed in the text. If the selection is pending delete then the delete is done. If the selection is a voice selection then the viewer is locked [against other voice edits]
targetViewer: ViewerClasses.Viewer;
targetStart, targetEnd: TiogaOpsDefs.Location;
targetCaretBefore, targetPendingDelete: BOOLEAN;
alreadyBeingEdited: BOOLEANFALSE;
alreadyVoiceThere: BOOLEANFALSE;
suitableViewer: BOOLEAN;
DoIt: INTERNAL PROC [root: TiogaOpsDefs.Ref] = {
level: TiogaOps.SelectionGrain;
[viewer: targetViewer, start: targetStart, end: targetEnd, caretBefore: targetCaretBefore, pendingDelete: targetPendingDelete, level: level] ← TiogaOps.GetSelection[];
suitableViewer ← targetViewer.class.flavor = $Text;
IF suitableViewer THEN
{ addingIntoTextViewer ← ViewerOps.FetchProp[targetViewer, $voiceViewerInfo] = NIL;
IF addingIntoTextViewer THEN
{ IF targetPendingDelete THEN
{ VoiceInText.ApplyToLockedChars[VoiceInText.DeleteVoiceFromChar];
TiogaOps.SetSelection[viewer: targetViewer, start: targetStart, end: targetEnd,
level: level, caretBefore: targetCaretBefore,
pendingDelete: FALSE, which: primary] -- simply makes not pending delete
};
{
targetChar: TiogaOpsDefs.Location ← IF targetCaretBefore THEN targetStart ELSE targetEnd;
node: TextNode.Ref ← TiogaButtons.TextNodeRef[targetChar.node]; -- just a type convertor
alreadyVoiceThere ← TextEdit.GetCharProp[node, targetChar.where, $voice] # NIL;
targetNode ← targetChar.node;
IF ~alreadyVoiceThere THEN TiogaExtraOps.PutTextKey[targetChar.node, targetChar.where, $recordingMark]
}
}
ELSE
{
[selection: targetSoundSelection, failed: alreadyBeingEdited] ← VoiceEditOps.DescribeSelection[which: primary, forceDelete: FALSE, returnSoundInterval: FALSE];
IF alreadyBeingEdited THEN RETURN;
IF targetSoundSelection.ropeInterval.length # 0 -- i.e. pending delete
THEN IF VoiceEditOps.ReplaceSelectionWithSavedInterval[targetSoundSelection, NIL, FALSE].viewerDeleted
THEN NewDictationWindow[]; -- this means "if applicable, do the pending delete. If that delete should reduce the window contents to zero it will disappear, so create a new one"
cueInsertPosition ← MIN [targetSoundSelection.ropeInterval.start/VoiceViewers.soundRopeCharLength, TextEdit.Size[TiogaButtons.TextNodeRef[TiogaOps.FirstChild[TiogaOps.ViewerDoc[targetSoundSelection.viewer]]]]-1] -- because you can't position the cursor as a point after the last character
cueInsertPosition ← targetSoundSelection.ropeInterval.start/ VoiceViewers.soundRopeCharLength
}
}
};
TiogaOps.CallWithLocks[DoIt ! TiogaOps.NoSelection => {suitableViewer ← FALSE; CONTINUE}];
test for failure conditions and report them to the user after releasing the viewer lock
IF NOT suitableViewer THEN
{ MessageWindow.Append["Make a selection in a tioga or text viewer first", TRUE];
MessageWindow.Blink[]
}
ELSE
{ IF alreadyBeingEdited THEN
{ MessageWindow.Append["Previous voice editing operation has yet to complete", TRUE];
MessageWindow.Blink[]
}
ELSE
{ IF alreadyVoiceThere THEN
{ MessageWindow.Append["Cannot add sound on top of another sound", TRUE];
MessageWindow.Blink[]
}
ELSE succeeded ← TRUE
}
}
};
RecordNewRope: PROC = {
BroadcastRecordingDone: ENTRY PROC = { BROADCAST recordingDone }; -- keeps the compiler happy!
recordedRope ← VoiceRope.Record[VoiceInText.thrushHandle];
BroadcastRecordingDone[]
};
RecordingTimer: PROC = { 
aVeryLongTime: BasicTime.Pulses = BasicTime.MicrosecondsToPulses[300000000];
see comments in VoicePlayBackImpl on this constant
cuePriority: Process.Priority = Process.priorityRealTime;
Process.SetPriority[cuePriority];
try to keep this process up to time: however there is compensation for delay in each wake-up by looking at a real time clock; see below
nextWakeUp ← BasicTime.GetClockPulses[];
DO
now: BasicTime.Pulses ← BasicTime.GetClockPulses[];
IF nextWakeUp - now < aVeryLongTime -- so as to cope properly with wrap-around
THEN Process.Pause[Process.MsecToTicks[BasicTime.PulsesToMicroseconds[ nextWakeUp-now]/1000]];
IF ~ IncrementRecordTimer[] THEN RETURN
ENDLOOP
};
IncrementRecordTimer: ENTRY PROC RETURNS [stillRunning: BOOLEANTRUE] = {
IF timerState # on THEN
{ timerState ← off;
BROADCAST timerOff;
RETURN [FALSE]
};
nextWakeUp ← nextWakeUp + BasicTime.MicrosecondsToPulses[ 1000000/VoiceViewers.soundRopeCharsPerSecond];
recordingTimeInSoundChars ← recordingTimeInSoundChars + 1;
IF ~addingIntoTextViewer THEN
{ voiceViewer: ViewerClasses.Viewer ← targetSoundSelection.viewer;
caretLocation: TiogaOpsDefs.Location ← [TiogaOps.FirstChild[TiogaOps.ViewerDoc [voiceViewer]], cueInsertPosition + recordingTimeInSoundChars - 1];
this code adds another marker into a voice viewer to indicate how much has been recorded
TiogaOps.SaveSelA[];
TiogaOps.SetSelection[viewer: voiceViewer, start: caretLocation, end: caretLocation, level: point, caretBefore: TRUE, pendingDelete: FALSE, which: primary];
TiogaOps.InsertChar['>]; -- used as an 'inserting voice' indicator
TiogaOps.SetSelection[viewer: voiceViewer, start: caretLocation, end: caretLocation, level: char, caretBefore: TRUE, pendingDelete: FALSE, which: primary];
TiogaOps.SetLooks["v"];
TiogaOps.RestoreSelA[]
}
};
StopRecording: PUBLIC ENTRY PROC = {
this gets called every time a STOP button is clicked, after VoiceRope.Stop has been called
IF ~recordingInProgress THEN RETURN;
IF timerState = on THEN timerState ← abort;
WHILE recordedRope = NIL DO WAIT recordingDone ENDLOOP;
WHILE timerState # off DO WAIT timerOff ENDLOOP;
recordingInProgress ← FALSE; 
IF addingIntoTextViewer
THEN
{ AddVoice: PROC [root: TiogaOpsDefs.Ref] = {
TextEdit.PutCharProp[node, targetChar.where, $voice, recordedRope.ropeID];
next line places a 'talks bubble' on the selected character - see TalksBubbleImpl
TextEdit.PutCharProp[node, targetChar.where, $Artwork, NARROW["TalksBubble", Rope.ROPE]]; -- the NARROW prevents a REF TEXT being created
};
targetChar: TiogaOpsDefs.Location ← TiogaExtraOps.GetTextKey[targetNode, $recordingMark];
node: TextNode.Ref ← TiogaButtons.TextNodeRef[targetChar.node]; -- just a type convertor
IF targetNode # targetChar.node THEN ERROR; -- **** because you've caught all those !!!
TiogaExtraOps.RemoveTextKey[targetNode, $recordingMark];
IF TextEdit.GetCharProp[node, targetChar.where, $voice] # NIL THEN RETURN; -- can happen - a store might have been performed in the intervening time
TiogaOps.CallWithLocks[AddVoice, TiogaOps.Root[targetChar.node]] -- called locked because otherwise tioga doesn't immediately repaint the artwork
}
ELSE
{ IF targetSoundSelection.voiceViewerInfo.ropeInterval.ropeID = NIL
THEN -- this is a new dictation window, so just set its contents to the new rope
{ targetSoundSelection.voiceViewerInfo.edited ← TRUE;
VoiceViewers.SetViewerContents[targetSoundSelection.viewer, targetSoundSelection.voiceViewerInfo, recordedRope.ropeID, NIL, TRUE]
}
ELSE
{ newSoundList: VoiceViewers.SoundList ← SoundList.SoundListFromIntervalSpecs [VoiceRope.DescribeRope[VoiceInText.thrushHandle, recordedRope], recordedRope.length];
newSound: VoiceViewers.SoundInterval ← NEW [VoiceViewers.SoundIntervalRec ← [ropeInterval: recordedRope^, soundList: newSoundList]];
targetSoundSelection.ropeInterval.length ← 0;
this may already be the case, but if not we already did the delete before starting to record, so set it zero. If we did the delete then targetSoundSelection.ropeInterval will not bear the correct ropeID but targetSoundSelection.voiceViewerInfo and that is the ropeID which is used
targetSoundSelection.voiceViewerInfo.edited ← TRUE;
[] ← VoiceEditOps.ReplaceSelectionWithSavedInterval[targetSoundSelection, newSound, TRUE];
targetSoundSelection.voiceViewerInfo.editInProgress ← FALSE
}
}
};
NewDictationWindow: INTERNAL PROC = {
creates a new dictation window and sets targetSoundSelection up correctly to refer to it, also sets addingIntoTextViewer false
addingIntoTextViewer ← FALSE;
targetSoundSelection ← NEW [VoiceViewers.SelectionRec];
[viewerInfo: targetSoundSelection.voiceViewerInfo, viewer: targetSoundSelection.viewer] ← VoiceViewers.BuildVoiceViewer[NIL, NIL, TRUE];
BuildVoiceViewer[NIL] will set the voiceRope represented by the rope to NIL, the test used for a currently 'empty' viewer in StopRecording above
cueInsertPosition ← 1
};
---- the dictation machine creation stuff [the previous procedure is a general one for creating window with no voice in it]:
DictationMachine: PUBLIC ENTRY Menus.MenuProc = {
the procedure behind all DictationMachine buttons: if a recording is in progress then transfer it into another [newly created] window, otherwise make a new window and start recording
p: PROCESS;
IF recordingInProgress THEN
{ ChangeVoiceInputFocus[]; -- the non-trivial case - recording in progress
RETURN
};
the rest of this procedure implements the case of DictationMachine bugged when no recording is in progress: create a new viewer and start recording into it
IF VoicePlayBack.PlayBackInProgress[] THEN
{ MessageWindow.Append["Cancel playback before trying to record", TRUE];
MessageWindow.Blink[];
RETURN
}; -- PlayBackInProgress is only a hint: to be sure we'll do a cancel to avoid race conditions
VoicePlayBack.CancelPlayBack[];
NewDictationWindow[];
DictationOps.ToggleDictationMenu[targetSoundSelection.viewer];
recordingInProgress ← TRUE;
timerState ← on;
recordedRope ← NIL;
recordingTimeInSoundChars ← 0;
fork processes (a) to call VoiceRope.Record and (b) to time the recording. (a) is detached but will return when a VoiceRope.Stop occurs. It will then place the ID of the Rope in the appropriate state variable, where the routine called as a result of hitting the stop key can get it
p ← FORK RecordNewRope[];
TRUSTED {Process.Detach[p]};
p ← FORK RecordingTimer[];
TRUSTED {Process.Detach[p]}
};
ChangeVoiceInputFocus: INTERNAL PROC = {
DictationMachine was bugged when a recording was in progress: create a new voice viewer and alter the recording focus to it. In the case where recording going into a voice viewer, leave a marker where the focus was.
IF addingIntoTextViewer
THEN
{ TiogaExtraOps.RemoveTextKey[targetNode, $recordingMark]
**** put a source marker into the text viewer?
}
ELSE
{ IF targetSoundSelection.voiceViewerInfo.ropeInterval.ropeID = NIL THEN
{ MessageWindow.Append["you're already using the dictation machine!", TRUE];
MessageWindow.Blink[];
RETURN
};
VoiceMarkers.LockedAddCharMark[targetSoundSelection.viewer, MAX [targetSoundSelection.ropeInterval.start/VoiceViewers.soundRopeCharLength-1, 0]];
this procedure not only sets the mark but also redraws the viewer, removing the voice input markers
MessageWindow.Append["marker set where you were inserting voice", TRUE];
targetSoundSelection.voiceViewerInfo.editInProgress ← FALSE
};
{ currentContents: Rope.ROPE ← " ";
NewDictationWindow[]; -- that's an empty one: set its contents to reflect the sound already recorded
DictationOps.ToggleDictationMenu[targetSoundSelection.viewer];
FOR i: INT IN [1..recordingTimeInSoundChars] DO currentContents ← currentContents.Concat[">"] ENDLOOP;
currentContents ← currentContents.Concat[" "];
[] ← VoicePlayBack.RedrawViewer[targetSoundSelection.viewer, currentContents, 0, 0, 0, 0, FALSE, deSelected]
}
};
RecordInPlaceOfSelection: PUBLIC ENTRY PROC [selection: VoiceViewers.Selection] = {
this gets called by DictationOps when a selection [possibly zero length, in which case don't delete it] is to be replaced by new voice input: the selection is assumed to be locked with GetVoiceLock and there is assumed to be no playback or recording in progress at the time of the call
p: PROCESS;
targetSoundSelection ← selection;
addingIntoTextViewer ← FALSE;
IF targetSoundSelection.ropeInterval.length # 0 -- i.e. pending delete
THEN IF VoiceEditOps.ReplaceSelectionWithSavedInterval[targetSoundSelection, NIL, FALSE].viewerDeleted
THEN NewDictationWindow[]; -- this means "if applicable, do the pending delete. If that delete should reduce the window contents to zero it will disappear, so create a new one"
cueInsertPosition ← targetSoundSelection.ropeInterval.start/VoiceViewers.soundRopeCharLength;
recordingInProgress ← TRUE;
timerState ← on;
recordedRope ← NIL;
recordingTimeInSoundChars ← 0;
fork processes (a) to call VoiceRope.Record and (b) to time the recording. (a) is detached but will return when a VoiceRope.Stop occurs. It will then place the ID of the Rope in the appropriate state variable, where the routine called as a result of hitting the stop key can get it
p ← FORK RecordNewRope[];
TRUSTED {Process.Detach[p]};
p ← FORK RecordingTimer[];
TRUSTED {Process.Detach[p]}
};
END.