/*------------------------------------------------------------------------------ * Write data to the encoder *----------------------------------------------------------------------------*/ unsigned int LameLibEncoder :: write ( const void * buf, unsigned int len ) throw ( Exception ) { if ( !isOpen() ) { return 0; } unsigned int bitsPerSample = getInBitsPerSample(); unsigned int channels = getInChannel(); if ( channels != 1 && channels != 2 ) { throw Exception( __FILE__, __LINE__, "unsupported number of channels for the encoder", channels ); } unsigned int sampleSize = (bitsPerSample / 8) * channels; unsigned char * b = (unsigned char*) buf; unsigned int processed = len - (len % sampleSize); unsigned int nSamples = processed / sampleSize; short int leftBuffer[nSamples]; short int rightBuffer[nSamples]; if ( bitsPerSample == 8 ) { conv8( b, processed, leftBuffer, rightBuffer, channels); } else if ( bitsPerSample == 16 ) { conv16( b, processed, leftBuffer, rightBuffer, channels); } else { throw Exception( __FILE__, __LINE__, "unsupported number of bits per sample for the encoder", bitsPerSample ); } // data chunk size estimate according to lame documentation unsigned int mp3Size = (unsigned int) (1.25 * nSamples + 7200); unsigned char mp3Buf[mp3Size]; int ret; ret = lame_encode_buffer( lameGlobalFlags, leftBuffer, channels == 2 ? rightBuffer : leftBuffer, nSamples, mp3Buf, mp3Size ); if ( ret < 0 ) { reportEvent( 3, "lame encoding error", ret); return 0; } unsigned int written = sink->write( mp3Buf, ret); // just let go data that could not be written if ( written < (unsigned int) ret ) { reportEvent( 2, "couldn't write all from encoder to underlying sink", ret - written); } return processed; }
// ============================================================ // Author : Scott Kirkwood // Date : Tue Nov 10 1992 // // Prototype : iformat.h // Description : // Main routine, handles output of unsigned longs. // Note: setting width to anything but 0 causes a big speed penalty. // const char *IFormat::Str(unsigned long num, int neg) { int as = 0; int old_putseps = putseps; // Commas will move the string to the right need 2 bytes for // the potential SI postfix and the null. char *last = str + sizeof(str) - IFormatMaxCommas - 2; char *first; int len = 0; if (width) { if (use_si) { int tempwidth = width; register unsigned long tempnum = num; while (as < IFormatMAXSI - 1 && Size(tempnum, neg) > tempwidth) { if (as == 0) tempwidth--; // because of the postfix character as++; tempnum = num / divby[as]; } num = tempnum; } else { if (putseps && Size(num, neg) > width) { putseps = 0; if (Size(num, neg) > width) putseps = old_putseps; } } } // The converter routines grows the string backwards from the least sig. // digit to the m.s. digit. No null is placed. switch (mode) { case ios::hex: first = conv16(num, last, len); break; case ios::oct: first = conv8(num, last, len); break; default: first = uconv10(num, last, len); break; } last++; *last = 0; if (putseps) PutCommas(last, len); if (neg) { first--; *first = '-'; len++; } if (width) { if (use_si && as) { *last++ = ext[as]; *last = 0; len++; } if (right_adjust) RightAdjust(first, len, width); } putseps = old_putseps; return first; }