669
#define savefileFromTable_info \
670
"\nCreates an audio file from the content of a table.\n\nsavefileFromTable(table, path, fileformat=0, sampletype=0)\n\nParameters:\n\n \
671
table : PyoTableObject\n table from which to retrieve samples to write.\n \
672
path : string\n Full path (including extension) of the new file.\n \
673
fileformat : int, optional\n Format type of the new file. Defaults to 0. Supported formats are:\n \
674
0 : WAVE - Microsoft WAV format (little endian) {.wav, .wave}\n \
675
1 : AIFF - Apple/SGI AIFF format (big endian) {.aif, .aiff}\n \
676
sampletype ; int, optional\n Bit depth encoding of the audio file. Defaults to 0. Supported types are:\n \
681
4 : 64 bit float\n\n\
684
>>> home = os.path.expanduser('~')\n \
685
>>> path1 = SNDS_PATH + '/transparent.aif'\n \
686
>>> path2 = os.path.join(home, '/transparent2.aif')\n \
687
>>> t = SndTable(path1)\n \
688
>>> savefileFromTable(table=t, path=path, fileformat=1, sampletype=1)\n\n"
691
savefileFromTable(PyObject *self, PyObject *args, PyObject *kwds) {
696
PyObject *tablestreamlist;
706
static char *kwlist[] = {"table", "path", "fileformat", "sampletype", NULL};
708
if (! PyArg_ParseTupleAndKeywords(args, kwds, "Os|ii", kwlist, &table, &recpath, &fileformat, &sampletype))
709
return PyInt_FromLong(-1);
711
base_objs = PyObject_GetAttrString(table, "_base_objs");
712
channels = PyList_Size(base_objs);
713
tablestreamlist = PyList_New(channels);
714
for (i=0; i<channels; i++) {
715
PyList_SET_ITEM(tablestreamlist, i, PyObject_CallMethod(PyList_GetItem(base_objs, i), "getTableStream", NULL));
717
sr = (int)TableStream_getSamplingRate((PyObject *)PyList_GetItem(tablestreamlist, 0));
718
size = TableStream_getSize((PyObject *)PyList_GetItem(tablestreamlist, 0));
720
recinfo.samplerate = sr;
721
recinfo.channels = channels;
722
switch (fileformat) {
724
recinfo.format = SF_FORMAT_WAV;
727
recinfo.format = SF_FORMAT_AIFF;
730
switch (sampletype) {
732
recinfo.format = recinfo.format | SF_FORMAT_PCM_16;
735
recinfo.format = recinfo.format | SF_FORMAT_PCM_24;
738
recinfo.format = recinfo.format | SF_FORMAT_PCM_32;
741
recinfo.format = recinfo.format | SF_FORMAT_FLOAT;
744
recinfo.format = recinfo.format | SF_FORMAT_DOUBLE;
748
if (! (recfile = sf_open(recpath, SFM_WRITE, &recinfo))) {
749
printf ("savefileFromTable: failed to open output file %s.\n", recpath);
750
Py_XDECREF(base_objs);
751
Py_XDECREF(tablestreamlist);
752
return PyInt_FromLong(-1);
757
if (size < (sr * 60)) {
758
data = TableStream_getData((PyObject *)PyList_GetItem(tablestreamlist, 0));
759
sampsarray = (MYFLT *)malloc(size * sizeof(MYFLT));
760
for (i=0; i<size; i++) {
761
sampsarray[i] = data[i];
763
SF_WRITE(recfile, sampsarray, size);
766
data = TableStream_getData((PyObject *)PyList_GetItem(tablestreamlist, 0));
768
sampsarray = (MYFLT *)malloc(num_items * sizeof(MYFLT));
770
if ((size - count) < num_items)
771
num_items = size - count;
772
for (i=0; i<num_items; i++) {
773
sampsarray[i] = data[count++];
775
SF_WRITE(recfile, sampsarray, num_items);
776
} while (num_items == (sr * 30));
780
MYFLT *data[channels];
781
if (size < (sr * 60)) {
782
for (j=0; j<channels; j++) {
783
data[j] = TableStream_getData((PyObject *)PyList_GetItem(tablestreamlist, j));
785
sampsarray = (MYFLT *)malloc(size * channels * sizeof(MYFLT));
786
for (i=0; i<size; i++) {
787
for (j=0; j<channels; j++) {
788
sampsarray[i*channels+j] = data[j][i];
791
SF_WRITE(recfile, sampsarray, size * channels);
794
for (j=0; j<channels; j++) {
795
data[j] = TableStream_getData((PyObject *)PyList_GetItem(tablestreamlist, j));
798
sampsarray = (MYFLT *)malloc(num_items * channels * sizeof(MYFLT));
800
if ((size - count) < num_items)
801
num_items = size - count;
802
for (i=0; i<num_items; i++) {
803
for (j=0; j<channels; j++) {
804
sampsarray[i*channels+j] = data[j][count];
808
SF_WRITE(recfile, sampsarray, num_items * channels);
809
} while (num_items == (sr * 30));
815
Py_XDECREF(base_objs);
816
Py_XDECREF(tablestreamlist);
708
821
/****** Sampling rate conversions ******/
709
822
#define upsamp_info \
710
823
"\nIncreases the sampling rate of an audio file.\n\nupsamp(path, outfile, up=4, order=128)\n\nParameters:\n\n \
1771
1886
{"pm_get_default_output", (PyCFunction)portmidi_get_default_output, METH_NOARGS, portmidi_get_default_output_info},
1772
1887
{"sndinfo", (PyCFunction)sndinfo, METH_VARARGS|METH_KEYWORDS, sndinfo_info},
1773
1888
{"savefile", (PyCFunction)savefile, METH_VARARGS|METH_KEYWORDS, savefile_info},
1889
{"savefileFromTable", (PyCFunction)savefileFromTable, METH_VARARGS|METH_KEYWORDS, savefileFromTable_info},
1774
1890
{"upsamp", (PyCFunction)upsamp, METH_VARARGS|METH_KEYWORDS, upsamp_info},
1775
1891
{"downsamp", (PyCFunction)downsamp, METH_VARARGS|METH_KEYWORDS, downsamp_info},
1776
1892
{"reducePoints", (PyCFunction)reducePoints, METH_VARARGS|METH_KEYWORDS, reducePoints_info},