svn merge -r39781:39792 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[blender-staging.git] / intern / audaspace / intern / AUD_FileWriter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/intern/AUD_FileWriter.cpp
28  *  \ingroup audaspaceintern
29  */
30
31 #ifdef WITH_FFMPEG
32 // needed for INT64_C
33 #ifndef __STDC_CONSTANT_MACROS
34 #define __STDC_CONSTANT_MACROS
35 #endif
36 #include "AUD_FFMPEGWriter.h"
37 #endif
38
39 #ifdef WITH_SNDFILE
40 #include "AUD_SndFileWriter.h"
41 #endif
42
43 #include "AUD_FileWriter.h"
44 #include "AUD_Buffer.h"
45
46 static const char* write_error = "AUD_FileWriter: File couldn't be written.";
47
48 AUD_Reference<AUD_IWriter> AUD_FileWriter::createWriter(std::string filename,AUD_DeviceSpecs specs,
49                                                                                                                 AUD_Container format, AUD_Codec codec, unsigned int bitrate)
50 {
51 #ifdef WITH_SNDFILE
52         try
53         {
54                 return new AUD_SndFileWriter(filename, specs, format, codec, bitrate);
55         }
56         catch(AUD_Exception&) {}
57 #endif
58
59 #ifdef WITH_FFMPEG
60         try
61         {
62                 return new AUD_FFMPEGWriter(filename, specs, format, codec, bitrate);
63         }
64         catch(AUD_Exception&) {}
65 #endif
66
67         AUD_THROW(AUD_ERROR_SPECS, write_error);
68 }
69
70 void AUD_FileWriter::writeReader(AUD_Reference<AUD_IReader> reader, AUD_Reference<AUD_IWriter> writer, unsigned int length, unsigned int buffersize)
71 {
72         AUD_Buffer buffer(buffersize * AUD_SAMPLE_SIZE(writer->getSpecs()));
73         sample_t* buf = buffer.getBuffer();
74
75         int len;
76         bool eos = false;
77         int channels = writer->getSpecs().channels;
78
79         for(unsigned int pos = 0; ((pos < length) || (length <= 0)) && !eos; pos += len)
80         {
81                 len = buffersize;
82                 if((len > length - pos) && (length > 0))
83                         len = length - pos;
84                 reader->read(len, eos, buf);
85
86                 for(int i = 0; i < len * channels; i++)
87                 {
88                         // clamping!
89                         if(buf[i] > 1)
90                                 buf[i] = 1;
91                         else if(buf[i] < -1)
92                                 buf[i] = -1;
93                 }
94
95                 writer->write(len, buf);
96         }
97 }