Skip to content

Instantly share code, notes, and snippets.

@njh
Created January 2, 2011 13:06
Show Gist options
  • Save njh/762513 to your computer and use it in GitHub Desktop.
Save njh/762513 to your computer and use it in GitHub Desktop.
Patch to using floats for internal buffers in twolame
diff -Naur twolame/libtwolame/common.h twolame-patched/libtwolame/common.h
--- twolame/libtwolame/common.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/common.h Wed Jul 25 15:08:05 2007
@@ -79,7 +79,8 @@
#define HAN_SIZE 512
#define SCALE_BLOCK 12
#define SCALE_RANGE 64
-#define SCALE 32768
+#define SCALE 1
+#define DOWN_SCALE (1.0 / 32768.0)
#define CRC16_POLYNOMIAL 0x8005
#define CRC8_POLYNOMIAL 0x1D
@@ -335,7 +336,7 @@
// Used by twolame_encode_frame
int twolame_init;
- short int buffer[2][TWOLAME_SAMPLES_PER_FRAME]; // Sample buffer
+ FLOAT bufferF[2][TWOLAME_SAMPLES_PER_FRAME]; // Sample buffer
unsigned int samples_in_buffer; // Number of samples currently in buffer
unsigned int psycount;
unsigned int num_crc_bits; // Number of bits CRC is calculated on
diff -Naur twolame/libtwolame/energy.c twolame-patched/libtwolame/energy.c
--- twolame/libtwolame/energy.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/energy.c Wed Jul 25 15:08:05 2007
@@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#include "twolame.h"
#include "common.h"
@@ -68,10 +69,11 @@
you'll be overwriting mpeg audio data)
*/
- short int *leftpcm = glopts->buffer[0];
- short int *rightpcm = glopts->buffer[1];
+ FLOAT *leftpcm = glopts->bufferF[0];
+ FLOAT *rightpcm = glopts->bufferF[1];
- int i, leftMax, rightMax;
+ int i;
+ FLOAT leftMax, rightMax;
unsigned char rhibyte, rlobyte, lhibyte, llobyte;
// Get the position (in butes) of the end of the mpeg audio frame
@@ -81,26 +83,26 @@
// find the maximum in the left and right channels
leftMax = rightMax = -1;
for (i=0; i<TWOLAME_SAMPLES_PER_FRAME; i++) {
- if (abs(leftpcm[i])>leftMax)
- leftMax = abs(leftpcm[i]);
- if (abs(rightpcm[i])>rightMax)
- rightMax = abs(rightpcm[i]);
+ if (fabs(leftpcm[i])>leftMax)
+ leftMax = fabs(leftpcm[i]);
+ if (fabs(rightpcm[i])>rightMax)
+ rightMax = fabs(rightpcm[i]);
}
// fix any overflows
- if (leftMax > 32767)
- leftMax = 32767;
+ if (leftMax > 1.0)
+ leftMax = 1.0;
- if (rightMax > 32767)
- rightMax = 32767;
+ if (rightMax > 1.0)
+ rightMax = 1.0;
// convert max value to hi/lo bytes and write into buffer
- lhibyte = leftMax/256;
- llobyte = leftMax - 256*lhibyte;
+ lhibyte = (int)(leftMax*256.0*128.0);
+ llobyte = (int)(leftMax*128.0 - 256*lhibyte);
// Write the left channel into the last two bytes of the frame
bs->buf[frameEnd-1] = llobyte;
@@ -111,8 +113,8 @@
// if we're in stereo mode.
if (glopts->mode!=TWOLAME_MONO) {
- rhibyte = rightMax/256;
- rlobyte = rightMax - 256*rhibyte;
+ rhibyte = (int)(rightMax*256.0*128.0);
+ rlobyte = (int)(rightMax*128.0 - 256*rhibyte);
bs->buf[frameEnd-4] = rlobyte;
bs->buf[frameEnd-5] = rhibyte;
@@ -123,4 +125,5 @@
// vim:ts=4:sw=4:nowrap:
+
diff -Naur twolame/libtwolame/psycho_1.c twolame-patched/libtwolame/psycho_1.c
--- twolame/libtwolame/psycho_1.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_1.c Wed Jul 25 15:08:05 2007
@@ -562,7 +562,7 @@
*/
-void psycho_1 (twolame_options *glopts, short buffer[2][1152], FLOAT scale[2][SBLIMIT],
+void psycho_1 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT scale[2][SBLIMIT],
FLOAT ltmin[2][SBLIMIT])
{
psycho_1_mem *mem;
@@ -612,7 +612,7 @@
saves about 4% overall during an encode */
int ok = mem->off[k] % 1408;
for (i = 0; i < 1152; i++) {
- fft_buf[k][ok++] = (FLOAT) buffer[k][i] / SCALE;
+ fft_buf[k][ok++] = (FLOAT) bufferF[k][i] / SCALE;
if (ok >= 1408)
ok = 0;
}
diff -Naur twolame/libtwolame/psycho_1.h twolame-patched/libtwolame/psycho_1.h
--- twolame/libtwolame/psycho_1.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_1.h Wed Jul 25 15:08:05 2007
@@ -25,7 +25,7 @@
#ifndef _PSYCHO_1_H_
#define _PSYCHO_1_H_
-void psycho_1 (twolame_options *glopts, short int buffer[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32]);
+void psycho_1 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32]);
void psycho_1_deinit(psycho_1_mem **mem);
#endif
diff -Naur twolame/libtwolame/psycho_2.c twolame-patched/libtwolame/psycho_2.c
--- twolame/libtwolame/psycho_2.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_2.c Wed Jul 25 15:08:05 2007
@@ -255,8 +255,8 @@
return(mem);
}
-void psycho_2 (twolame_options *glopts, short int buffer[2][1152],
- short int savebuf[2][1056],
+void psycho_2 (twolame_options *glopts, FLOAT bufferF[2][1152],
+ FLOAT savebufF[2][1056],
FLOAT smr[2][32])
{
psycho_2_mem *mem;
@@ -335,17 +335,17 @@
BLKSIZE = 1024
*****************************************************************************/
{
- short int *bufferp = buffer[ch];
+ FLOAT *bufferpF = bufferF[ch];
for (j = 0; j < 480; j++) {
- savebuf[ch][j] = savebuf[ch][j + mem->flush];
- wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
+ savebufF[ch][j] = savebufF[ch][j + mem->flush];
+ wsamp_r[j] = window[j] * ((FLOAT) savebufF[ch][j]);
}
for (; j < 1024; j++) {
- savebuf[ch][j] = *bufferp++;
- wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
+ savebufF[ch][j] = *bufferpF++;
+ wsamp_r[j] = window[j] * ((FLOAT) savebufF[ch][j]);
}
for (; j < 1056; j++)
- savebuf[ch][j] = *bufferp++;
+ savebufF[ch][j] = *bufferpF++;
}
/**Compute FFT****************************************************************/
diff -Naur twolame/libtwolame/psycho_2.h twolame-patched/libtwolame/psycho_2.h
--- twolame/libtwolame/psycho_2.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_2.h Wed Jul 25 15:08:05 2007
@@ -25,7 +25,7 @@
#ifndef _PSYCHO_2_H_
#define _PSYCHO_2_H_
-void psycho_2 (twolame_options *glopts, short int buffer[2][1152], short int savebuf[2][1056], FLOAT smr[2][32]);
+void psycho_2 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT savebuf[2][1056], FLOAT smr[2][32]);
void psycho_2_deinit(psycho_2_mem **mem);
#endif
diff -Naur twolame/libtwolame/psycho_3.c twolame-patched/libtwolame/psycho_3.c
--- twolame/libtwolame/psycho_3.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_3.c Wed Jul 25 15:08:05 2007
@@ -520,7 +520,7 @@
}
-void psycho_3 (twolame_options *glopts, short int buffer[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32])
+void psycho_3 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32])
{
psycho_3_mem *mem;
int nch = glopts->num_channels_out;
@@ -542,7 +542,7 @@
for (k = 0; k < nch; k++) {
int ok = mem->off[k] % 1408;
for (i = 0; i < 1152; i++) {
- mem->fft_buf[k][ok++] = (FLOAT) buffer[k][i] / SCALE;
+ mem->fft_buf[k][ok++] = (FLOAT) bufferF[k][i] / SCALE;
if (ok >= 1408)
ok = 0;
}
diff -Naur twolame/libtwolame/psycho_3.h twolame-patched/libtwolame/psycho_3.h
--- twolame/libtwolame/psycho_3.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_3.h Wed Jul 25 15:08:05 2007
@@ -25,7 +25,7 @@
#ifndef _PSYCHO_3_H_
#define _PSYCHO_3_H_
-void psycho_3 (twolame_options *glopts, short int buffer[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32]);
+void psycho_3 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT scale[2][32], FLOAT ltmin[2][32]);
void psycho_3_deinit(psycho_3_mem **mem);
#endif
diff -Naur twolame/libtwolame/psycho_4.c twolame-patched/libtwolame/psycho_4.c
--- twolame/libtwolame/psycho_4.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_4.c Wed Jul 25 15:08:05 2007
@@ -300,8 +300,8 @@
void psycho_4 (twolame_options *glopts,
- short int buffer[2][1152],
- short int savebuf[2][1056],
+ FLOAT bufferF[2][1152],
+ FLOAT savebufF[2][1056],
FLOAT smr[2][32])
/* to match prototype : FLOAT args are always FLOAT */
{
@@ -374,17 +374,17 @@
sync_flush = syncsize - flush; 480
BLKSIZE = 1024 */
{
- short int *bufferp = buffer[ch];
+ FLOAT *bufferp = bufferF[ch];
for (j = 0; j < 480; j++) {
- savebuf[ch][j] = savebuf[ch][j + 576];
- wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
+ savebufF[ch][j] = savebufF[ch][j + 576];
+ wsamp_r[j] = window[j] * ((FLOAT) savebufF[ch][j]);
}
for (; j < 1024; j++) {
- savebuf[ch][j] = *bufferp++;
- wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
+ savebufF[ch][j] = *bufferp++;
+ wsamp_r[j] = window[j] * ((FLOAT) savebufF[ch][j]);
}
for (; j < 1056; j++)
- savebuf[ch][j] = *bufferp++;
+ savebufF[ch][j] = *bufferp++;
}
/* Compute FFT */
diff -Naur twolame/libtwolame/psycho_4.h twolame-patched/libtwolame/psycho_4.h
--- twolame/libtwolame/psycho_4.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/psycho_4.h Wed Jul 25 15:08:05 2007
@@ -25,7 +25,7 @@
#ifndef _PSYCHO_4_H_
#define _PSYCHO_4_H_
-void psycho_4 (twolame_options *glopts, short int buffer[2][1152], short int savebuf[2][1056], FLOAT smr[2][32]);
+void psycho_4 (twolame_options *glopts, FLOAT bufferF[2][1152], FLOAT savebuf[2][1056], FLOAT smr[2][32]);
void psycho_4_deinit(psycho_4_mem **mem);
#endif
diff -Naur twolame/libtwolame/subband.c twolame-patched/libtwolame/subband.c
--- twolame/libtwolame/subband.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/subband.c Wed Jul 25 15:08:05 2007
@@ -41,10 +41,11 @@
for (i = 0; i < 16; i++)
for (k = 0; k < 32; k++) {
- if ((filter[i][k] = 1e9 * cos ((FLOAT) ((2 * i + 1) * k * PI64))) >= 0)
- modf (filter[i][k] + 0.5, &filter[i][k]);
- else
- modf (filter[i][k] - 0.5, &filter[i][k]);
+ if ((filter[i][k] = 1e9 * cos ((FLOAT) ((2 * i + 1) * k * PI64))) >= 0) {
+ filter[i][k] = (int)(filter[i][k] + 0.5);
+ } else {
+ filter[i][k] = (int)(filter[i][k] - 0.5);
+ }
filter[i][k] *= 1e-9;
}
}
@@ -65,7 +66,7 @@
}
-void window_filter_subband (subband_mem *smem, short *pBuffer, int ch, FLOAT s[SBLIMIT])
+void window_filter_subband (subband_mem *smem, FLOAT *pBufferF, int ch, FLOAT s[SBLIMIT])
{
register int i, j;
int pa, pb, pc, pd, pe, pf, pg, ph;
@@ -79,7 +80,7 @@
/* replace 32 oldest samples with 32 new samples */
for (i = 0; i < 32; i++)
- dp[(31 - i) * 8] = (FLOAT) pBuffer[i] / SCALE;
+ dp[(31 - i) * 8] = (FLOAT) pBufferF[i] / SCALE;
// looks like "school example" but does faster ...
dp = (smem->x[ch] + smem->half[ch] * 256);
diff -Naur twolame/libtwolame/subband.h twolame-patched/libtwolame/subband.h
--- twolame/libtwolame/subband.h Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/subband.h Wed Jul 25 15:08:05 2007
@@ -27,7 +27,7 @@
#define _SUBBAND_H_
int init_subband (subband_mem *smem);
-void window_filter_subband(subband_mem *smem, short *pBuffer, int ch, FLOAT s[SBLIMIT]);
+void window_filter_subband(subband_mem *smem, FLOAT *pBuffer, int ch, FLOAT s[SBLIMIT]);
#endif
diff -Naur twolame/libtwolame/twolame.c twolame-patched/libtwolame/twolame.c
--- twolame/libtwolame/twolame.c Thu Jul 26 12:10:05 2007
+++ twolame-patched/libtwolame/twolame.c Wed Jul 25 15:08:05 2007
@@ -326,7 +326,7 @@
glopts->sb_sample = (sb_sample_t *) TWOLAME_MALLOC(sizeof (sb_sample_t));
// clear buffers
- memset ((char *) glopts->buffer, 0, sizeof(glopts->buffer));
+ memset ((char *) glopts->bufferF, 0, sizeof(glopts->bufferF));
memset ((char *) glopts->bit_alloc, 0, sizeof (glopts->bit_alloc));
memset ((char *) glopts->scfsi, 0, sizeof (glopts->scfsi));
memset ((char *) glopts->scalar, 0, sizeof (glopts->scalar));
@@ -359,38 +359,38 @@
// apply scaling to both channels
if (glopts->scale != 0 && glopts->scale != 1.0) {
for (i=0 ; i<num_samples; ++i) {
- glopts->buffer[0][i] *= glopts->scale;
+ glopts->bufferF[0][i] *= glopts->scale;
if (glopts->num_channels_in == 2)
- glopts->buffer[1][i] *= glopts->scale;
+ glopts->bufferF[1][i] *= glopts->scale;
}
}
// apply scaling to channel 0 (left)
if (glopts->scale_left != 0 && glopts->scale_left != 1.0) {
for (i=0 ; i<num_samples; ++i) {
- glopts->buffer[0][i] *= glopts->scale_left;
+ glopts->bufferF[0][i] *= glopts->scale_left;
}
}
// apply scaling to channel 1 (right)
if (glopts->scale_right != 0 && glopts->scale_right != 1.0) {
for (i=0 ; i<num_samples; ++i) {
- glopts->buffer[1][i] *= glopts->scale_right;
+ glopts->bufferF[1][i] *= glopts->scale_right;
}
}
// Downmix to Mono if 2 channels in and 1 channel out
if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1) {
for (i=0; i<num_samples; ++i) {
- glopts->buffer[0][i] = ((long) glopts->buffer[0][i] + glopts->buffer[1][i]) / 2;
- glopts->buffer[1][i] = 0;
+ glopts->bufferF[0][i] = (glopts->bufferF[0][i] + glopts->bufferF[1][i]) * 0.5f;
+ glopts->bufferF[1][i] = 0;
}
}
// Upmix to Stereo if 2 channels out and 1 channel in
if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2) {
for (i=0; i<num_samples; ++i) {
- glopts->buffer[1][i] = glopts->buffer[0][i];
+ glopts->bufferF[1][i] = glopts->bufferF[0][i];
}
}
@@ -410,7 +410,7 @@
int nch = glopts->num_channels_out;
int sb, ch, adb, i;
unsigned long frameBits, initial_bits;
- short sam[2][1056];
+ FLOAT sam[2][1056];
if (!glopts->twolame_init) {
fprintf (stderr, "Please call twolame_init_params() before starting encoding.\n");
@@ -473,7 +473,7 @@
for( gr = 0; gr < 3; gr++ )
for ( bl = 0; bl < 12; bl++ )
for ( ch = 0; ch < nch; ch++ )
- window_filter_subband( &glopts->smem, &glopts->buffer[ch][gr * 12 * 32 + 32 * bl], ch,
+ window_filter_subband( &glopts->smem, &glopts->bufferF[ch][gr * 12 * 32 + 32 * bl], ch,
&(*glopts->sb_sample)[ch][gr][bl][0] );
}
@@ -503,18 +503,18 @@
psycho_0 (glopts, glopts->smr, glopts->scalar);
break;
case 1:
- psycho_1 (glopts, glopts->buffer, glopts->max_sc, glopts->smr);
+ psycho_1 (glopts, glopts->bufferF, glopts->max_sc, glopts->smr);
break;
case 2:
- psycho_2 (glopts, glopts->buffer, sam, glopts->smr );
+ psycho_2 (glopts, glopts->bufferF, sam, glopts->smr );
break;
case 3:
// Modified psy model 1
- psycho_3 (glopts, glopts->buffer, glopts->max_sc, glopts->smr);
+ psycho_3 (glopts, glopts->bufferF, glopts->max_sc, glopts->smr);
break;
case 4:
// Modified psy model 2
- psycho_4 (glopts, glopts->buffer, sam, glopts->smr );
+ psycho_4 (glopts, glopts->bufferF, sam, glopts->smr );
break;
default:
fprintf (stderr, "Invalid psy model specification: %i\n", glopts->psymodel);
@@ -639,9 +639,9 @@
/* Copy across samples */
for(i=0; i<samples_to_copy; i++) {
- glopts->buffer[0][glopts->samples_in_buffer+i] = *leftpcm++;
+ glopts->bufferF[0][glopts->samples_in_buffer+i] = (FLOAT)(*leftpcm++) * DOWN_SCALE;
if (glopts->num_channels_in==2)
- glopts->buffer[1][glopts->samples_in_buffer+i] = *rightpcm++;
+ glopts->bufferF[1][glopts->samples_in_buffer+i] = (FLOAT)(*rightpcm++) * DOWN_SCALE;
}
@@ -696,9 +696,9 @@
/* Copy across samples */
for(i=0; i<samples_to_copy; i++) {
- glopts->buffer[0][glopts->samples_in_buffer+i] = *pcm++;
+ glopts->bufferF[0][glopts->samples_in_buffer+i] = (FLOAT)(*pcm++)* DOWN_SCALE;
if (glopts->num_channels_in==2)
- glopts->buffer[1][glopts->samples_in_buffer+i] = *pcm++;
+ glopts->bufferF[1][glopts->samples_in_buffer+i] = (FLOAT)(*pcm++) * DOWN_SCALE;
}
@@ -768,6 +768,7 @@
{
int mp2_size=0;
bit_stream *mybs;
+ int i;
if (num_samples==0) return 0;
@@ -783,14 +784,15 @@
// fill up glopts->buffer with as much as we can
int samples_to_copy = TWOLAME_SAMPLES_PER_FRAME - glopts->samples_in_buffer;
if (num_samples < samples_to_copy) samples_to_copy = num_samples;
-
+
/* Copy across samples */
- float32_to_short( leftpcm, &glopts->buffer[0][glopts->samples_in_buffer], samples_to_copy, 1 );
- if (glopts->num_channels_in==2)
- float32_to_short( rightpcm, &glopts->buffer[1][glopts->samples_in_buffer], samples_to_copy, 1 );
- leftpcm+=samples_to_copy;
- rightpcm+=samples_to_copy;
-
+ for (i=0; i<samples_to_copy; i++) {
+ glopts->bufferF[0][glopts->samples_in_buffer+i] = (FLOAT)(*leftpcm++);
+ if (glopts->num_channels_in==2)
+ glopts->bufferF[1][glopts->samples_in_buffer+i] = (FLOAT)(*rightpcm++);
+ }
+
+
/* Update sample counts */
glopts->samples_in_buffer += samples_to_copy;
num_samples -= samples_to_copy;
@@ -824,6 +826,7 @@
{
int mp2_size=0;
bit_stream *mybs;
+ int i;
if (num_samples==0) return 0;
@@ -840,10 +843,11 @@
if (num_samples < samples_to_copy) samples_to_copy = num_samples;
/* Copy across samples */
- float32_to_short( pcm, &glopts->buffer[0][glopts->samples_in_buffer], samples_to_copy, glopts->num_channels_in );
- if (glopts->num_channels_in==2)
- float32_to_short( pcm+1, &glopts->buffer[1][glopts->samples_in_buffer], samples_to_copy, glopts->num_channels_in );
- pcm+=(samples_to_copy*glopts->num_channels_in);
+ for(i=0; i<samples_to_copy; i++) {
+ glopts->bufferF[0][glopts->samples_in_buffer+i] = (FLOAT)(*pcm++);
+ if (glopts->num_channels_in==2)
+ glopts->bufferF[1][glopts->samples_in_buffer+i] = (FLOAT)(*pcm++);
+ }
/* Update sample counts */
@@ -888,7 +892,7 @@
// Pad out the PCM buffers with 0 and encode the frame
for (i=glopts->samples_in_buffer; i< TWOLAME_SAMPLES_PER_FRAME; i++) {
- glopts->buffer[0][i] = glopts->buffer[1][i] = 0;
+ glopts->bufferF[0][i] = glopts->bufferF[1][i] = 0.0;
}
// Encode the frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment