1 /*
2 Copyright (C) 2007 Ben Levitt
3 
4 This file is part of Traverso
5 
6 Traverso is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
19 
20 */
21 
22 #include "AbstractAudioWriter.h"
23 #include "SFAudioWriter.h"
24 #include "WPAudioWriter.h"
25 #if defined MP3_ENCODE_SUPPORT
26 #include "LameAudioWriter.h"
27 #endif
28 #include "VorbisAudioWriter.h"
29 #include "FlacAudioWriter.h"
30 
31 #include <QString>
32 
33 RELAYTOOL_WAVPACK;
34 RELAYTOOL_MP3LAME;
35 RELAYTOOL_FLAC;
36 
37 // Always put me below _all_ includes, this is needed
38 // in case we run with memory leak detection enabled!
39 #include "Debugger.h"
40 
41 
AbstractAudioWriter()42 AbstractAudioWriter::AbstractAudioWriter()
43 {
44 	m_channels = 0;
45 	m_rate = 0;
46 	m_sampleWidth = 0;
47 	m_writePos = 0;
48 
49 	m_isOpen = false;
50 }
51 
52 
~AbstractAudioWriter()53 AbstractAudioWriter::~AbstractAudioWriter()
54 {
55 }
56 
57 
set_num_channels(int channels)58 void AbstractAudioWriter::set_num_channels(int channels)
59 {
60 	m_channels = channels;
61 }
62 
63 
set_bits_per_sample(int bits)64 void AbstractAudioWriter::set_bits_per_sample(int bits)
65 {
66 	m_sampleWidth = bits;
67 }
68 
69 
set_rate(int rate)70 void AbstractAudioWriter::set_rate(int rate)
71 {
72 	m_rate = rate;
73 }
74 
75 
set_format_attribute(const QString & key,const QString & value)76 bool AbstractAudioWriter::set_format_attribute(const QString& key, const QString& value)
77 {
78 	Q_UNUSED(key);
79 	Q_UNUSED(value);
80 	return false;
81 }
82 
83 
pos()84 nframes_t AbstractAudioWriter::pos()
85 {
86 	return m_writePos;
87 }
88 
89 
open(const QString & filename)90 bool AbstractAudioWriter::open(const QString& filename)
91 {
92 	if (m_isOpen) {
93 		close();
94 	}
95 
96 	m_writePos = 0;
97 	m_fileName = filename;
98 
99 	m_isOpen = open_private();
100 
101 	return m_isOpen;
102 }
103 
104 
close()105 bool AbstractAudioWriter::close()
106 {
107 	bool success = false;;
108 
109 	if (m_isOpen) {
110 		success = close_private();
111 		m_isOpen = false;
112 	}
113 
114 	return success;
115 }
116 
117 
write(void * buffer,nframes_t count)118 nframes_t AbstractAudioWriter::write(void* buffer, nframes_t count)
119 {
120 	if (m_isOpen && buffer && count) {
121 		nframes_t framesWritten = write_private(buffer, count);
122 
123 		if (framesWritten > 0) {
124 			m_writePos += framesWritten;
125 		}
126 
127 		return framesWritten;
128 	}
129 
130 	return 0;
131 }
132 
133 
134 // Static method used by other classes to get an AudioWriter for the correct file type
create_audio_writer(const QString & type)135 AbstractAudioWriter* AbstractAudioWriter::create_audio_writer(const QString& type)
136 {
137 	if (type == "sndfile") {
138 		return new SFAudioWriter();
139 	}
140 	else if (libwavpack_is_present && type == "wavpack") {
141 		return new WPAudioWriter();
142 	}
143 #if defined MP3_ENCODE_SUPPORT
144 	else if (libmp3lame_is_present && type == "lame") {
145 		return new LameAudioWriter();
146 	}
147 #endif
148 	else if (type == "vorbis") {
149 		return new VorbisAudioWriter();
150 	}
151 	else if (libFLAC_is_present && type == "flac") {
152 		return new FlacAudioWriter();
153 	}
154 
155 	return 0;
156 }
157