traverso-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Traverso-commit] traverso/src core/Song.cpp core/WriteSource.cpp...


From: Ben Levitt
Subject: [Traverso-commit] traverso/src core/Song.cpp core/WriteSource.cpp...
Date: Fri, 03 Aug 2007 02:39:36 +0000

CVSROOT:        /sources/traverso
Module name:    traverso
Changes by:     Ben Levitt <benjie>     07/08/03 02:39:36

Modified files:
        src/core       : Song.cpp WriteSource.cpp WriteSource.h 
        src/audiofileio: audiofileio.pro 
        src/traverso   : traverso.pro 
Added files:
        src/audiofileio/encode: AbstractAudioWriter.cpp 
                                AbstractAudioWriter.h SFAudioWriter.cpp 
                                SFAudioWriter.h 

Log message:
        Pull out libsoundfile writing into SFAudioWriter.
        
        I still need to pull the last vestiges of libsndfile out of the rest of
        the code.  (export spec still uses SF_FORMAT_WAV, SF_FORMAT_FLOAT, etc)
        
        Unfortunately, it seems that audio writing was already broken, but this
        patch doesn't break it any further.  :P  (Remon, do you think this broke
        with the universal samplerate changes?)

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/traverso/src/core/Song.cpp?cvsroot=traverso&r1=1.125&r2=1.126
http://cvs.savannah.gnu.org/viewcvs/traverso/src/core/WriteSource.cpp?cvsroot=traverso&r1=1.24&r2=1.25
http://cvs.savannah.gnu.org/viewcvs/traverso/src/core/WriteSource.h?cvsroot=traverso&r1=1.14&r2=1.15
http://cvs.savannah.gnu.org/viewcvs/traverso/src/audiofileio/audiofileio.pro?cvsroot=traverso&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/traverso/src/audiofileio/encode/AbstractAudioWriter.cpp?cvsroot=traverso&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/traverso/src/audiofileio/encode/AbstractAudioWriter.h?cvsroot=traverso&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/traverso/src/audiofileio/encode/SFAudioWriter.cpp?cvsroot=traverso&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/traverso/src/audiofileio/encode/SFAudioWriter.h?cvsroot=traverso&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/traverso/src/traverso/traverso.pro?cvsroot=traverso&r1=1.65&r2=1.66

Patches:
Index: core/Song.cpp
===================================================================
RCS file: /sources/traverso/traverso/src/core/Song.cpp,v
retrieving revision 1.125
retrieving revision 1.126
diff -u -b -r1.125 -r1.126
--- core/Song.cpp       19 Jul 2007 12:28:42 -0000      1.125
+++ core/Song.cpp       3 Aug 2007 02:39:35 -0000       1.126
@@ -1077,7 +1077,7 @@
                set_recording(false, false);
        } else {
                if (!any_track_armed()) {
-                       info().critical(tr("No Tracks armed to record too!"));
+                       info().critical(tr("No Tracks armed for recording!"));
                        return 0;
                }
                

Index: core/WriteSource.cpp
===================================================================
RCS file: /sources/traverso/traverso/src/core/WriteSource.cpp,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- core/WriteSource.cpp        4 Jun 2007 18:33:38 -0000       1.24
+++ core/WriteSource.cpp        3 Aug 2007 02:39:35 -0000       1.25
@@ -25,6 +25,8 @@
 #include <math.h>
 
 #include <AudioDevice.h>
+#include <AbstractAudioWriter.h>
+#include <SFAudioWriter.h>
 #include "Peak.h"
 #include "Utils.h"
 #include "DiskIO.h"
@@ -40,6 +42,7 @@
 {
        diskio = 0;
        m_buffer = 0;
+       m_writer = 0;
        prepare_export();
 }
 
@@ -50,6 +53,7 @@
 {
        diskio = 0;
        m_buffer = 0;
+       m_writer = 0;
        m_channelCount = superChannelCount;
        prepare_export();
 }
@@ -68,6 +72,9 @@
        if (m_spec->isRecording) {
                delete m_spec;
        }
+       if (m_writer) {
+               delete m_writer;
+       }
 }
 
 int WriteSource::process (nframes_t nframes)
