1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-20 14:20:06 +02:00
Leland Lucius 15b9bb96cd Update nyquist to SVN r331 (r3.16+)
------------------------------------------------------------------------
   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.
2021-01-27 23:45:25 -06:00

299 lines
12 KiB
C

/* multiread.c -- read multichannel sound file */
/* CHANGE LOG
* --------------------------------------------------------------------
* 28Apr03 dm changes for portability and fix compiler warnings
*/
#include "stdio.h"
#ifdef UNIX
#include "sys/file.h"
#endif
#ifndef mips
#include "stdlib.h"
#endif
#include "sndfmt.h"
#include "xlisp.h"
#include "sound.h"
#include "falloc.h"
#include "sndfile.h"
#include "sndread.h"
#include "multiread.h"
/* allocate input buffer space for this many bytes/frame,
* e.g. 8 allows 2 channels
* If frames are bigger, then multiple reads will be issued.
*/
#define max_bytes_per_frame (sizeof(float) * 2)
#define input_buffer_max (max_sample_block_len * max_bytes_per_frame)
#define input_buffer_samps (max_sample_block_len * 2)
/* multiread_fetch - read samples into multiple channels. */
/*
* The susp is shared by all channels. The susp has backpointers
* to the tail-most snd_list node of each channels, and it is by
* extending the list at these nodes that sounds are read in.
* To avoid a circularity, the reference counts on snd_list nodes
* do not include the backpointers from this susp. When a snd_list
* node refcount goes to zero, the multiread susp's free routine
* is called. This must scan the backpointers to find the node that
* has a zero refcount (the free routine is called before the node
* is deallocated, so this is safe). The backpointer is then set
* to NULL. When all backpointers are NULL, the susp itself is
* deallocated, because it can only be referenced through the
* snd_list nodes to which there are backpointers.
*/
void multiread_fetch(snd_susp_type a_susp, snd_list_type snd_list)
{
read_susp_type susp = (read_susp_type) a_susp;
int i, j;
int frames_read = 0; /* total frames read in this call to fetch */
int n;
sample_block_type out;
// char input_buffer[input_buffer_max];
float input_buffer[input_buffer_samps];
int file_frame_size;
/* when we are called, the caller (SND_get_first) will insert a new
* snd_list node. We need to do this here for all other channels.
*/
for (j = 0; j < susp->sf_info.channels; j++) {
/* nyquist_printf("multiread_fetch: chan[%d] = ", j);
print_snd_list_type(susp->chan[j]);
stdputstr("\n");
*/
if (!susp->chan[j]) { /* ignore non-existent channels */
/* nyquist_printf("multiread_fetch: ignore channel %d\n", j);*/
continue;
}
falloc_sample_block(out, "multiread_fetch");
/* nyquist_printf("multiread: allocated block %x\n", out); */
/* Since susp->chan[i] exists, we want to append a block of samples.
* The block, out, has been allocated. Before we insert the block,
* we must figure out whether to insert a new snd_list_type node for
* the block. Recall that before SND_get_next is called, the last
* snd_list_type in the list will have a null block pointer, and the
* snd_list_type's susp field points to the suspension (in this case,
* susp). When SND_get_next (in sound.c) is called, it appends a new
* snd_list_type and points the previous one to internal_zero_block
* before calling this fetch routine. On the other hand, since
* SND_get_next is only going to be called on one of the channels, the
* other channels will not have had a snd_list_type appended.
* SND_get_next does not tell us directly which channel it wants (it
* doesn't know), but we can test by looking for a non-null block in the
* snd_list_type pointed to by our back-pointers in susp->chan[]. If
* the block is null, the channel was untouched by SND_get_next, and
* we should append a snd_list_type. If it is non-null, then it
* points to internal_zero_block (the block inserted by SND_get_next)
* and a new snd_list_type has already been appended.
*/
/* Before proceeding, it may be that garbage collection ran when we
* allocated out, so check again to see if susp->chan[j] is Null:
*/
if (!susp->chan[j]) {
ffree_sample_block(out, "multiread_fetch");
continue;
}
if (!susp->chan[j]->block) {
snd_list_type snd_list = snd_list_create((snd_susp_type) susp);
/* Now we have a snd_list to append to the channel, but a very
* interesting thing can happen here. snd_list_create, which
* we just called, MAY have invoked the garbage collector, and
* the GC MAY have freed all references to this channel, in which
* case multread_free(susp) will have been called, and susp->chan[j]
* will now be NULL!
*/
if (!susp->chan[j]) {
nyquist_printf("susp %p Channel %d disappeared!\n", susp, j);
ffree_snd_list(snd_list, "multiread_fetch");
} else {
susp->chan[j]->u.next = snd_list;
}
}
/* see the note above: we don't know if susp->chan still exists */
/* Note: We DO know that susp still exists because even if we lost
* some channels in a GC, someone is still calling SND_get_next on
* some channel. I suppose that there might be some very pathological
* code that could free a global reference to a sound that is in the
* midst of being computed, perhaps by doing something bizarre in the
* closure that snd_seq activates at the logical stop time of its first
* sound, but I haven't thought that one through.
*/
if (susp->chan[j]) {
susp->chan[j]->block = out;
/* check some assertions */
if (susp->chan[j]->u.next->u.susp != (snd_susp_type) susp) {
nyquist_printf("didn't find susp at end of list for chan %d\n", j);
}
} else { /* we allocated out, but don't need it anymore due to GC */
ffree_sample_block(out, "multiread_fetch");
}
}
file_frame_size = susp->sf_info.channels;
/* now fill sample blocks with frames from the file
until eof or end of blocks */
while (true) {
/* compute how many frames to read to fill sample blocks */
int frame_count = max_sample_block_len - frames_read;
int actual; /* how many frames actually read */
/* make sure frames will fit in buffer */
if (frame_count * file_frame_size > input_buffer_samps) {
frame_count = input_buffer_samps / file_frame_size;
}
actual = (int) sf_readf_float(susp->sndfile, input_buffer, frame_count);
n = actual;
/* don't read too many */
if (n > (susp->cnt - susp->susp.current)) {
n = (int) (susp->cnt - susp->susp.current);
}
/* process one channel at a time, multiple passes through input */
for (j = 0; j < susp->sf_info.channels; j++) {
register sample_block_values_type out_ptr;
/* offset by channel number: */
float *float_ptr = input_buffer + j;
/* ignore nonexistent channels */
if (!susp->chan[j]) continue;
/* find pointer to sample buffer */
out_ptr = susp->chan[j]->block->samples + frames_read;
/* copy samples */
for (i = 0; i < n; i++) {
*out_ptr++ = *float_ptr;
float_ptr += susp->sf_info.channels;
}
susp->chan[j]->block_len = frames_read + n;
}
/* jlh BECAUSE, at this point, all the code cares about is
that n frames have been read and the samples put into their
appropriate snd_node buffers. */
frames_read += n;
susp->susp.current += n;
if (frames_read == 0) {
/* NOTE: this code should probably be removed -- how could we
ever get here? Since file formats know the sample count, we'll
always read frames. When we hit the end-of-file, the else
clause below will run and terminate the sound, so we'll never
try and read samples that are not there. The only exception is
an empty sound file with no samples, in which case we could omit
this if test and execute the else part below.
This code *might* be good for formats that do not encode a
sample count and where reading the end of file is the only way
to detect the end of the data.
Since it seeems to work, I'm going to leave this in place.
One tricky point of the algorithm: when we get here, we set up
susp->chan[j] to point to the right place and then call
snd_list_terminate(). This deletes the snd_list that chan[j]
is pointing to, but not before calling multiread_free(), which
upon detecting that the sound is being freed, sets chan[j] to
NULL. This works sequentially on each channel and than last
time, this susp is freed because no channels are active.
*/
/* we didn't read anything, but can't return length zero, so
* convert snd_list's to pointer to zero block. This loop
* will free the susp via snd_list_unref().
*/
for (j = 0; j < susp->sf_info.channels; j++) {
if (susp->chan[j]) {
snd_list_type the_snd_list = susp->chan[j];
/* this is done so that multiread_free works right: */
susp->chan[j] = susp->chan[j]->u.next;
/* nyquist_printf("end of file, terminating channel %d\n", j); */
/* this fixes up the tail of channel j */
snd_list_terminate(the_snd_list);
}
}
return;
} else if (susp->cnt == susp->susp.current || actual < frame_count) {
/* we've read the requested number of frames or we
* reached end of file
* last iteration will close file and free susp:
*/
for (j = 0; j < susp->sf_info.channels; j++) {
snd_list_type the_snd_list = susp->chan[j];
/* nyquist_printf("reached susp->cnt, terminating chan %d\n", j); */
if (the_snd_list) {
/* assert: */
if (the_snd_list->u.next->u.susp != (snd_susp_type) susp) {
stdputstr("assertion violation");
}
/* this is done so that multiread_free works right: */
susp->chan[j] = the_snd_list->u.next;
snd_list_unref(the_snd_list->u.next);
/* terminate by pointing to zero block */
the_snd_list->u.next = zero_snd_list;
}
}
return;
} else if (frames_read >= max_sample_block_len) {
/* move pointer to next list node */
for (j = 0; j < susp->sf_info.channels; j++) {
if (susp->chan[j]) susp->chan[j] = susp->chan[j]->u.next;
}
return;
}
}
} /* multiread__fetch */
void multiread_free(snd_susp_type a_susp)
{
read_susp_type susp = (read_susp_type) a_susp;
int j;
boolean active = false;
/* stdputstr("multiread_free: "); */
for (j = 0; j < susp->sf_info.channels; j++) {
if (susp->chan[j]) {
if (susp->chan[j]->refcnt) active = true;
else {
susp->chan[j] = NULL;
/* nyquist_printf("deactivating channel %d\n", j); */
}
}
}
if (!active) {
/* stdputstr("all channels freed, freeing susp now\n"); */
read_free(a_susp);
}
}
LVAL multiread_create(susp)
read_susp_type susp;
{
LVAL result;
int j;
xlsave1(result);
result = newvector(susp->sf_info.channels); /* create array for sounds */
falloc_generic_n(susp->chan, snd_list_type, susp->sf_info.channels,
"multiread_create");
/* create sounds to return */
for (j = 0; j < susp->sf_info.channels; j++) {
sound_type snd = sound_create((snd_susp_type)susp,
susp->susp.t0, susp->susp.sr, 1.0);
LVAL snd_lval = cvsound(snd);
/* nyquist_printf("multiread_create: sound %d is %x, LVAL %x\n", j, snd, snd_lval); */
setelement(result, j, snd_lval);
susp->chan[j] = snd->list;
}
xlpop();
return result;
}