1 // lame encoding functions for butt
2 //
3 // Copyright 2007-2018 by Daniel Noethen.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 
16 #include <stdlib.h>
17 #include <lame/lame.h>
18 
19 #include "gettext.h"
20 
21 #include "lame_encode.h"
22 
23 #include "fl_funcs.h"
24 
lame_enc_init(lame_enc * lame)25 int lame_enc_init(lame_enc *lame)
26 {
27     int rc;
28     char info_buf[256];
29 
30     lame->gfp = lame_init();
31 
32     lame_set_num_channels(lame->gfp, lame->channel);
33     lame_set_in_samplerate(lame->gfp, lame->samplerate);
34     lame_set_out_samplerate(lame->gfp, lame->samplerate);
35     lame_set_brate(lame->gfp, lame->bitrate);
36 
37     if((rc = lame_init_params(lame->gfp)) < 0)
38     {
39         snprintf(info_buf, sizeof(info_buf),
40                 _("unable to init lame params %d"), rc);
41 
42         print_info(info_buf, 1);
43         return 1;
44     }
45 
46     lame->state = LAME_READY;
47     return 0;
48 }
49 
lame_enc_reinit(lame_enc * lame)50 int lame_enc_reinit(lame_enc *lame)
51 {
52     if(lame != NULL)
53     {
54         lame_enc_close(lame);
55         return lame_enc_init(lame);
56     }
57     return 1;
58 }
59 
lame_enc_close(lame_enc * lame)60 void lame_enc_close(lame_enc *lame)
61 {
62     while(lame->state == LAME_BUSY)
63        ;
64 
65     if (lame->gfp != NULL)
66         lame_close(lame->gfp);
67 
68     lame->gfp = NULL;
69 }
70 
lame_enc_encode(lame_enc * lame,short * pcm_buf,char * enc_buf,int samples,int size)71 int lame_enc_encode(lame_enc *lame, short *pcm_buf, char *enc_buf, int samples, int size)
72 {
73     int rc;
74 
75     if(samples == 0 || lame->gfp == NULL)
76         return 0;
77 
78     lame->state = LAME_BUSY;
79 
80 
81     if(lame->channel == 2) // stereo
82         rc = lame_encode_buffer_interleaved(lame->gfp, pcm_buf, samples, (unsigned char*)enc_buf, size);
83     else // mono
84         rc = lame_encode_buffer(lame->gfp, pcm_buf, pcm_buf, samples, (unsigned char*)enc_buf, size);
85 
86 
87     lame->state = LAME_READY;
88 
89     return rc;
90 }
91 
92