fixed graphics bug
[swftools.git] / lib / lame / set_get.c
1 /* -*- mode: C; mode: fold -*- */
2 /*
3  * set/get functions for lame_global_flags
4  *
5  * Copyright (c) 2001 Alexander Leidinger
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* $Id: set_get.c,v 1.1 2002/04/28 17:30:28 kramm Exp $ */
24
25 #include "config_static.h"
26
27 #include <assert.h>
28 #include "util.h"
29 #include "bitstream.h"  /* because of compute_flushbits */
30
31 #ifdef WITH_DMALLOC
32 #include <dmalloc.h>
33 #endif
34
35
36 /*
37  * input stream description
38  */
39
40 /* number of samples */
41 /* it's unlikely for this function to return an error */
42 int
43 lame_set_num_samples( lame_global_flags*  gfp,
44                       unsigned long       num_samples)
45 {
46     /* default = 2^32-1 */
47
48     gfp->num_samples = num_samples;
49
50     return 0;
51 }
52
53 unsigned long
54 lame_get_num_samples( const lame_global_flags* gfp )
55 {
56     return gfp->num_samples;
57 }
58
59
60 /* input samplerate */
61 int
62 lame_set_in_samplerate( lame_global_flags*  gfp,
63                         int                 in_samplerate )
64 {
65     /* input sample rate in Hz,  default = 44100 Hz */
66     gfp->in_samplerate = in_samplerate;
67
68     return 0;
69 }
70
71 int
72 lame_get_in_samplerate( const lame_global_flags*  gfp )
73 {
74     return gfp->in_samplerate;
75 }
76
77
78 /* number of channels in input stream */
79 int
80 lame_set_num_channels( lame_global_flags*  gfp,
81                        int                 num_channels )
82 {
83     /* default = 2 */
84
85     if ( 2 < num_channels || 0 == num_channels )
86         return -1;    /* we don't support more than 2 channels */
87
88     gfp->num_channels = num_channels;
89
90     return 0;
91 }
92
93 int
94 lame_get_num_channels( const lame_global_flags*  gfp )
95 {
96     return gfp->num_channels;
97 }
98
99
100 /* scale the input by this amount before encoding (not used for decoding) */
101 int
102 lame_set_scale( lame_global_flags*  gfp,
103                 float               scale )
104 {
105     /* default = 0 */
106     gfp->scale = scale;
107
108     return 0;
109 }
110
111 float
112 lame_get_scale( const lame_global_flags*  gfp )
113 {
114     return gfp->scale;
115 }
116
117
118 /* scale the channel 0 (left) input by this amount before 
119    encoding (not used for decoding) */
120 int
121 lame_set_scale_left( lame_global_flags*  gfp,
122                      float               scale )
123 {
124     /* default = 0 */
125     gfp->scale_left = scale;
126
127     return 0;
128 }
129
130 float
131 lame_get_scale_left( const lame_global_flags*  gfp )
132 {
133     return gfp->scale_left;
134 }
135
136
137 /* scale the channel 1 (right) input by this amount before 
138    encoding (not used for decoding) */
139 int
140 lame_set_scale_right( lame_global_flags*  gfp,
141                       float               scale )
142 {
143     /* default = 0 */
144     gfp->scale_right = scale;
145
146     return 0;
147 }
148
149 float
150 lame_get_scale_right( const lame_global_flags*  gfp )
151 {
152     return gfp->scale_right;
153 }
154
155
156 /* output sample rate in Hz */
157 int
158 lame_set_out_samplerate( lame_global_flags*  gfp,
159                          int                 out_samplerate )
160 {
161     /*
162      * default = 0: LAME picks best value based on the amount
163      *              of compression
164      * MPEG only allows:
165      *  MPEG1    32, 44.1,   48khz
166      *  MPEG2    16, 22.05,  24
167      *  MPEG2.5   8, 11.025, 12
168      *
169      * (not used by decoding routines)
170      */
171     gfp->out_samplerate = out_samplerate;
172
173     return 0;
174 }
175
176 int
177 lame_get_out_samplerate( const lame_global_flags*  gfp )
178 {
179     return gfp->out_samplerate;
180 }
181
182
183
184
185 /*
186  * general control parameters
187  */
188
189 /* collect data for an MP3 frame analzyer */
190 int
191 lame_set_analysis( lame_global_flags*  gfp,
192                    int                 analysis )
193 {
194     /* default = 0 */
195
196     /* enforce disable/enable meaning, if we need more than two values
197        we need to switch to an enum to have an apropriate representation
198        of the possible meanings of the value */
199     if ( 0 > analysis || 1 < analysis )
200         return -1;
201
202     gfp->analysis = analysis;
203
204     return 0;
205 }
206
207 int
208 lame_get_analysis( const lame_global_flags*  gfp )
209 {
210     assert( 0 <= gfp->analysis && 1 >= gfp->analysis );
211
212     return gfp->analysis;
213 }
214
215
216 /* write a Xing VBR header frame */
217 int
218 lame_set_bWriteVbrTag( lame_global_flags*  gfp,
219                        int bWriteVbrTag )
220 {
221     /* default = 1 (on) for VBR/ABR modes, 0 (off) for CBR mode */
222
223     /* enforce disable/enable meaning, if we need more than two values
224        we need to switch to an enum to have an apropriate representation
225        of the possible meanings of the value */
226     if ( 0 > bWriteVbrTag || 1 < bWriteVbrTag )
227         return -1;
228
229     gfp->bWriteVbrTag = bWriteVbrTag;
230
231     return 0;
232 }
233
234 int
235 lame_get_bWriteVbrTag( const lame_global_flags*  gfp )
236 {
237     assert( 0 <= gfp->bWriteVbrTag && 1 >= gfp->bWriteVbrTag );
238
239     return gfp->bWriteVbrTag;
240 }
241
242
243
244 /* decode only, use lame/mpglib to convert mp3/ogg to wav */
245 int
246 lame_set_decode_only( lame_global_flags*  gfp,
247                       int                 decode_only )
248 {
249     /* default = 0 (disabled) */
250
251     /* enforce disable/enable meaning, if we need more than two values
252        we need to switch to an enum to have an apropriate representation
253        of the possible meanings of the value */
254     if ( 0 > decode_only || 1 < decode_only )
255         return -1;
256
257     gfp->decode_only = decode_only;
258
259     return 0;
260 }
261
262 int
263 lame_get_decode_only( const lame_global_flags*  gfp )
264 {
265     assert( 0 <= gfp->decode_only && 1 >= gfp->decode_only );
266
267     return gfp->decode_only;
268 }
269
270
271 /* encode a Vorbis .ogg file */
272 int
273 lame_set_ogg( lame_global_flags*  gfp,
274               int                 ogg )
275 {
276     /* default = 0 (disabled) */
277
278     /* enforce disable/enable meaning, if we need more than two values
279        we need to switch to an enum to have an apropriate representation
280        of the possible meanings of the value */
281     if ( 0 > ogg || 1 < ogg )
282         return -1;
283
284     gfp->ogg = ogg;
285
286     return 0;
287 }
288
289 int
290 lame_get_ogg( const lame_global_flags*  gfp )
291 {
292     assert( 0 <= gfp->ogg && 1 >= gfp->ogg );
293
294     return gfp->ogg;
295 }
296
297
298 /*
299  * Internal algorithm selection.
300  * True quality is determined by the bitrate but this variable will effect
301  * quality by selecting expensive or cheap algorithms.
302  * quality=0..9.  0=best (very slow).  9=worst.  
303  * recommended:  2     near-best quality, not too slow
304  *               5     good quality, fast
305  *               7     ok quality, really fast
306  */
307 int
308 lame_set_quality( lame_global_flags*  gfp,
309                   int                 quality )
310 {
311     gfp->quality = quality;
312
313     return 0;
314 }
315
316 int
317 lame_get_quality( const lame_global_flags*  gfp )
318 {
319     return gfp->quality;
320 }
321
322
323 /* mode = STEREO, JOINT_STEREO, DUAL_CHANNEL (not supported), MONO */
324 int
325 lame_set_mode( lame_global_flags*  gfp,
326                MPEG_mode           mode )
327 {
328     /* default: lame chooses based on compression ratio and input channels */
329
330     if( 0 > mode || MAX_INDICATOR <= mode )
331         return -1;  /* Unknown MPEG mode! */
332
333     gfp->mode = mode;
334
335     return 0;
336 }
337
338 MPEG_mode
339 lame_get_mode( const lame_global_flags*  gfp )
340 {
341     assert( 0 <= gfp->mode && MAX_INDICATOR > gfp->mode );
342
343     return gfp->mode;
344 }
345
346
347 /* Us a M/S mode with a switching threshold based on compression ratio */
348 int
349 lame_set_mode_automs( lame_global_flags*  gfp,
350                       int                 mode_automs )
351 {
352     /* default = 0 (disabled) */
353
354     /* enforce disable/enable meaning, if we need more than two values
355        we need to switch to an enum to have an apropriate representation
356        of the possible meanings of the value */
357     if ( 0 > mode_automs || 1 < mode_automs )
358         return -1;
359
360     gfp->mode_automs = mode_automs;
361
362     return 0;
363 }
364
365 int
366 lame_get_mode_automs( const lame_global_flags*  gfp )
367 {
368     assert( 0 <= gfp->mode_automs && 1 >= gfp->mode_automs );
369
370     return gfp->mode_automs;
371 }
372
373
374 /*
375  * Force M/S for all frames.  For testing only.
376  * Requires mode = 1.
377  */
378 int
379 lame_set_force_ms( lame_global_flags*  gfp,
380                    int                 force_ms )
381 {
382     /* default = 0 (disabled) */
383
384     /* enforce disable/enable meaning, if we need more than two values
385        we need to switch to an enum to have an apropriate representation
386        of the possible meanings of the value */
387     if ( 0 > force_ms || 1 < force_ms )
388         return -1;
389
390     gfp->force_ms = force_ms;
391
392     return 0;
393 }
394
395 int
396 lame_get_force_ms( const lame_global_flags*  gfp )
397 {
398     assert( 0 <= gfp->force_ms && 1 >= gfp->force_ms );
399
400     return gfp->force_ms;
401 }
402
403
404 /* Use free_format. */
405 int
406 lame_set_free_format( lame_global_flags*  gfp,
407                       int                 free_format )
408 {
409     /* default = 0 (disabled) */
410
411     /* enforce disable/enable meaning, if we need more than two values
412        we need to switch to an enum to have an apropriate representation
413        of the possible meanings of the value */
414     if ( 0 > free_format || 1 < free_format )
415         return -1;
416
417     gfp->free_format = free_format;
418
419     return 0;
420 }
421
422 int
423 lame_get_free_format( const lame_global_flags*  gfp )
424 {
425     assert( 0 <= gfp->free_format && 1 >= gfp->free_format );
426
427     return gfp->free_format;
428 }
429
430
431 /* message handlers */
432 int
433 lame_set_errorf( lame_global_flags*  gfp,
434                  void                (*func)( const char*, va_list ) )
435 {
436     gfp->report.errorf = func;
437
438     return 0;
439 }
440
441 int
442 lame_set_debugf( lame_global_flags*  gfp,
443                  void                (*func)( const char*, va_list ) )
444 {
445     gfp->report.debugf = func;
446
447     return 0;
448 }
449
450 int
451 lame_set_msgf( lame_global_flags*  gfp,
452                void                (*func)( const char *, va_list ) )
453 {
454     gfp->report.msgf = func;
455
456     return 0;
457 }
458
459
460 /*
461  * Set one of
462  *  - brate
463  *  - compression ratio.
464  *
465  * Default is compression ratio of 11.
466  */
467 int
468 lame_set_brate( lame_global_flags*  gfp,
469                 int                 brate )
470 {
471     gfp->brate = brate;
472
473     return 0;
474 }
475
476 int
477 lame_get_brate( const lame_global_flags*  gfp )
478 {
479     return gfp->brate;
480 }
481
482 int
483 lame_set_compression_ratio( lame_global_flags*  gfp,
484                             float               compression_ratio )
485 {
486     gfp->compression_ratio = compression_ratio;
487
488     return 0;
489 }
490
491 float
492 lame_get_compression_ratio( const lame_global_flags*  gfp )
493 {
494     return gfp->compression_ratio;
495 }
496
497
498
499
500 /*
501  * frame parameters
502  */
503
504 /* Mark as copyright protected. */
505 int
506 lame_set_copyright( lame_global_flags*  gfp,
507                     int                 copyright )
508 {
509     /* default = 0 (disabled) */
510
511     /* enforce disable/enable meaning, if we need more than two values
512        we need to switch to an enum to have an apropriate representation
513        of the possible meanings of the value */
514     if ( 0 > copyright || 1 < copyright )
515         return -1;
516
517     gfp->copyright = copyright;
518
519     return 0;
520 }
521
522 int
523 lame_get_copyright( const lame_global_flags*  gfp )
524 {
525     assert( 0 <= gfp->copyright && 1 >= gfp->copyright );
526
527     return gfp->copyright;
528 }
529
530
531 /* Mark as original. */
532 int
533 lame_set_original( lame_global_flags*  gfp,
534                    int                 original )
535 {
536     /* default = 1 (enabled) */
537
538     /* enforce disable/enable meaning, if we need more than two values
539        we need to switch to an enum to have an apropriate representation
540        of the possible meanings of the value */
541     if ( 0 > original || 1 < original )
542         return -1;
543
544     gfp->original = original;
545
546     return 0;
547 }
548
549 int
550 lame_get_original( const lame_global_flags*  gfp )
551 {
552     assert( 0 <= gfp->original && 1 >= gfp->original );
553
554     return gfp->original;
555 }
556
557
558 /*
559  * error_protection.
560  * Use 2 bytes from each frame for CRC checksum.
561  */
562 int
563 lame_set_error_protection( lame_global_flags*  gfp,
564                            int                 error_protection )
565 {
566     /* default = 0 (disabled) */
567
568     /* enforce disable/enable meaning, if we need more than two values
569        we need to switch to an enum to have an apropriate representation
570        of the possible meanings of the value */
571     if ( 0 > error_protection || 1 < error_protection )
572         return -1;
573
574     gfp->error_protection = error_protection;
575
576     return 0;
577 }
578
579 int
580 lame_get_error_protection( const lame_global_flags*  gfp )
581 {
582     assert( 0 <= gfp->error_protection && 1 >= gfp->error_protection );
583
584     return gfp->error_protection;
585 }
586
587
588 /*
589  * padding_type.
590  *  PAD_NO     = pad no frames
591  *  PAD_ALL    = pad all frames
592  *  PAD_ADJUST = adjust padding
593  */
594 int
595 lame_set_padding_type( lame_global_flags*  gfp,
596                        Padding_type        padding_type )
597 {
598     /* default = 2 */
599
600     if ( 0 > padding_type || PAD_MAX_INDICATOR < padding_type )
601         return -1;  /* Unknown padding type */
602
603     gfp->padding_type = padding_type;
604
605     return 0;
606 }
607
608 Padding_type
609 lame_get_padding_type( const lame_global_flags*  gfp )
610 {
611     assert( 0 <= gfp->padding_type && PAD_MAX_INDICATOR > gfp->padding_type );
612
613     return gfp->padding_type;
614 }
615
616
617 /* MP3 'private extension' bit. Meaningless. */
618 int
619 lame_set_extension( lame_global_flags*  gfp,
620                     int                 extension )
621 {
622     /* default = 0 (disabled) */
623
624     /* enforce disable/enable meaning, if we need more than two values
625        we need to switch to an enum to have an apropriate representation
626        of the possible meanings of the value */
627     if ( 0 > extension || 1 < extension )
628         return -1;
629
630     gfp->extension = extension;
631
632     return 0;
633 }
634
635 int
636 lame_get_extension( const lame_global_flags*  gfp )
637 {
638     assert( 0 <= gfp->extension && 1 >= gfp->extension );
639
640     return gfp->extension;
641 }
642
643
644 /* Enforce strict ISO compliance. */
645 int
646 lame_set_strict_ISO( lame_global_flags*  gfp,
647                      int                 strict_ISO )
648 {
649     /* default = 0 (disabled) */
650
651     /* enforce disable/enable meaning, if we need more than two values
652        we need to switch to an enum to have an apropriate representation
653        of the possible meanings of the value */
654     if ( 0 > strict_ISO || 1 < strict_ISO )
655         return -1;
656
657     gfp->strict_ISO = strict_ISO;
658
659     return 0;
660 }
661
662 int
663 lame_get_strict_ISO( const lame_global_flags*  gfp )
664 {
665     assert( 0 <= gfp->strict_ISO && 1 >= gfp->strict_ISO );
666
667     return gfp->strict_ISO;
668 }
669  
670
671
672
673 /********************************************************************
674  * quantization/noise shaping 
675  ***********************************************************************/
676
677 /* Disable the bit reservoir. For testing only. */
678 int
679 lame_set_disable_reservoir( lame_global_flags*  gfp,
680                             int                 disable_reservoir )
681 {
682     /* default = 0 (disabled) */
683
684     /* enforce disable/enable meaning, if we need more than two values
685        we need to switch to an enum to have an apropriate representation
686        of the possible meanings of the value */
687     if ( 0 > disable_reservoir || 1 < disable_reservoir )
688         return -1;
689
690     gfp->disable_reservoir = disable_reservoir;
691
692     return 0;
693 }
694
695 int
696 lame_get_disable_reservoir( const lame_global_flags*  gfp )
697 {
698     assert( 0 <= gfp->disable_reservoir && 1 >= gfp->disable_reservoir );
699
700     return gfp->disable_reservoir;
701 }
702
703
704
705
706 /* Select a different "best quantization" function. default = 0 */
707 int
708 lame_set_experimentalX( lame_global_flags*  gfp,
709                         int                 experimentalX )
710 {
711     gfp->experimentalX = experimentalX;
712
713     return 0;
714 }
715
716 int
717 lame_get_experimentalX( const lame_global_flags*  gfp )
718 {
719     return gfp->experimentalX;
720 }
721
722
723 /* Another experimental option. For testing only. */
724 int
725 lame_set_experimentalY( lame_global_flags*  gfp,
726                         int                 experimentalY )
727 {
728     gfp->experimentalY = experimentalY;
729
730     return 0;
731 }
732
733 int
734 lame_get_experimentalY( const lame_global_flags*  gfp )
735 {
736     return gfp->experimentalY;
737 }
738
739
740 /* Another experimental option. For testing only. */
741 int
742 lame_set_experimentalZ( lame_global_flags*  gfp,
743                         int                 experimentalZ )
744 {
745     gfp->experimentalZ += experimentalZ;
746
747     return 0;
748 }
749
750 int
751 lame_get_experimentalZ( const lame_global_flags*  gfp )
752 {
753     return gfp->experimentalZ;
754 }
755
756
757 /* Naoki's psycho acoustic model. */
758 int
759 lame_set_exp_nspsytune( lame_global_flags*  gfp,
760                         int                 exp_nspsytune )
761 {
762     /* default = 0 (disabled) */
763
764     gfp->exp_nspsytune = exp_nspsytune;
765
766     return 0;
767 }
768
769 int
770 lame_get_exp_nspsytune( const lame_global_flags*  gfp )
771 {
772     return gfp->exp_nspsytune;
773 }
774
775
776
777
778 /********************************************************************
779  * VBR control
780  ***********************************************************************/
781
782 // Types of VBR.  default = vbr_off = CBR
783 int
784 lame_set_VBR( lame_global_flags*  gfp,
785               vbr_mode            VBR )
786 {
787     if( 0 > VBR || vbr_max_indicator <= VBR )
788         return -1;  /* Unknown VBR mode! */
789
790     gfp->VBR = VBR;
791
792     return 0;
793 }
794
795 vbr_mode
796 lame_get_VBR( const lame_global_flags*  gfp )
797 {
798     assert( 0 <= gfp->VBR && vbr_max_indicator > gfp->VBR );
799
800     return gfp->VBR;
801 }
802
803
804 /*
805  * VBR quality level.
806  *  0 = highest
807  *  9 = lowest 
808  */
809 int
810 lame_set_VBR_q( lame_global_flags*  gfp,
811                 int                 VBR_q )
812 {
813     /* XXX: This should be an enum */
814     /*  to whoever added this note: why should it be an enum?
815         do you want to call a specific setting by name? 
816         say VBR quality level red? */
817     /* No, but VBR_Q_HIGHEST, VBR_Q_HIGH, ..., VBR_Q_MID, ...
818        VBR_Q_LOW, VBR_Q_LOWEST (or something like that )and a
819        VBR_Q_DEFAULT, which aliases the default setting of
820        e.g. VBR_Q_MID. */
821
822
823     if( 0 > VBR_q || 10 <= VBR_q )
824         return -1;  /* Unknown VBR quality level! */
825
826     gfp->VBR_q = VBR_q;
827
828     return 0;
829 }
830
831 int
832 lame_get_VBR_q( const lame_global_flags*  gfp )
833 {
834     assert( 0 <= gfp->VBR_q && 10 > gfp->VBR_q );
835
836     return gfp->VBR_q;
837 }
838
839
840 /* Ignored except for VBR = vbr_abr (ABR mode) */
841 int
842 lame_set_VBR_mean_bitrate_kbps( lame_global_flags*  gfp,
843                                 int                 VBR_mean_bitrate_kbps )
844 {
845     gfp->VBR_mean_bitrate_kbps = VBR_mean_bitrate_kbps;
846
847     return 0;
848 }
849
850 int
851 lame_get_VBR_mean_bitrate_kbps( const lame_global_flags*  gfp )
852 {
853     return gfp->VBR_mean_bitrate_kbps;
854 }
855
856 int
857 lame_set_VBR_min_bitrate_kbps( lame_global_flags*  gfp,
858                                int                 VBR_min_bitrate_kbps )
859 {
860     gfp->VBR_min_bitrate_kbps = VBR_min_bitrate_kbps;
861
862     return 0;
863 }
864
865 int
866 lame_get_VBR_min_bitrate_kbps( const lame_global_flags*  gfp )
867 {
868     return gfp->VBR_min_bitrate_kbps;
869 }
870
871 int
872 lame_set_VBR_max_bitrate_kbps( lame_global_flags*  gfp,
873                                int                 VBR_max_bitrate_kbps )
874 {
875     gfp->VBR_max_bitrate_kbps = VBR_max_bitrate_kbps;
876
877     return 0;
878 }
879
880 int
881 lame_get_VBR_max_bitrate_kbps( const lame_global_flags*  gfp )
882 {
883     return gfp->VBR_max_bitrate_kbps;
884 }
885
886
887 /*
888  * Strictly enforce VBR_min_bitrate.
889  * Normally it will be violated for analog silence.
890  */
891 int
892 lame_set_VBR_hard_min( lame_global_flags*  gfp,
893                        int                 VBR_hard_min )
894 {
895     /* default = 0 (disabled) */
896
897     /* enforce disable/enable meaning, if we need more than two values
898        we need to switch to an enum to have an apropriate representation
899        of the possible meanings of the value */
900     if ( 0 > VBR_hard_min || 1 < VBR_hard_min )
901         return -1;
902
903     gfp->VBR_hard_min = VBR_hard_min;
904
905     return 0;
906 }
907
908 int
909 lame_get_VBR_hard_min( const lame_global_flags*  gfp )
910 {
911     assert( 0 <= gfp->VBR_hard_min && 1 >= gfp->VBR_hard_min );
912
913     return gfp->VBR_hard_min;
914 }
915
916
917 /********************************************************************
918  * Filtering control
919  ***********************************************************************/
920
921 /*
922  * Freqency in Hz to apply lowpass.
923  *   0 = default = lame chooses
924  *  -1 = disabled
925  */
926 int
927 lame_set_lowpassfreq( lame_global_flags*  gfp,
928                       int                 lowpassfreq )
929 {
930     gfp->lowpassfreq = lowpassfreq;
931
932     return 0;
933 }
934
935 int
936 lame_get_lowpassfreq( const lame_global_flags*  gfp )
937 {
938     return gfp->lowpassfreq;
939 }
940
941
942 /*
943  * Width of transition band (in Hz).
944  *  default = one polyphase filter band
945  */
946 int
947 lame_set_lowpasswidth( lame_global_flags*  gfp,
948                        int                 lowpasswidth )
949 {
950     gfp->lowpasswidth = lowpasswidth;
951
952     return 0;
953 }
954
955 int
956 lame_get_lowpasswidth( const lame_global_flags*  gfp )
957 {
958     return gfp->lowpasswidth;
959 }
960
961
962 /*
963  * Frequency in Hz to apply highpass.
964  *   0 = default = lame chooses
965  *  -1 = disabled
966  */
967 int
968 lame_set_highpassfreq( lame_global_flags*  gfp,
969                        int                 highpassfreq )
970 {
971     gfp->highpassfreq = highpassfreq;
972
973     return 0;
974 }
975
976 int
977 lame_get_highpassfreq( const lame_global_flags*  gfp )
978 {
979     return gfp->highpassfreq;
980 }
981
982
983 /*
984  * Width of transition band (in Hz).
985  *  default = one polyphase filter band
986  */
987 int
988 lame_set_highpasswidth( lame_global_flags*  gfp,
989                         int                 highpasswidth )
990 {
991     gfp->highpasswidth = highpasswidth;
992
993     return 0;
994 }
995
996 int
997 lame_get_highpasswidth( const lame_global_flags*  gfp )
998 {
999     return gfp->highpasswidth;
1000 }
1001
1002
1003
1004
1005 /*
1006  * psycho acoustics and other arguments which you should not change 
1007  * unless you know what you are doing
1008  */
1009
1010 /* Only use ATH for masking. */
1011 int
1012 lame_set_ATHonly( lame_global_flags*  gfp,
1013                   int                 ATHonly )
1014 {
1015     gfp->ATHonly = ATHonly;
1016
1017     return 0;
1018 }
1019
1020 int
1021 lame_get_ATHonly( const lame_global_flags*  gfp )
1022 {
1023     return gfp->ATHonly;
1024 }
1025
1026
1027 /* Only use ATH for short blocks. */
1028 int
1029 lame_set_ATHshort( lame_global_flags*  gfp,
1030                    int                 ATHshort )
1031 {
1032     gfp->ATHshort = ATHshort;
1033
1034     return 0;
1035 }
1036
1037 int
1038 lame_get_ATHshort( const lame_global_flags*  gfp )
1039 {
1040     return gfp->ATHshort;
1041 }
1042
1043
1044 /* Disable ATH. */
1045 int
1046 lame_set_noATH( lame_global_flags*  gfp,
1047                 int                 noATH )
1048 {
1049     gfp->noATH = noATH;
1050
1051     return 0;
1052 }
1053
1054 int
1055 lame_get_noATH( const lame_global_flags*  gfp )
1056 {
1057     return gfp->noATH;
1058 }
1059
1060
1061 /* Select ATH formula. */
1062 int
1063 lame_set_ATHtype( lame_global_flags*  gfp,
1064                   int                 ATHtype )
1065 {
1066     /* XXX: ATHtype should be converted to an enum. */
1067     gfp->ATHtype = ATHtype;
1068
1069     return 0;
1070 }
1071
1072 int
1073 lame_get_ATHtype( const lame_global_flags*  gfp )
1074 {
1075     return gfp->ATHtype;
1076 }
1077
1078
1079 /* Lower ATH by this many db. */
1080 int
1081 lame_set_ATHlower( lame_global_flags*  gfp,
1082                    float               ATHlower )
1083 {
1084     gfp->ATHlower = ATHlower;
1085     return 0;
1086 }
1087
1088 float
1089 lame_get_ATHlower( const lame_global_flags*  gfp )
1090 {
1091     return gfp->ATHlower;
1092 }
1093
1094
1095 /* Select ATH adaptive adjustment scheme. */
1096 int
1097 lame_set_athaa_type( lame_global_flags*  gfp,
1098                       int                athaa_type )
1099 {
1100     gfp->athaa_type = athaa_type;
1101
1102     return 0;
1103 }
1104
1105 int
1106 lame_get_athaa_type( const lame_global_flags*  gfp )
1107 {
1108     return gfp->athaa_type;
1109 }
1110
1111
1112 /* Select the loudness approximation used by the ATH adaptive auto-leveling. */
1113 int
1114 lame_set_athaa_loudapprox( lame_global_flags*  gfp,
1115                            int                 athaa_loudapprox )
1116 {
1117     gfp->athaa_loudapprox = athaa_loudapprox;
1118
1119     return 0;
1120 }
1121
1122 int
1123 lame_get_athaa_loudapprox( const lame_global_flags*  gfp )
1124 {
1125     return gfp->athaa_loudapprox;
1126 }
1127
1128
1129 /* Adjust (in dB) the point below which adaptive ATH level adjustment occurs. */
1130 int
1131 lame_set_athaa_sensitivity( lame_global_flags*  gfp,
1132                             float               athaa_sensitivity )
1133 {
1134     gfp->athaa_sensitivity = athaa_sensitivity;
1135
1136     return 0;
1137 }
1138
1139 float
1140 lame_get_athaa_sensitivity( const lame_global_flags*  gfp )
1141 {
1142     return gfp->athaa_sensitivity;
1143 }
1144
1145
1146 /* Predictability limit (ISO tonality formula) */
1147 int
1148 lame_set_cwlimit( lame_global_flags*  gfp,
1149                   int                 cwlimit )
1150 {
1151     gfp->cwlimit = cwlimit;
1152
1153     return 0;
1154 }
1155
1156 int
1157 lame_get_cwlimit( const lame_global_flags*  gfp )
1158 {
1159     return gfp->cwlimit;
1160 }
1161
1162
1163
1164 /*
1165  * Allow blocktypes to differ between channels.
1166  * default:
1167  *  0 for jstereo => block types coupled
1168  *  1 for stereo  => block types may differ
1169  */
1170 int
1171 lame_set_allow_diff_short( lame_global_flags*  gfp,
1172                            int                 allow_diff_short )
1173 {
1174     gfp->short_blocks = 
1175         allow_diff_short ? short_block_allowed : short_block_coupled;
1176
1177     return 0;
1178 }
1179
1180 int
1181 lame_get_allow_diff_short( const lame_global_flags*  gfp )
1182 {
1183     if ( gfp->short_blocks == short_block_allowed ) 
1184         return 1; /* short blocks allowed to differ */
1185     else 
1186         return 0; /* not set, dispensed, forced or coupled */
1187 }
1188
1189
1190 /* Use temporal masking effect */
1191 int
1192 lame_set_useTemporal( lame_global_flags*  gfp,
1193                       int                 useTemporal )
1194 {
1195     /* default = 1 (enabled) */
1196
1197     /* enforce disable/enable meaning, if we need more than two values
1198        we need to switch to an enum to have an apropriate representation
1199        of the possible meanings of the value */
1200     if ( 0 > useTemporal || 1 < useTemporal )
1201         return -1;
1202
1203     gfp->useTemporal = useTemporal;
1204
1205     return 0;
1206 }
1207
1208 int
1209 lame_get_useTemporal( const lame_global_flags*  gfp )
1210 {
1211     assert( 0 <= gfp->useTemporal && 1 >= gfp->useTemporal );
1212
1213     return gfp->useTemporal;
1214 }
1215
1216
1217 /* Disable short blocks. */
1218 int
1219 lame_set_no_short_blocks( lame_global_flags*  gfp,
1220                           int                 no_short_blocks )
1221 {
1222     /* enforce disable/enable meaning, if we need more than two values
1223        we need to switch to an enum to have an apropriate representation
1224        of the possible meanings of the value */
1225     if ( 0 > no_short_blocks || 1 < no_short_blocks )
1226         return -1;
1227
1228     gfp->short_blocks = 
1229         no_short_blocks ? short_block_dispensed : short_block_allowed;
1230
1231     return 0;
1232 }
1233 int
1234 lame_get_no_short_blocks( const lame_global_flags*  gfp )
1235 {
1236     switch ( gfp->short_blocks ) {
1237     default:
1238     case short_block_not_set:   return -1;
1239     case short_block_dispensed: return 1;
1240     case short_block_allowed:
1241     case short_block_coupled:
1242     case short_block_forced:    return 0;
1243     }
1244 }
1245
1246
1247 /* Force short blocks. */
1248 int
1249 lame_set_force_short_blocks( lame_global_flags*  gfp,
1250                           int                 short_blocks )
1251 {
1252     /* enforce disable/enable meaning, if we need more than two values
1253        we need to switch to an enum to have an apropriate representation
1254        of the possible meanings of the value */
1255     if ( 0 > short_blocks || 1 < short_blocks )
1256         return -1;
1257
1258     if (short_blocks == 1)
1259         gfp->short_blocks = short_block_forced;
1260     else if (gfp->short_blocks == short_block_forced) 
1261         gfp->short_blocks = short_block_allowed;
1262
1263     return 0;
1264 }
1265 int
1266 lame_get_force_short_blocks( const lame_global_flags*  gfp )
1267 {
1268     switch ( gfp->short_blocks ) {
1269     default:
1270     case short_block_not_set:   return -1;
1271     case short_block_dispensed: 
1272     case short_block_allowed:
1273     case short_block_coupled:   return 0;
1274     case short_block_forced:    return 1;
1275     }
1276 }
1277
1278
1279 /*
1280  * Input PCM is emphased PCM
1281  * (for instance from one of the rarely emphased CDs).
1282  *
1283  * It is STRONGLY not recommended to use this, because psycho does not
1284  * take it into account, and last but not least many decoders
1285  * ignore these bits
1286  */
1287 int
1288 lame_set_emphasis( lame_global_flags*  gfp,
1289                    int                 emphasis )
1290 {
1291     /* XXX: emphasis should be converted to an enum */
1292     if ( 0 > emphasis || 4 <= emphasis )
1293         return -1;
1294
1295     gfp->emphasis = emphasis;
1296
1297     return 0;
1298 }
1299
1300 int
1301 lame_get_emphasis( const lame_global_flags*  gfp )
1302 {
1303     assert( 0 <= gfp->emphasis && 4 > gfp->emphasis );
1304
1305     return gfp->emphasis;
1306 }
1307
1308
1309
1310
1311 /***************************************************************/
1312 /* internal variables, cannot be set...                        */
1313 /* provided because they may be of use to calling application  */
1314 /***************************************************************/
1315
1316 /* MPEG version.
1317  *  0 = MPEG-2
1318  *  1 = MPEG-1
1319  * (2 = MPEG-2.5)    
1320  */
1321 int
1322 lame_get_version( const lame_global_flags* gfp )
1323 {
1324     return gfp->version;
1325 }
1326
1327
1328 /* Encoder delay. */
1329 int
1330 lame_get_encoder_delay( const lame_global_flags*  gfp )
1331 {
1332     return gfp->encoder_delay;
1333 }
1334
1335 /* padding added to the end of the input */
1336 int
1337 lame_get_encoder_padding( const lame_global_flags*  gfp )
1338 {
1339     return gfp->encoder_padding;
1340 }
1341
1342
1343 /* Size of MPEG frame. */
1344 int
1345 lame_get_framesize( const lame_global_flags*  gfp )
1346 {
1347     return gfp->framesize;
1348 }
1349
1350
1351 /* Number of frames encoded so far. */
1352 int
1353 lame_get_frameNum( const lame_global_flags*  gfp )
1354 {
1355     return gfp->frameNum;
1356 }
1357
1358 int
1359 lame_get_mf_samples_to_encode( const lame_global_flags*  gfp )
1360 {
1361     lame_internal_flags *gfc = gfp->internal_flags;
1362     return gfc->mf_samples_to_encode;
1363 }
1364
1365
1366 int CDECL lame_get_size_mp3buffer( const lame_global_flags*  gfp )
1367 {
1368     int size;
1369     compute_flushbits(gfp,&size);
1370     return size;
1371 }
1372
1373
1374
1375 /*
1376  * LAME's estimate of the total number of frames to be encoded.
1377  * Only valid if calling program set num_samples.
1378  */
1379 int
1380 lame_get_totalframes( const lame_global_flags*  gfp )
1381 {
1382     int totalframes;
1383     /* estimate based on user set num_samples: */
1384     totalframes =
1385         2 + ((double)gfp->num_samples * gfp->out_samplerate) / 
1386               ((double)gfp->in_samplerate * gfp->framesize);
1387
1388     /* check to see if we underestimated totalframes */
1389     //    if (totalframes < gfp->frameNum)
1390     //        totalframes = gfp->frameNum;
1391
1392     return totalframes;
1393 }
1394
1395
1396 /*
1397
1398 UNDOCUMENTED, experimental settings.  These routines are not prototyped
1399 in lame.h.  You should not use them, they are experimental and may
1400 change.  
1401
1402 */
1403
1404
1405 /*
1406  *  just another daily changing developer switch  
1407  */
1408 void lame_set_tune( lame_global_flags* gfp, float val ) 
1409 {
1410     gfp->tune_value_a = val;
1411     gfp->tune = 1;
1412 }
1413
1414 /* Custom msfix hack */
1415 void
1416 lame_set_msfix( lame_global_flags*  gfp, double msfix )
1417 {
1418     /* default = 0 */
1419     gfp->msfix = msfix;
1420
1421 }
1422
1423 int
1424 lame_set_preset_expopts( lame_global_flags*  gfp, int preset_expopts )
1425 {
1426
1427     lame_internal_flags *gfc = gfp->internal_flags;
1428
1429     gfc->presetTune.use = 1;
1430
1431     /* default = 0 (disabled) */
1432     gfp->preset_expopts = preset_expopts;
1433
1434     switch (preset_expopts)
1435     {
1436         case 1:
1437
1438           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1439           lame_set_experimentalX(gfp, 3);
1440           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2); // safejoint
1441           lame_set_ATHtype(gfp, 2);
1442
1443           gfc->presetTune.attackthre   = 35;
1444           gfc->presetTune.attackthre_s = 150;
1445           gfc->presetTune.ms_maskadjust = .5;
1446                   gfc->presetTune.quantcomp_type_s = 3;
1447           gfc->presetTune.quantcomp_alt_type = 3;
1448           gfc->presetTune.athadjust_switch_level = 2; // Always switch
1449
1450           break;
1451
1452         case 2:
1453
1454           if (gfp->VBR == vbr_mtrh) {
1455              lame_set_experimentalX(gfp, 2);
1456              gfc->presetTune.quantcomp_adjust_mtrh = 9;
1457              gfc->presetTune.quantcomp_type_s = 4;
1458              gfc->presetTune.quantcomp_alt_type = 0;
1459              gfc->presetTune.athadjust_safe_noiseshaping_thre = 0.0;
1460                          gfc->presetTune.athadjust_safe_athaasensitivity = 8.0;
1461           }
1462           else {
1463              lame_set_experimentalX(gfp, 3);
1464              gfc->presetTune.quantcomp_adjust_rh_tot = 600;
1465                          gfc->presetTune.quantcomp_adjust_rh_max = 60;
1466              gfc->presetTune.quantcomp_type_s = 3;
1467              gfc->presetTune.quantcomp_alt_type = 1;
1468           }
1469
1470           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1471           lame_set_experimentalZ(gfp, 1);
1472           lame_set_VBR_q(gfp, 2);
1473           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2); // safejoint
1474           lame_set_ATHtype(gfp, 2);                             
1475           // modify sfb21 by 3 dB plus ns-treble=0                 
1476           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (12 << 20));
1477
1478           gfc->presetTune.attackthre   = 35;
1479           gfc->presetTune.attackthre_s = 150;
1480           gfc->presetTune.ms_maskadjust = .5;
1481           gfc->presetTune.athadjust_switch_level = 1;
1482           gfc->presetTune.athadjust_msfix = 2.13;
1483
1484           break;
1485
1486         case 3:
1487
1488           if (gfp->VBR == vbr_mtrh) {
1489              gfc->presetTune.quantcomp_type_s = 4;
1490              gfc->presetTune.quantcomp_adjust_mtrh = 9;
1491                          gfc->presetTune.quantcomp_alt_type = 0;
1492              (void) lame_set_ATHlower( gfp, -2 );
1493              gfc->presetTune.athadjust_safe_noiseshaping_thre = 0.0;
1494                          gfc->presetTune.athadjust_safe_athaasensitivity = 8.0;
1495           }
1496           else {
1497              gfc->presetTune.quantcomp_type_s = 3;
1498              gfc->presetTune.quantcomp_adjust_rh_tot = 600;
1499                          gfc->presetTune.quantcomp_adjust_rh_max = 60;
1500              (void) lame_set_ATHlower( gfp, -1 );
1501           }
1502
1503           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1504           lame_set_experimentalZ(gfp, 1);
1505           lame_set_experimentalX(gfp, 1);
1506           lame_set_VBR_q(gfp, 2);
1507           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2); // safejoint
1508    (void) lame_set_msfix( gfp, 2.13 );
1509           lame_set_ATHtype(gfp, 4);
1510           // modify sfb21 by 3.75 dB plus ns-treble=0                 
1511           lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (15 << 20));
1512
1513           gfc->presetTune.attackthre   = 35;
1514           gfc->presetTune.attackthre_s = 150;
1515           gfc->presetTune.ms_maskadjust = .5;
1516           gfc->presetTune.athadjust_switch_level = 1;
1517
1518           break;
1519     }
1520     return 0;
1521 }
1522
1523 int
1524 lame_set_preset_notune( lame_global_flags*  gfp, int preset_notune )
1525 {
1526     lame_internal_flags *gfc = gfp->internal_flags;
1527
1528     gfc->presetTune.use = 0;  // Turn off specialized preset tunings
1529
1530     return 0;
1531 }
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550