Make duplicate script compatible with Python 3
[wdl/wdl-ol.git] / WDL / mp3write.h
blob8add096b2d1612b9dd75861b79e322e1c2e8f8ec
1 /*
2 WDL - mp3write.h
3 Copyright (C) 2005 Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
25 This file provides a simple class for writing MP3 files (using lameencdec.h)
30 #ifndef _MP3WRITE_H_
31 #define _MP3WRITE_H_
34 #include <stdio.h>
35 #include "lameencdec.h"
37 class mp3Writer
39 public:
40 // appending doesnt check sample types
41 mp3Writer()
43 m_enc=0;
44 m_fp=0;
45 m_srate=0;
46 m_nch=0;
49 mp3Writer(char *filename, int nch, int srate, int bitrate, int allow_append=1)
51 m_enc=0;
52 m_fp=0;
53 m_srate=0;
54 m_nch=0;
55 Open(filename,nch,srate,bitrate,allow_append);
59 int Open(char *filename, int nch, int srate, int bitrate, int allow_append=1)
61 m_fp=0;
62 if (allow_append)
64 m_fp=fopen(filename,"r+b");
65 if (m_fp)
67 fseek(m_fp,0,SEEK_END);
70 if (!m_fp)
72 m_fp=fopen(filename,"wb");
74 m_nch=nch>1?2:1;
75 m_srate=srate;
76 m_enc = new LameEncoder(srate,nch,bitrate);
77 if (m_enc->Status())
79 delete m_enc;
80 m_enc=0;
82 return m_fp && m_enc;
85 ~mp3Writer()
87 if (m_fp)
89 if (m_enc)
91 m_enc->Encode(NULL,0);
92 if (m_enc->outqueue.Available())
94 fwrite(m_enc->outqueue.Get(),1,m_enc->outqueue.GetSize(),m_fp);
95 fflush(m_fp);
96 m_enc->outqueue.Advance(m_enc->outqueue.GetSize());
97 m_enc->outqueue.Compact();
101 fclose(m_fp);
102 m_fp=0;
104 if (m_enc)
106 delete m_enc;
107 m_enc=0;
111 int Status() { return m_enc && m_fp; }
113 void WriteFloats(float *samples, int nsamples)
115 if (!m_fp || !m_enc) return;
117 m_enc->Encode(samples,nsamples/m_nch);
118 if (m_enc->outqueue.Available())
120 fwrite(m_enc->outqueue.Get(),1,m_enc->outqueue.GetSize(),m_fp);
121 fflush(m_fp);
122 m_enc->outqueue.Advance(m_enc->outqueue.GetSize());
123 m_enc->outqueue.Compact();
128 int get_nch() { return m_nch; }
129 int get_srate() { return m_srate; }
131 private:
132 FILE *m_fp;
133 int m_nch,m_srate;
134 LameEncoder *m_enc;
138 #endif//_MP3WRITE_H_