mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-20 22:30:05 +02:00
------------------------------------------------------------------------ r331 | rbd | 2020-10-13 12:40:12 -0500 (Tue, 13 Oct 2020) | 2 lines Also forgot to install NyquistWords.txt ------------------------------------------------------------------------ r330 | rbd | 2020-10-13 12:34:06 -0500 (Tue, 13 Oct 2020) | 2 lines Forgot to move nyquistman.pdf from docsrc/s2h to release ------------------------------------------------------------------------ r329 | rbd | 2020-10-13 11:32:33 -0500 (Tue, 13 Oct 2020) | 2 lines Updated some version numbers for 3.16. ------------------------------------------------------------------------ r328 | rbd | 2020-10-13 11:20:52 -0500 (Tue, 13 Oct 2020) | 2 lines Fixed NyquistIDE antialiasing for plot text, fix format of message. ------------------------------------------------------------------------ r327 | rbd | 2020-10-12 21:01:53 -0500 (Mon, 12 Oct 2020) | 2 lines Fixed a couple of format problems in manual. This version of Nyquist has been tested wtih macOS, Linux, 32&64-bit Windows. ------------------------------------------------------------------------ r326 | rbd | 2020-10-12 20:21:38 -0500 (Mon, 12 Oct 2020) | 1 line Modified WIN32 32-bit XLisp to use 64-bit FIXNUMs. This allows XLisp and Nyquist to handle big sounds even on 32-bit machines. Probably at some cost, but inner loops are mostly float and int32, and the Nyquist release is 64-bit anyway. Maybe we'll have to run some benchmarks on Audacity, which is still 32-bit on Windows. ------------------------------------------------------------------------ r325 | rbd | 2020-10-12 13:16:57 -0500 (Mon, 12 Oct 2020) | 1 line Win64 passes bigfiletest.lsp now. This version should work on all 64-bit systems now. These changes untested on Linux and macOS. ------------------------------------------------------------------------ r324 | rbd | 2020-10-11 21:31:53 -0500 (Sun, 11 Oct 2020) | 2 lines I couldn't free enough space on my linux box, so I adjusted the bigfiletest to write 8-bit ulaw. It's still >4GB and >4G samples. Works on Linux. ------------------------------------------------------------------------ r323 | rbd | 2020-10-11 19:41:25 -0500 (Sun, 11 Oct 2020) | 2 lines Missing file from last commit. ------------------------------------------------------------------------ r322 | rbd | 2020-10-11 19:36:08 -0500 (Sun, 11 Oct 2020) | 1 line Found another case where WIN64 needs int64_t instead of long for sample count. ------------------------------------------------------------------------ r321 | rbd | 2020-10-11 19:33:25 -0500 (Sun, 11 Oct 2020) | 3 lines Fixed s-save to handle optional and keyword parameters (which should never have been mixed in the first place). Documentation cleanup - should be final for this version. ------------------------------------------------------------------------ r320 | rbd | 2020-10-11 14:44:37 -0500 (Sun, 11 Oct 2020) | 2 lines Fixes to handle IRCAM sound format and tests for big file io working on macOS. ------------------------------------------------------------------------ r319 | rbd | 2020-10-10 21:31:58 -0500 (Sat, 10 Oct 2020) | 2 lines Changes for linux and to avoid compiler warnings on linux. ------------------------------------------------------------------------ r318 | rbd | 2020-10-10 20:50:23 -0500 (Sat, 10 Oct 2020) | 1 line This is the test used for Win64 version. ------------------------------------------------------------------------ r317 | rbd | 2020-10-10 20:34:34 -0500 (Sat, 10 Oct 2020) | 1 line This version works on Win64. Need to test changes on macOS and linux. ------------------------------------------------------------------------ r316 | rbd | 2020-10-10 19:59:15 -0500 (Sat, 10 Oct 2020) | 2 lines PWL changes to avoid compiler warning. ------------------------------------------------------------------------ r315 | rbd | 2020-10-10 19:34:04 -0500 (Sat, 10 Oct 2020) | 2 lines A few more changes for 64-bit sample counts on Win64 ------------------------------------------------------------------------ r314 | rbd | 2020-10-10 13:19:42 -0500 (Sat, 10 Oct 2020) | 2 lines Fixed int64_t declaration in gate.alg ------------------------------------------------------------------------ r313 | rbd | 2020-10-10 12:07:40 -0500 (Sat, 10 Oct 2020) | 2 lines Fixes to gate for long sounds ------------------------------------------------------------------------ r312 | rbd | 2020-10-10 11:47:29 -0500 (Sat, 10 Oct 2020) | 2 lines Fixed sound_save types for intgen ------------------------------------------------------------------------ r311 | rbd | 2020-10-10 11:09:01 -0500 (Sat, 10 Oct 2020) | 2 lines Fixed a 64-bit sample count problem in siosc.alg ------------------------------------------------------------------------ r310 | rbd | 2020-10-10 11:03:12 -0500 (Sat, 10 Oct 2020) | 2 lines Fixed sndmax to handle 64-bit sample counts. ------------------------------------------------------------------------ r309 | rbd | 2020-10-10 10:57:04 -0500 (Sat, 10 Oct 2020) | 2 lines Forgot to re-translate all tran/*.alg files with fix for int64 cast to int32. This version compiles on macOS and ready for test on Win64. ------------------------------------------------------------------------ r308 | rbd | 2020-10-10 10:16:05 -0500 (Sat, 10 Oct 2020) | 2 lines Everything seems to compile and run on macOS now. Moving changes to Windows for test. ------------------------------------------------------------------------ r307 | rbd | 2020-10-10 09:23:45 -0500 (Sat, 10 Oct 2020) | 1 line Added casts to avoid compiler warnings and to review changes to support 64-bit sample counts on Windows. Still not complete, and waiting to regenerate and compile tran directory code after updates to translation code that will insert more casts. ------------------------------------------------------------------------ r306 | rbd | 2020-10-09 21:55:15 -0500 (Fri, 09 Oct 2020) | 2 lines Rebuilt seqfnint.c from header files. ------------------------------------------------------------------------ r305 | rbd | 2020-10-09 21:53:33 -0500 (Fri, 09 Oct 2020) | 1 line Changed some FIXNUMS to LONG to avoid compiler warnings in seqfnint.c ------------------------------------------------------------------------ r304 | rbd | 2020-10-09 21:44:03 -0500 (Fri, 09 Oct 2020) | 2 lines I discovered forgotten regression-test.lsp and added test that requires 64-bit sample counts to pass. Fixed a few bugs revealed by running the type-checking regression tests. ------------------------------------------------------------------------ r303 | rbd | 2020-10-09 12:28:58 -0500 (Fri, 09 Oct 2020) | 2 lines Changes for 64-bit sample counts broke mult-channel s-save. Fixed in the commit for macOS. ------------------------------------------------------------------------ r302 | rbd | 2020-10-09 10:03:39 -0500 (Fri, 09 Oct 2020) | 2 lines Changed snd-play to return samples computed and used that to make a test for computing long sounds that would overflow 32-bit length counts. ------------------------------------------------------------------------ r301 | rbd | 2020-10-09 09:11:26 -0500 (Fri, 09 Oct 2020) | 2 lines corrected mistake in delaycv.alg and re-translated ------------------------------------------------------------------------ r300 | rbd | 2020-10-09 09:09:06 -0500 (Fri, 09 Oct 2020) | 2 lines Fix to delaycv.alg -- "s" changed to "input" to avoid matching "s" in "sample_type". ------------------------------------------------------------------------ r299 | rbd | 2020-10-09 09:03:33 -0500 (Fri, 09 Oct 2020) | 4 lines To avoid compiler warnings, XLisp interfaces to C int and long are now specified as LONG rather than FIXNUM, and the stubs that call the C functions cast FIXNUMs from XLisp into longs before calling C functions. ------------------------------------------------------------------------ r298 | rbd | 2020-10-08 22:20:26 -0500 (Thu, 08 Oct 2020) | 2 lines This commit has many more fixes to handle long (64-bit) sounds, including a lot of fixes for warnings by Visual Studio assigning int64_t to long (works on macOS, doesn't work on VS). This was compiled and tested on macOS, and even computed a 27.1-hour sound using OSC, LP, SUM and MULT (haven't tested I/O yet). ------------------------------------------------------------------------ r297 | rbd | 2020-10-07 13:04:02 -0500 (Wed, 07 Oct 2020) | 2 lines This is a major cleanup. It started with the goal of changing long to int64_t for sample counts so that on 64-bit windows, where long is only 32-bits, the sample counts would nevertheless be 64-bit allowing long sounds, which was a limitation for long recordings in Audacity. Since I was using compiler warnings to track possible loss-of-precision conversions from 64-bit sample counts, and there were *many* warnings, I started cleaning up *all* the warnings and ended up with a very large set of changes, including "modernizing" C declarations that date back to XLisp and CMU MIDI Toolkit code and were never changed. This version runs all the examples.sal code on macOS, but will surely have problems on Windows and Linux given the number of changes. ------------------------------------------------------------------------ r296 | rbd | 2020-10-06 13:34:20 -0500 (Tue, 06 Oct 2020) | 2 lines More changes from long to int64_t for sample counts. ------------------------------------------------------------------------ r295 | rbd | 2020-10-06 11:53:49 -0500 (Tue, 06 Oct 2020) | 2 lines More work on using 64-bit sample counts. Changed MAX_STOP from 32-bit to 64-bit limit. ------------------------------------------------------------------------ r294 | rbd | 2020-10-06 11:48:05 -0500 (Tue, 06 Oct 2020) | 2 lines Made some changes so that sample counts are int64_t (for windows) instead of long to support sample counts above 31 bits. ------------------------------------------------------------------------ r293 | rbd | 2020-10-04 21:30:55 -0500 (Sun, 04 Oct 2020) | 2 lines Fixed a few minor things for Linux and tested on Linux. ------------------------------------------------------------------------ r292 | rbd | 2020-10-04 21:00:28 -0500 (Sun, 04 Oct 2020) | 2 lines Update extensions: all are minor changes. ------------------------------------------------------------------------ r291 | rbd | 2020-09-24 13:59:31 -0500 (Thu, 24 Sep 2020) | 2 lines New implementation of seq and seqrep, added get-real-time, documented get-real-time, fixed examples.sal and examples.lsp which are now in lib rather than extensions (so they are now back in the basic installation), other cleanup. ------------------------------------------------------------------------ r290 | rbd | 2020-08-16 16:24:52 -0500 (Sun, 16 Aug 2020) | 2 lines Fixed bug in snd-gate, revised GATE and NOISE-GATE to handle multi-channel sound. RMS now handles multi-channel input. S-AVG added to take multichannel input (but not used, because RMS could not be written without making SND-SRATE convert multichannel sound to vector of floats. That seems to be going toward a fully vectorized model. Not going there for now. ------------------------------------------------------------------------ r289 | rbd | 2020-07-09 16:27:45 -0500 (Thu, 09 Jul 2020) | 2 lines Added GET-REAL-TIME function to XLISP. May not work yet on Windows. Various fixes for compiler warnings. I noticed FLAC doesn't work (I guess it never did) and I cannot figure out how this even links because flac_min seems to be undefined. Something to look at later.
636 lines
22 KiB
C
636 lines
22 KiB
C
/* record.c -- keyboard to adagio recorder
|
||
* Copyright 1989 Carnegie Mellon University
|
||
*
|
||
* the interface consists of three routines:
|
||
* rec_init() -- initialization
|
||
* rec_event(byte *data) -- called to insert (record) midi data,
|
||
* -- returns FALSE if no more space
|
||
* rec_final() -- called to finish up
|
||
*/
|
||
|
||
/*****************************************************************************
|
||
* Change Log
|
||
* Date | Change
|
||
*-----------+-----------------------------------------------------------------
|
||
* 27-Feb-86 | Created changelog
|
||
* | Use pedal information when computing durations (code taken
|
||
* | from transcribe.c)
|
||
* 23-Mar-86 | Determine size of transcription when rec_init is called.
|
||
* 21-May-86 | Major rewrite to use continuous controls (code taken
|
||
* | from transcribe.c)
|
||
* 1-Aug-87 | F.H. Changed rec_init() to new memory handling.
|
||
* 17-Oct-88 | JCD : portable version.
|
||
* 31-Jan-90 | GWL : cleaned up for LATTICE
|
||
* 30-Jun-90 | RBD : further changes
|
||
* 2-Apr-91 | JDW : further changes
|
||
* 28-Apr-03 | DM : changed for portability; true->TRUE, false->FALSE
|
||
*****************************************************************************/
|
||
|
||
#include "switches.h"
|
||
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
#include "cext.h"
|
||
#include "midifns.h"
|
||
#include "userio.h"
|
||
#include "midicode.h"
|
||
#include "record.h"
|
||
#include "cmdline.h"
|
||
|
||
extern long space; /* how much space is left? */
|
||
|
||
int debug_rec = FALSE; /* verbose debug flag for this module */
|
||
|
||
long max_notes = -1L; /* -1 is flag that space must be allocated */
|
||
|
||
time_type previous_time;
|
||
|
||
/****************************************************************
|
||
* data structure notes: the midi stream is stored as an array
|
||
* of 4-byte records, each of which is either a time or midi
|
||
* data. Midi data always begins with a control byte (high
|
||
* order bit set), and it is assumed times are positive (high
|
||
* order bit clear), so the two are easy to distinguish
|
||
* IF THE COMPILER PUTS THESE BITS IN THE SAME PLACE. It looks
|
||
* like the high order byte of the time lines up with the last
|
||
* byte of a 4 byte array, so we will always set the high order
|
||
* bit of the last array byte when the first 3 bytes are filled
|
||
* with MIDI data. This is refered to as the "tag" bit.
|
||
* WARNING: Lattice C longs are UNSIGNED, therefore always
|
||
* positive. Test the high order bit with a mask.
|
||
****************************************************************/
|
||
|
||
#define MIDI_CMD_BIT 0x80
|
||
#define HIGH_BIT 0x80000000
|
||
#define istime(note) (!(((note)->when) & HIGH_BIT))
|
||
|
||
typedef union note_struct {
|
||
byte n[4];
|
||
long when;
|
||
}
|
||
*note_type, note_node;
|
||
|
||
private note_type event_buff; /* pointer to allocated buffer */
|
||
private note_type next; /* pointer to next entry in buffer */
|
||
private note_type last; /* pointer to last entry in buffer */
|
||
private int pile_ups; /* inner loop iteration count */
|
||
private int max_pile_up; /* maximum of pile_ups */
|
||
private boolean fixed_octave; /* used to avoid many error messages */
|
||
|
||
/****************************************************************************
|
||
* Routines local to this module
|
||
****************************************************************************/
|
||
private void bend_filter(note_type note, note_type last, long now);
|
||
private void byteorder(void);
|
||
private void ctrl_filter(note_type note, note_type last, long now);
|
||
private int event_bend(note_type note);
|
||
private void filter(note_type last);
|
||
private long getdur(int i, note_type last, int ped, long now);
|
||
private long getnext(int i, note_type last, long now);
|
||
private char map_ctrl(int control);
|
||
private void output(FILE *fp, note_type last, boolean absflag);
|
||
|
||
/****************************************************************************
|
||
* bend_filter
|
||
* Inputs:
|
||
* note_type note: the current note
|
||
* note_type last: the last recorded event
|
||
* long now: the current time
|
||
* Effect:
|
||
* remove pitch bend events in same 0.01 sec time slot
|
||
* Implementation:
|
||
* If the current event is a pitch bend that bends again
|
||
* in the same time slot, make it a no-op by replacing it with
|
||
* the time.
|
||
****************************************************************************/
|
||
|
||
private void bend_filter(note_type note, note_type last, long now)
|
||
{
|
||
/* first see if there is another bend in this time
|
||
* slot.
|
||
*/
|
||
note_type note2 = note + 1;
|
||
while (note2 < last) {
|
||
if (istime(note2) && (note2->when > now)) {
|
||
break; /* new time slot */
|
||
}
|
||
else if (note->n[0] == note2->n[0]) {
|
||
note->when = now;
|
||
return; /* found another bend */
|
||
}
|
||
note2++;
|
||
}
|
||
}
|
||
|
||
/****************************************************************************
|
||
* byteorder
|
||
* Effect:
|
||
* check out assumptions about byte order and placement
|
||
****************************************************************************/
|
||
|
||
private void byteorder(void)
|
||
{
|
||
note_node test_event;
|
||
if ((sizeof(test_event) != 4) ||
|
||
(sizeof(test_event.when) != 4) ||
|
||
(sizeof(test_event.n[0]) != 1)) {
|
||
gprintf(ERROR, "implementation error: size problem\n");
|
||
EXIT(1);
|
||
}
|
||
test_event.n[0] = 0x12;
|
||
test_event.n[1] = 0x34;
|
||
test_event.n[2] = 0x56;
|
||
test_event.n[3] = 0x78;
|
||
if ((test_event.when != 0x78563412) &&
|
||
(test_event.when != 0x12345678)) {
|
||
gprintf(ERROR, "implementation error: layout problem\n");
|
||
EXIT(1);
|
||
}
|
||
}
|
||
|
||
/****************************************************************************
|
||
* ctrl_filter
|
||
* Inputs:
|
||
* note_type note: the current note
|
||
* note_type last: the last recorded event
|
||
* long now: the current time
|
||
* Effect:
|
||
* remove ctrl change events in same 0.01 sec time slot
|
||
* Implementation:
|
||
* If the current event is a control change that changes again
|
||
* in the same time slot, make it a no-op by replacing it with
|
||
* the time.
|
||
****************************************************************************/
|
||
|
||
private void ctrl_filter(note_type note, note_type last, long now)
|
||
{
|
||
/* see if there is another control change in this time
|
||
* slot.
|
||
*/
|
||
note_type note2 = note+1;
|
||
while (note2 < last) {
|
||
if (istime(note2) && (note2->when > now)) {
|
||
break; /* new time slot */
|
||
}
|
||
else if ((note->n[0] == note2->n[0]) &&
|
||
(note->n[1] == note2->n[1])) {
|
||
note->when = now;
|
||
return; /* found another change */
|
||
}
|
||
note2++;
|
||
}
|
||
}
|
||
|
||
/****************************************************************************
|
||
* event_bend
|
||
* Inputs:
|
||
* note_type note: pointer to a pitch bend event
|
||
* Outputs:
|
||
* returns int: an 8 bit pitch bend number
|
||
****************************************************************************/
|
||
|
||
private int event_bend(note_type note)
|
||
{
|
||
return((int) (((note->n[1]) >> 6) + ((note->n[2]) << 1)));
|
||
}
|
||
|
||
/****************************************************************************
|
||
* filter
|
||
* Inputs:
|
||
* note_type last: the last note recorded
|
||
* Effect: allow only one control change per time slot (0.01 sec)
|
||
* Implementation:
|
||
* call ctrl_filter and bend_filter to overwrite control changes with
|
||
* noop data (the current time is used as a noop)
|
||
****************************************************************************/
|
||
|
||
private void filter(note_type last)
|
||
{
|
||
note_type note; /* loop control variable */
|
||
long now=0; /* last time seen */
|
||
int command; /* command pointed to by note */
|
||
|
||
for (note = event_buff; note <= last; note++) {
|
||
if (istime(note)) {
|
||
now = note->when;
|
||
}
|
||
else {
|
||
command = note->n[0] & MIDI_CODE_MASK;
|
||
|
||
if (command == MIDI_CTRL &&
|
||
note->n[1] == SUSTAIN) {
|
||
/* do nothing */;
|
||
}
|
||
else if (command == MIDI_CTRL) {
|
||
ctrl_filter(note, last, now);
|
||
}
|
||
else if (command == MIDI_TOUCH) {
|
||
bend_filter(note, last, now); /* bend and touch use the */
|
||
}
|
||
else if (command == MIDI_BEND) { /* same filter routines */
|
||
bend_filter(note, last, now);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/****************************************************************************
|
||
* getdur
|
||
* Inputs:
|
||
* int i: index of the note
|
||
* note_type last: pointer to the last event recorded
|
||
* int ped: TRUE if pedal is down at event i
|
||
* long now: the time at event i
|
||
* Outputs:
|
||
* returns long: the duration of note i
|
||
* Assumes:
|
||
* assumes i is a note
|
||
* Implementation:
|
||
* This is tricky because of pedal messages. The note is kept on by
|
||
* either the key or the pedal. Keep 2 flags, key and ped. Key is
|
||
* turned off when a key is released, ped goes off and on with pedal.
|
||
* Note ends when (1) both key and ped are FALSE, (2) key is
|
||
* pressed (this event will also start another note).
|
||
****************************************************************************/
|
||
|
||
private long getdur(int i, note_type last, int ped, long now)
|
||
{
|
||
int key = TRUE; /* flag that says if note is on */
|
||
long start = now;
|
||
int chan = event_buff[i].n[0] & MIDI_CHN_MASK;
|
||
int pitch = event_buff[i].n[1];
|
||
note_type note = &(event_buff[i+1]);
|
||
int noteon; /* TRUE if a noteon message received on chan */
|
||
int keyon; /* TRUE if noteon message had non-zero velocity */
|
||
|
||
/* search from the next event (i+1) to the end of the buffer:
|
||
*/
|
||
for (; note < last; note++) {
|
||
if (istime(note)) {
|
||
now = note->when;
|
||
}
|
||
else {
|
||
noteon = keyon = FALSE;
|
||
if ((note->n[0] & MIDI_CHN_MASK) == chan) {
|
||
noteon = ((note->n[0] & MIDI_CODE_MASK) == MIDI_ON_NOTE) &&
|
||
(note->n[1] == pitch);
|
||
keyon = noteon && (note->n[2] != 0);
|
||
if ((noteon && (note->n[2] == 0)) ||
|
||
(((note->n[0] & MIDI_CODE_MASK) == MIDI_OFF_NOTE) &&
|
||
(note->n[1] == pitch))) key = FALSE;
|
||
if (((note->n[0] & MIDI_CODE_MASK) == MIDI_CTRL) &&
|
||
note->n[1] == SUSTAIN && note->n[2] == 127) ped = TRUE;
|
||
if (((note->n[0] & MIDI_CODE_MASK) == MIDI_CTRL) &&
|
||
note->n[1] == SUSTAIN && note->n[2] == 0) ped = FALSE;
|
||
|
||
if ((!key && !ped) || keyon)
|
||
return(now - start);
|
||
}
|
||
}
|
||
}
|
||
return(last->when - start);
|
||
}
|
||
|
||
/****************************************************************************
|
||
* getnext
|
||
* Inputs:
|
||
* int i: the index of the current note
|
||
* note_type last: pointer to last valid data
|
||
* long now: the current time
|
||
* Outputs:
|
||
* returns long: the time of the next note, program, or control change
|
||
* (returns time of last event if nothing else is found)
|
||
****************************************************************************/
|
||
|
||
private long getnext(int i, note_type last, long now)
|
||
{
|
||
i++; /* advance to next item */
|
||
for (; event_buff + i < last; i++) {
|
||
note_type note = &(event_buff[i]);
|
||
int cmd = note->n[0] & MIDI_CODE_MASK;
|
||
|
||
if (istime(note)) {
|
||
now = note->when;
|
||
}
|
||
else if (((cmd == MIDI_ON_NOTE) &&
|
||
(note->n[2] != 0)) /* note on */ ||
|
||
(cmd == MIDI_CH_PROGRAM) /* program change */ ||
|
||
((cmd == MIDI_CTRL) &&
|
||
(note->n[1] != SUSTAIN) /* control change */ ) ||
|
||
(cmd == MIDI_TOUCH) ||
|
||
(cmd == MIDI_BEND)) {
|
||
return(now);
|
||
}
|
||
}
|
||
return(last->when);
|
||
}
|
||
|
||
/****************************************************************************
|
||
* map_ctrl
|
||
* Inputs:
|
||
* int control: a midi control number
|
||
* Outputs:
|
||
* returns char: an adagio control change command letter, EOS if
|
||
* control change is not one of PORTARATE, PORTASWITCH,
|
||
* MODWHEEL, FOOT
|
||
****************************************************************************/
|
||
|
||
private char map_ctrl(int control)
|
||
{
|
||
switch (control) {
|
||
/* 'J' is no longer code for PORTARATE
|
||
case PORTARATE:
|
||
return 'J'; */
|
||
case PORTASWITCH:
|
||
return 'K';
|
||
case MODWHEEL:
|
||
return 'M';
|
||
case VOLUME:
|
||
return 'X';
|
||
default:
|
||
return EOS;
|
||
}
|
||
#ifdef LATTICE322
|
||
return EOS; /* make Lattice C type checker happy */
|
||
#endif
|
||
}
|
||
|
||
/****************************************************************************
|
||
* output
|
||
* Inputs:
|
||
* FILE *fp: an opened file pointer
|
||
* note_type last: the last data in the buffer
|
||
* boolean absflag: set to TRUE if first line of the adagio score should
|
||
* include the absolute time
|
||
* Effect:
|
||
* write adagio file using data in event_buff
|
||
* Implementation:
|
||
* NOTE: put all program changes in rests
|
||
* use N(ext) notation for all timing
|
||
* output no more than one continuous parameter change per
|
||
* clock tick for each continuous change parameter
|
||
****************************************************************************/
|
||
|
||
private void output(FILE *fp, note_type last, boolean absflag)
|
||
{
|
||
int i; /* loop counter */
|
||
int command; /* the current command */
|
||
int voice; /* the midi channel of the current event */
|
||
int last_velocity = -1; /* used to filter repeated Lnn attributes */
|
||
int last_voice = 0; /* the default adagio channel (1) */
|
||
int ped = FALSE; /* flag maintains state of pedal */
|
||
/* size of event_buff is seems to be about 20000 events */
|
||
int how_many = (int) (last - event_buff);
|
||
long now=0; /* the time of the next event */
|
||
|
||
if (fp == NULL) {
|
||
gprintf(ERROR, "internal error: output called with NULL file.\n");
|
||
EXIT(1);
|
||
}
|
||
|
||
if (debug_rec)
|
||
gprintf(GDEBUG,"hint: if file is not being closed, decrease MAXSPACE\n");
|
||
|
||
|
||
fprintf(fp, "!MSEC\n"); /* times will be in milliseconds */
|
||
/* set the initial absolute time, all other times are relative */
|
||
|
||
if (absflag) {
|
||
now = event_buff[0].when;
|
||
if (now < 0) {
|
||
fprintf(fp, "* First event took place at Adagio time %d,\n",
|
||
(int)now);
|
||
fprintf(fp, "* but Adagio cannot represent negative times,\n");
|
||
fprintf(fp, "* so this entire score will be %d ms late\n",
|
||
(int)-now);
|
||
gprintf(TRANS, "First event took place at Adagio time %d!\n",
|
||
(int)now);
|
||
gprintf(TRANS, "All events times will be %d ms late\n",
|
||
(int)-now);
|
||
now = 0L;
|
||
}
|
||
fprintf(fp, "T%ld ", now);
|
||
}
|
||
|
||
for (i = 0; i < how_many; i++) {
|
||
if (debug_rec) {
|
||
gprintf(GDEBUG,"ev %d: %x %x %x (%ld)\n", i, event_buff[i].n[0],
|
||
event_buff[i].n[1], event_buff[i].n[2], event_buff[i].when);
|
||
}
|
||
|
||
if (istime(event_buff+i)) {
|
||
now = event_buff[i].when;
|
||
if (debug_rec) gprintf(GDEBUG,"i = %d, now = %ld\n", i, now);
|
||
} else {
|
||
boolean needs_voice = TRUE;
|
||
command = event_buff[i].n[0] & MIDI_CODE_MASK;
|
||
voice = event_buff[i].n[0] & MIDI_CHN_MASK;
|
||
|
||
if (command == MIDI_ON_NOTE && event_buff[i].n[2] != 0) {
|
||
int velocity = event_buff[i].n[2];
|
||
write_pitch(fp, event_buff[i].n[1]);
|
||
fprintf(fp, " U%ld", getdur(i, last, ped, now));
|
||
if (last_velocity != velocity) {
|
||
fprintf(fp, " L%d", velocity);
|
||
last_velocity = velocity;
|
||
}
|
||
} else if (command == MIDI_CH_PROGRAM) {
|
||
fprintf(fp, "Z%d", event_buff[i].n[1] + 1);
|
||
} else if (command == MIDI_CTRL &&
|
||
event_buff[i].n[1] == SUSTAIN) {
|
||
ped = (event_buff[i].n[2] != 0);
|
||
needs_voice = FALSE;
|
||
} else if (command == MIDI_CTRL) {
|
||
char c = map_ctrl(event_buff[i].n[1]);
|
||
if (c != EOS) fprintf(fp, "%c%d", c, event_buff[i].n[2]);
|
||
else fprintf(fp, "~%d(%d)", event_buff[i].n[1], event_buff[i].n[2]);
|
||
} else if (command == MIDI_TOUCH) {
|
||
fprintf(fp, "O%d", event_buff[i].n[1]);
|
||
} else if (command == MIDI_BEND) {
|
||
fprintf(fp, "Y%d", event_bend(&event_buff[i]));
|
||
} else if (command == MIDI_ON_NOTE || command == MIDI_OFF_NOTE) {
|
||
needs_voice = FALSE; /* ignore note-offs */
|
||
} else {
|
||
gprintf(ERROR, "Command 0x%x ignored\n", command);
|
||
needs_voice = FALSE;
|
||
}
|
||
if (needs_voice) {
|
||
if (last_voice != voice) {
|
||
fprintf(fp, " V%d", voice + 1);
|
||
last_voice = voice;
|
||
}
|
||
fprintf(fp, " N%d", (int)(getnext(i, last, now) - now));
|
||
fprintf(fp, "\n");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/****************************************************************************
|
||
* write_pitch
|
||
* Inputs:
|
||
* FILE *fp: an open file
|
||
* int p: a pitch number
|
||
* Effect: write out the pitch name for a given number
|
||
****************************************************************************/
|
||
|
||
void write_pitch(FILE *fp, int p)
|
||
{
|
||
static char *ptos[] = {
|
||
"C", "CS", "D", "EF", "E", "F", "FS", "G",
|
||
"GS", "A", "BF", "B" };
|
||
/* avoid negative numbers: adagio can't express lowest octave: */
|
||
while (p < 12) {
|
||
if (!fixed_octave) {
|
||
gprintf(ERROR, "%s%s%s",
|
||
"A low note was transposed up an octave\n",
|
||
"(Adagio cannot express the lowest MIDI octave).\n",
|
||
"This message will appear only once.\n");
|
||
fixed_octave = TRUE;
|
||
}
|
||
p += 12;
|
||
}
|
||
fprintf(fp, "%s%d", ptos[p % 12], (p / 12) - 1);
|
||
}
|
||
|
||
/**********************************************************************
|
||
* rec_final
|
||
* Inputs:
|
||
* boolean absflag: output absolute time of first note if TRUE
|
||
* Effect:
|
||
* Write recorded data to a file
|
||
**********************************************************************/
|
||
|
||
void rec_final(FILE *fp, boolean absflag)
|
||
{
|
||
next->when = gettime();
|
||
last = next;
|
||
if (debug_rec) gprintf(GDEBUG,"max_pile_up = %d, ", max_pile_up);
|
||
gprintf(TRANS,"%ld times and events recorded.\n",
|
||
(long) (last - event_buff));
|
||
filter(last);
|
||
output(fp, last, absflag);
|
||
fclose(fp);
|
||
FREE(event_buff);
|
||
max_notes = -1;
|
||
}
|
||
|
||
/****************************************************************************
|
||
* rec_init
|
||
* Inputs:
|
||
* char *file: pointer to file name from command line (if any)
|
||
* boolean bender: TRUE if pitch bend should be enabled
|
||
* Outputs:
|
||
* return TRUE if initialization succeeds
|
||
* Effect:
|
||
* prepares module to record midi input
|
||
****************************************************************************/
|
||
|
||
/* ENOUGH_ROOM says if we have room for 10000 events + 10000 timestamps =
|
||
* 20000 note_struct's, then that's "enough room" for recording a sequence.
|
||
* If more ram is available, it won't be used. If less is available, we'll
|
||
* use as much as we can get, minus "SPACE_FOR_PLAY", which leaves a little
|
||
* bit of spare ram in case Moxc or stdio need to allocate some space.
|
||
* For DOS, we limit recording space to 64K.
|
||
*/
|
||
#ifdef DOS
|
||
#define ENOUGH_ROOM 64000L
|
||
#else
|
||
#define ENOUGH_ROOM (20000L * sizeof(union note_struct))
|
||
#endif
|
||
|
||
|
||
boolean rec_init(boolean bender)
|
||
{
|
||
size_t biggestChunk, spaceForRecord;
|
||
|
||
debug_rec = cl_switch("debug");
|
||
byteorder();
|
||
pile_ups = 0;
|
||
max_pile_up = 0;
|
||
previous_time = (unsigned) -1L; /* this will force putting in initial timestamp */
|
||
fixed_octave = FALSE;
|
||
|
||
if (max_notes == -1) { /* allocate space 1st time rec_init called */
|
||
biggestChunk = AVAILMEM;
|
||
if (biggestChunk <= SPACE_FOR_PLAY) {
|
||
/* not enough memory; give up */
|
||
return(FALSE);
|
||
}
|
||
else {
|
||
spaceForRecord =
|
||
MIN((biggestChunk - SPACE_FOR_PLAY), ENOUGH_ROOM);
|
||
/* leave SPACE_FOR_PLAY contiguous bytes of memory */
|
||
}
|
||
max_notes = (long) (spaceForRecord / sizeof(note_node));
|
||
/* gprintf(GDEBUG,"max_notes = %d\n", max_notes);*/
|
||
event_buff = (note_type) MALLOC(spaceForRecord);
|
||
if (event_buff == NULL) {
|
||
/* should never happen */
|
||
gprintf(FATAL, "Implementation error (record.c): getting memory.");
|
||
return FALSE;
|
||
}
|
||
}
|
||
next = event_buff;
|
||
last = event_buff + max_notes - 2; /* it is critical that last not point
|
||
* to the very last storage loc */
|
||
midi_cont(bender);
|
||
return((boolean)(max_notes > 10));
|
||
/* it would be silly to record with only room enough for 10 notes! */
|
||
}
|
||
|
||
#ifdef NEED_REC_EVENT
|
||
/*
|
||
This function was part of the CMU MIDI Toolkit. It provided a constant time
|
||
fast way to record midi data into a buffer. After recording, the buffered
|
||
data could be transferred into an Adagio score structure, which involved
|
||
linked list allocation and insertion that might have caused performance
|
||
problems. This code uses the high-order bit of longs to distinguish timestamps
|
||
from data (MIDI messages), but the code seems to assume little endian, and
|
||
I'm not sure how it worked on both Intel and 68000 processors. Rather than
|
||
look more closely or fix it, I'm commenting it out because Nyquist does not
|
||
have any MIDI I/O capability and does not need the function. It is here for
|
||
completeness, since this is probably the only archived version of the CMU
|
||
MIDI Toolkit. */
|
||
|
||
/****************************************************************************
|
||
* rec_event
|
||
* Inputs:
|
||
* long time: the current time
|
||
* long data: midi data to record
|
||
* Outputs:
|
||
* returns FALSE if there is no more memory
|
||
* Effect: reads and stores any input
|
||
* Implementation:
|
||
* time stamps and midi events share the same buffer of 4-byte events
|
||
* save time at most once per call to rec_poll
|
||
* save time only if it changes
|
||
****************************************************************************/
|
||
|
||
boolean rec_event(long *data, time_type time)
|
||
{
|
||
/* can't allow negative time because sign bit distinguishes
|
||
* data from time: */
|
||
if (time < 0) time = 0;
|
||
|
||
if (previous_time != time) {
|
||
next++->when = previous_time = time;
|
||
if (next >= last) goto overflow;
|
||
}
|
||
|
||
next->when = *data;
|
||
next++->n[3] = MIDI_CMD_BIT; /* set tag bit */
|
||
if (next >= last) goto overflow;
|
||
return TRUE;
|
||
|
||
overflow:
|
||
next = last; /* last doesn't really point to last storage */
|
||
gprintf(ERROR, "No more memory.\n");
|
||
return FALSE;
|
||
}
|
||
|
||
#endif
|