@@ -76,8 +83,7 @@
        uint32_t chn;
        uint32_t x;
        uint32_t i;
-       sf_count_t written;
-       char errbuf[256];
+       nframes_t written;
        nframes_t to_write = 0;
        int cnt = 0;
 
@@ -209,32 +215,7 @@
                }
 
                /* and export to disk */
-
-               switch (m_spec->data_width) {
-               case 8:
-                       written = sf_write_raw (sf, (void*) output_data, 
to_write * channels);
-                       break;
-
-               case 16:
-                       written = sf_writef_short (sf, (short*) output_data, 
to_write);
-                       break;
-
-               case 24:
-               case 32:
-                       written = sf_writef_int (sf, (int*) output_data, 
to_write);
-                       break;
-
-               default:
-                       written = sf_writef_float (sf, float_buffer, to_write);
-                       break;
-               }
-
-               if ((nframes_t) written != to_write) {
-                       sf_error_str (sf, errbuf, sizeof (errbuf) - 1);
-                       printf(("Export: could not write data to output file 
(%s)\n"), errbuf);
-                       return -1;
-               }
-
+               written = m_writer->write(output_data, to_write);
 
        } while (leftover_frames >= nframes);
 
@@ -247,7 +228,6 @@
        
        Q_ASSERT(m_spec->is_valid() == 1);
        
-       char errbuf[256];
        GDitherSize dither_size;
 
        sample_rate = audiodevice().get_sample_rate();
@@ -279,31 +259,37 @@
                break;
        }
 
-       memset (&sfinfo, 0, sizeof(sfinfo));
-
-       sfinfo.format = m_spec->format;
-       sfinfo.samplerate = m_spec->sample_rate;
-       sfinfo.frames = m_spec->end_frame - m_spec->start_frame + 1;
-       sfinfo.channels = m_spec->channels;
-
-       if (sf_format_check(&sfinfo) == false) {
-               PWARN("sf_format_check returned false");
+       if (m_writer) {
+               delete m_writer;
        }
 
-
-       /* XXX make sure we have enough disk space for the output */
-
        QString name = m_fileName;
        if (m_spec->isRecording) {
                name.append("-ch" + QByteArray::number(m_channelNumber) + 
".wav");
        }
        
-       if ((sf = sf_open(name.toUtf8().data(), SFM_WRITE, &sfinfo)) == 0) {
-               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-               PWARN("Export: cannot open output file \"%s\" (%s)", 
QS_C(m_fileName), errbuf);
-               return -1;
+       if (m_writer) {
+               delete m_writer;
        }
 
+       if (1) {
+               SFAudioWriter* sfWriter = new SFAudioWriter(name);
+               sfWriter->set_format(m_spec->format); // FIXME: keep SF_FORMAT 
within SFAudioWriter
+               m_writer = sfWriter;
+       }
+       else {
+               //WPAudioWriter* wpWriter = new WPAudioWriter(m_fileName);
+               //m_writer = wpWriter;
+       }
+       m_writer->set_rate(m_spec->sample_rate);
+       m_writer->set_bits_per_sample(m_spec->data_width);
+       m_writer->set_num_channels(m_spec->channels);
+       
+       /* XXX make sure we have enough disk space for the output */
+
+       if (m_writer->open() == false) {
+               return -1;
+       }
 
        if ((uint)m_spec->sample_rate != sample_rate) {
                qDebug("Doing samplerate conversion");
@@ -360,10 +346,10 @@
 {
        PENTER;
 
-       if (sf_close (sf)) {
-               qWarning("sf_close returned an error!");
+       if (m_writer) {
+               delete m_writer;
+               m_writer = 0;
        }
-       sf = 0;
 
        if (dataF2)
                delete dataF2;

Index: core/WriteSource.h
===================================================================
RCS file: /sources/traverso/traverso/src/core/WriteSource.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- core/WriteSource.h  4 Jun 2007 18:33:38 -0000       1.14
+++ core/WriteSource.h  3 Aug 2007 02:39:35 -0000       1.15
@@ -31,6 +31,7 @@
 struct ExportSpecification;
 class Peak;
 class DiskIO;
+class AbstractAudioWriter;
 
 /// WriteSource is an AudioSource only used for writing (recording, rendering) 
purposes
 class WriteSource : public AudioSource
@@ -63,11 +64,10 @@
 
 private:
        RingBufferNPT<audio_sample_t>*  m_buffer;
-       SNDFILE*        sf;
-       SF_INFO         sfinfo;
-       DiskIO*         diskio;
+       AbstractAudioWriter*            m_writer;
        ExportSpecification*    m_spec;
        
+       DiskIO*         diskio;
        GDither         dither;
        nframes_t       out_samples_max;
        nframes_t       sample_rate;

Index: audiofileio/audiofileio.pro
===================================================================
RCS file: /sources/traverso/traverso/src/audiofileio/audiofileio.pro,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- audiofileio/audiofileio.pro 31 Jul 2007 18:45:12 -0000      1.1
+++ audiofileio/audiofileio.pro 3 Aug 2007 02:39:35 -0000       1.2
@@ -16,14 +16,18 @@
        decode/ResampleAudioReader.cpp \
        decode/VorbisAudioReader.cpp \
        decode/WPAudioReader.cpp \
-       decode/MadAudioReader.cpp
+       decode/MadAudioReader.cpp \
+       encode/AbstractAudioWriter.cpp \
+       encode/SFAudioWriter.cpp
 HEADERS = decode/AbstractAudioReader.h \
        decode/SFAudioReader.h \
        decode/FlacAudioReader.h \
        decode/ResampleAudioReader.h \
        decode/VorbisAudioReader.h \
        decode/WPAudioReader.h \
-       decode/MadAudioReader.h
+       decode/MadAudioReader.h \
+       encode/AbstractAudioWriter.h \
+       encode/SFAudioWriter.h
 macx{
     QMAKE_LIBDIR += /usr/local/qt/lib
 }

Index: traverso/traverso.pro
===================================================================
RCS file: /sources/traverso/traverso/src/traverso/traverso.pro,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -b -r1.65 -r1.66
--- traverso/traverso.pro       31 Jul 2007 18:45:15 -0000      1.65
+++ traverso/traverso.pro       3 Aug 2007 02:39:36 -0000       1.66
@@ -188,5 +188,5 @@
                POST_TARGETDEPS += ../3rdparty/slv2
        }
        
-       POST_TARGETDEPS += ../engine ../commands ../core ../plugins songcanvas
+       POST_TARGETDEPS += ../engine ../commands ../core ../audiofileio 
../plugins songcanvas
 }

Index: audiofileio/encode/AbstractAudioWriter.cpp
===================================================================
RCS file: audiofileio/encode/AbstractAudioWriter.cpp
diff -N audiofileio/encode/AbstractAudioWriter.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ audiofileio/encode/AbstractAudioWriter.cpp  3 Aug 2007 02:39:35 -0000       
1.1
@@ -0,0 +1,109 @@
+/*
+Copyright (C) 2007 Ben Levitt 
+
+This file is part of Traverso
+
+Traverso is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+
+*/
+
+#include "AbstractAudioWriter.h"
+#include "SFAudioWriter.h"
+//#include "WPAudioWriter.h"
+
+#include <QString>
+
+// Always put me below _all_ includes, this is needed
+// in case we run with memory leak detection enabled!
+#include "Debugger.h"
+
+
+AbstractAudioWriter::AbstractAudioWriter(const QString& filename)
+ : QObject(0)
+{
+       m_fileName = filename;
+       m_channels = 0;
+       m_rate = 0;
+       m_sampleWidth = 0;
+       m_writePos = 0;
+       
+       m_isOpen = false;
+}
+
+
+AbstractAudioWriter::~AbstractAudioWriter()
+{
+}
+
+
+void AbstractAudioWriter::set_num_channels(int channels)
+{
+       m_channels = channels;
+}
+
+
+void AbstractAudioWriter::set_bits_per_sample(int bits)
+{
+       m_sampleWidth = bits;
+}
+
+
+void AbstractAudioWriter::set_rate(int rate)
+{
+       m_rate = rate;
+}
+
+
+nframes_t AbstractAudioWriter::pos()
+{
+       return m_writePos;
+}
+
+
+bool AbstractAudioWriter::open()
+{
+       close();
+       
+       m_writePos = 0;
+       
+       m_isOpen = open_private();
+       
+       return m_isOpen;
+}
+
+
+void AbstractAudioWriter::close()
+{
+       if (m_isOpen) {
+               close_private();
+               m_isOpen = false;
+       }
+}
+
+
+nframes_t AbstractAudioWriter::write(void* buffer, nframes_t count)
+{
+       if (count) {
+               nframes_t framesWritten = write_private(buffer, count);
+               
+               if (framesWritten > 0) {
+                       m_writePos += framesWritten;
+               }
+               
+               return framesWritten;
+       }
+       
+       return 0;
+}

Index: audiofileio/encode/AbstractAudioWriter.h
===================================================================
RCS file: audiofileio/encode/AbstractAudioWriter.h
diff -N audiofileio/encode/AbstractAudioWriter.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ audiofileio/encode/AbstractAudioWriter.h    3 Aug 2007 02:39:35 -0000       
1.1
@@ -0,0 +1,62 @@
+/*
+Copyright (C) 2007 Ben Levitt 
+
+This file is part of Traverso
+
+Traverso is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+
+*/
+
+#ifndef ABSTRACTAUDIOWRITER_H
+#define ABSTRACTAUDIOWRITER_H
+
+#include <QObject>
+
+#include "defines.h"
+
+class QString;
+
+class AbstractAudioWriter : public QObject
+{
+       Q_OBJECT
+       
+public:
+       AbstractAudioWriter(const QString& filename);
+       ~AbstractAudioWriter();
+       
+       void set_num_channels(int channels);
+       void set_bits_per_sample(int bits);
+       void set_rate(int rate);
+       nframes_t pos();
+       
+       bool open();
+       nframes_t write(void* buffer, nframes_t frameCount);
+       void close();
+       
+protected:
+       virtual bool is_valid_format() = 0;
+       virtual bool open_private() = 0;
+       virtual nframes_t write_private(void* buffer, nframes_t frameCount) = 0;
+       virtual void close_private() = 0;
+       
+       QString         m_fileName;
+       bool            m_isOpen;
+       int             m_sampleWidth;
+       nframes_t       m_writePos;
+       nframes_t       m_channels;
+       nframes_t       m_rate;
+};
+
+#endif

Index: audiofileio/encode/SFAudioWriter.cpp
===================================================================
RCS file: audiofileio/encode/SFAudioWriter.cpp
diff -N audiofileio/encode/SFAudioWriter.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ audiofileio/encode/SFAudioWriter.cpp        3 Aug 2007 02:39:35 -0000       
1.1
@@ -0,0 +1,126 @@
+/*
+Copyright (C) 2007 Ben Levitt 
+
+This file is part of Traverso
+
+Traverso is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+
+*/
+
+#include "SFAudioWriter.h"
+
+#include <QString>
+
+// Always put me below _all_ includes, this is needed
+// in case we run with memory leak detection enabled!
+#include "Debugger.h"
+
+
+SFAudioWriter::SFAudioWriter(const QString& filename)
+ : AbstractAudioWriter(filename)
+{
+       m_sf = 0;
+}
+
+
+SFAudioWriter::~SFAudioWriter()
+{
+       if (m_sf) {
+               close_private();
+       }
+}
+
+
+bool SFAudioWriter::is_valid_format()
+{
+       memset (&m_sfinfo, 0, sizeof(m_sfinfo));
+       m_sfinfo.format = m_format;
+       m_sfinfo.samplerate = m_rate;
+       m_sfinfo.channels = m_channels;
+       //m_sfinfo.frames = m_spec->end_frame - m_spec->start_frame + 1;
+       
+       return sf_format_check(&m_sfinfo);
+}
+
+
+void SFAudioWriter::set_format(int format)
+{
+       m_format = format;
+}
+
+
+bool SFAudioWriter::open_private()
+{
+       char errbuf[256];
+       
+       memset (&m_sfinfo, 0, sizeof(m_sfinfo));
+       m_sfinfo.format = m_format;
+       m_sfinfo.frames = 48000*100;
+       m_sfinfo.samplerate = m_rate;
+       m_sfinfo.channels = m_channels;
+       //m_sfinfo.frames = m_spec->end_frame - m_spec->start_frame + 1;
+       
+       m_sf = sf_open(m_fileName.toUtf8().data(), SFM_WRITE, &m_sfinfo);
+       if (m_sf == 0) {
+               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
+               PWARN("Export: cannot open output file \"%s\" (%s)", 
m_fileName.toUtf8().data(), errbuf);
+               return false;
+       }
+       
+       return true;
+}
+
+
+nframes_t SFAudioWriter::write_private(void* buffer, nframes_t frameCount)
+{
+       int written = 0;
+       char errbuf[256];
+       
+       switch (m_sampleWidth) {
+               case 8:
+                       written = sf_write_raw (m_sf, (void*) buffer, 
frameCount * m_channels);
+                       break;
+
+               case 16:
+                       written = sf_writef_short (m_sf, (short*) buffer, 
frameCount);
+                       break;
+
+               case 24:
+               case 32:
+                       written = sf_writef_int (m_sf, (int*) buffer, 
frameCount);
+                       break;
+
+               default:
+                       written = sf_writef_float (m_sf, (float*) buffer, 
frameCount);
+                       break;
+       }
+       
+       if ((nframes_t) written != frameCount) {
+               sf_error_str (m_sf, errbuf, sizeof (errbuf) - 1);
+               PERROR("Export: could not write data to output file (%s)\n", 
errbuf);
+               return -1;
+       }
+       
+       return written;
+}
+
+
+void SFAudioWriter::close_private()
+{
+       if (sf_close(m_sf)) {
+               qWarning("sf_close returned an error!");
+       }
+       m_sf = 0;
+}

Index: audiofileio/encode/SFAudioWriter.h
===================================================================
RCS file: audiofileio/encode/SFAudioWriter.h
diff -N audiofileio/encode/SFAudioWriter.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ audiofileio/encode/SFAudioWriter.h  3 Aug 2007 02:39:35 -0000       1.1
@@ -0,0 +1,53 @@
+/*
+Copyright (C) 2007 Ben Levitt 
+
+This file is part of Traverso
+
+Traverso is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+
+*/
+
+#ifndef SFAUDIOWRITER_H
+#define SFAUDIOWRITER_H
+
+#include "AbstractAudioWriter.h"
+
+#include "defines.h"
+#include "sndfile.h"
+
+class QString;
+
+class SFAudioWriter : public AbstractAudioWriter
+{
+       Q_OBJECT
+       
+public:
+       SFAudioWriter(const QString& filename);
+       ~SFAudioWriter();
+       
+       bool is_valid_format();
+       void set_format(int format);
+       
+protected:
+       bool open_private();
+       nframes_t write_private(void* buffer, nframes_t frameCount);
+       void close_private();
+       
+       int             m_format;       
+       SNDFILE*        m_sf;
+       SF_INFO         m_sfinfo;
+};
+
+#endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]