Exemplo n.º 1
0
Arquivo: main.c Projeto: awersatos/AD
void main(void)
{
    int i, j;
    audio = audio_open(AUDIO_1);
    leds  = led_open(LEDS);
    led_set_all_on_intensity(leds, 0);
    led_turn_all_on(leds);

    while (1)
    {
        // get audio to left and right buffers for processing.
        get_audio(stereo_buf, AUDIO_BUF_SIZE);
        for (i = 0, j = 0; i < AUDIO_BUF_SIZE ; i++, j++)
        {
          audio_buf_r[j] = stereo_buf[i++];
          audio_buf_l[j] = stereo_buf[i];
        }

        // Loop left and right channels through.
        put_audio(stereo_buf, AUDIO_BUF_SIZE);

        // Get the D.C. average value of the wavelet.
        intensity_l = abs_ave(audio_buf_l, AUDIO_BUF_SIZE/2);
        intensity_r = abs_ave(audio_buf_r, AUDIO_BUF_SIZE/2);

        // Set VU meter on LEDS
        update_intensity(intensity_l, LEFT);
        update_intensity(intensity_r, RIGHT);
    }
}
Exemplo n.º 2
0
QmlVlcPlayerProxy::QmlVlcPlayerProxy( const std::shared_ptr<vlc::player>& player,
                                      QObject* parent /*= 0*/ )
    : QObject( parent ), m_player( player ), m_audio( *player ), m_input( *player ),
      m_playlist( this ), m_subtitle( *player ), m_video( *player ),
      m_currentMediaDesc( this )
{
    qRegisterMetaType<QmlVlcPlayerProxy::State>( "QmlVlcPlayerProxy::State" );

    m_errorTimer.setInterval( 1000 );
    m_errorTimer.setSingleShot( true );

    connect( this, SIGNAL( mediaPlayerEncounteredError() ),
             &m_errorTimer, SLOT( start() ) );
    connect( &m_errorTimer, SIGNAL( timeout() ),
             this, SLOT( currentItemEndReached() ) );

    connect( this, SIGNAL( mediaPlayerEndReached() ),
             &m_errorTimer, SLOT( stop() ) );
    connect( this, SIGNAL( mediaPlayerEndReached() ),
             this, SLOT( currentItemEndReached() ) );

    connect( this, SIGNAL( mediaPlayerMediaChanged() ),
             &m_errorTimer, SLOT( stop() ) );
    connect( this, &QmlVlcPlayerProxy::mediaPlayerMediaChanged,
             get_subtitle(), &QmlVlcSubtitle::eraseLoaded );

    connect( get_audio(), SIGNAL( volumeChanged() ),
             this, SIGNAL( volumeChanged() ) );
}
Exemplo n.º 3
0
int main()
{

	Xint16 audio_data[128];
	int i;
	u8 *oled_equalizer_buf=(u8 *)malloc(128*sizeof(u8));
	Xil_Out32(OLED_BASE_ADDR,0xff);
	OLED_Init();			//oled init
	IicConfig(XPAR_XIICPS_0_DEVICE_ID);
	AudioPllConfig(); //enable core clock for ADAU1761
	AudioConfigure();

	xil_printf("ADAU1761 configured\n\r");

	/*
	 * perform continuous read and writes from the codec that result in a loopback
	 * from Line in to Line out
	 */

	while(1)
	{
		get_audio(audio_data);
		for(i=0;i<128;i++)
		{
			oled_equalizer_buf[i]=audio_data[i]>>18;
		}
		OLED_Clear();
		OLED_Equalizer_128(oled_equalizer_buf);
	}
    return 0;
}
Exemplo n.º 4
0
/************************************************************************
*
* lame_readframe()
*
* PURPOSE:  reads a frame of audio data from a file to the buffer,
*   aligns the data for future processing, and separates the
*   left and right channels
*
*
************************************************************************/
int lame_readframe(lame_global_flags *gfp,short int Buffer[2][1152])
{
  int iread;

  /* note: if input is gfp->stereo and output is mono, get_audio() 
   * will return  .5*(L+R) in channel 0,  and nothing in channel 1. */
  iread = get_audio(gfp,Buffer,gfp->stereo);
  
  /* check to see if we overestimated/underestimated totalframes */
  if (iread==0)  gfp->totalframes = Min(gfp->totalframes,gfp->frameNum+2);
  if (gfp->frameNum > (gfp->totalframes-1)) gfp->totalframes = gfp->frameNum;
  return iread;
}
Exemplo n.º 5
0
/* Translate cursor position to audio samples */
struct audio_set get_audio_at_cursor(const struct cursor *cursor)
{
	struct audio_set set = { NULL, { NULL, NULL, NULL } };
	const struct electorate *elec;

	elec = get_voter_electorate();

	/* Are they on the group heading? */
	if (cursor->screen_candidate_index == -1) {
		set.group_audio = get_audio("electorates/%u/%u.raw",
					    elec->code,
					    cursor->group_index);
	} else {
		unsigned int dbci;
		struct ballot_contents *ballot;

		assert(cursor->screen_candidate_index >= 0);
		/* We need to know the number of candidates in this group */
		ballot = get_ballot_contents();

		/* They're on a candidate.  Translate. */
		dbci = translate_sci_to_dbci(ballot->num_candidates
					     [cursor->group_index],
					     cursor->screen_candidate_index,
					     get_current_rotation());
		set.candidate_audio[0]
			= get_pref_audio_for_candidate(cursor->group_index,
						       dbci);
		set.candidate_audio[1] = get_audio("electorates/%u/%u/%u.raw",
						elec->code, 
						cursor->group_index,
						   dbci);
		set.candidate_audio[2] = get_audio("electorates/%u/%u.raw",
						elec->code, 
						cursor->group_index);
	}
	return set;
}
Exemplo n.º 6
0
static struct audio *
get_pref_audio_for_candidate(unsigned int group_index,
			     unsigned int dbci)
{
	unsigned int prefnum;

	prefnum = get_pref_num(group_index, dbci);

	/* Fetch the audio sample (no commentry if not selected) */
	if (prefnum != 0)
		return get_audio("numbers/%u.raw", prefnum);
	else
		return NULL;
}
Exemplo n.º 7
0
int main() {
    savepoint("==========================");

    char buf_bat[200] = {0};
    char buf_back[200] = {0};
    char buf_net[200] = {0};
    char buf_audio[200] = {0};
    char buf_date[200] = {0};
    char buf_ws[300] = {0};
    savepoint("allocation");

    get_workspace(buf_ws);
    savepoint("workspace");

    get_battery(buf_bat);
    savepoint("battery");

    get_audio(buf_audio);
    savepoint("audio");

    get_date(buf_date);
    savepoint("date");

    if (get_network(buf_net) == 1) {
        get_backlight(buf_back, COLOR_BG_NET);
        savepoint("backlight");

        printf("%%{l}%s %%{r}%s %s %s %s %s%%{F%s}%%{B%s}\n", buf_ws, buf_audio, buf_net, buf_back, buf_bat, buf_date, COLOR_BG, COLOR_BG);
    } else {
        get_backlight(buf_back, COLOR_BG_AUDIO);
        savepoint("backlight");

        printf("%%{F%s}%%{B%s}%%{l}%s%%{F%s}%%{B%s} %%{r}%s %s %s %s%%{F%s}%%{B%s}\n",COLOR_BG, COLOR_BG,  buf_ws, COLOR_BG, COLOR_BG, buf_audio, buf_back, buf_bat, buf_date, COLOR_BG, COLOR_BG);
    }
    savepoint("print");
    savepoint("==========================");
    return 0;
}
Exemplo n.º 8
0
/* DDS????: Display Error */
void display_error(enum error err)
{
	unsigned int language;
	struct image *bgimage;

	language = get_language();

	/* Draw background if we can, otherwise clear screen */
	bgimage = get_message_mayfail(language, MSG_BACKGROUND);
	if (bgimage) paste_image(0, 0, bgimage);
	else clear_screen();

	/* Message to tell them */
	play_audio(true, get_audio("error.raw"));

	/* Use electorate "1" here for the numbers. */
	draw_error(get_message_mayfail(language, MSG_ERROR),
		   get_preference_image_mayfail(1, (unsigned int)err),
		   err);

	/* Loop until reset. */
	wait_for_reset();
}
Exemplo n.º 9
0
Arquivo: main.c Projeto: awersatos/AD
void main(void)
{
    int i, j;
    audio = audio_open(AUDIO_1);
    leds  = led_open(LEDS);
    while (1)
    {
        // get audio to left and right buffers for processing.
        get_audio(stereo_buf, AUDIO_BUF_SIZE);
        for (i = 0, j = 0; i < AUDIO_BUF_SIZE ; i++, j++)
        {
          audio_buf_r[j] = stereo_buf[i++];
          audio_buf_l[j] = stereo_buf[i];
        }

        // Loop left and right channels through.
        put_audio(stereo_buf, AUDIO_BUF_SIZE);

        // Put the D.C. average value of the wavelet on LEDs
        update_intensity(abs_ave(audio_buf_l, AUDIO_BUF_SIZE/2),LEFT);
        update_intensity(abs_ave(audio_buf_r, AUDIO_BUF_SIZE/2),RIGHT);
    }
}
Exemplo n.º 10
0
int main()
{
    //init_platform();
	u32 i;
	u32 ret[128];

	Xint16 audio_data[128];
	u8 *oled_equalizer_buf=(u8 *)malloc(128*sizeof(u8));//WARNING bro...: -Wimplicit-function-declaration

	/* -------- LED and HW related -------- */
	Xil_Out32(OLED_BASE_ADDR,0xff);
	OLED_Init();			//oled init
	IicConfig(XPAR_XIICPS_0_DEVICE_ID);
	AudioPllConfig(); //enable core clock for ADAU1761
	AudioConfigure();

	xil_printf("ADAU1761 configured\n\r");
while(1) {
	//DEBUG: xil_printf("*");

    // capture audio data
    get_audio(audio_data);

#if (MEASURE_TIME==1)
    // Your total latency (FFT + Magnitude + Noise cancellation + Averaging + to & fro  PS-PL communication) ...
    // should be less than 103us.
	init_timer(timer_ctrl, timer_counter_l, timer_counter_h);
	start_timer(timer_ctrl);
#endif

    for(i=0;i<128;i++)
    {
    	Xil_Out32(XPAR_AXI4BURST_PS2PL_0_S_AXI_MEM0_BASEADDR, audio_data[i]);
    	//DEBUG: xil_printf("sent data %0x\n\r", audio_data[i]);
    }

    //TODO: find more elegant way, checking some "completed status" signal maybe...
    usleep(10);// less than 10usec doesn't work

    for(i=0;i<128;i++)
    {
    	ret[i] = Xil_In32(XPAR_AXI4BURST_PS2PL_0_S_AXI_MEM0_BASEADDR);

    	//TODO: put these post-process in FPGA
    	oled_equalizer_buf[i] = (u8)(ret[i] >> 4);
    	//DEBUG: xil_printf("oled[%d]=%d\n\r", i, oled_equalizer_buf[i]);

    	if (oled_equalizer_buf[i] >= 40) {
    		oled_equalizer_buf[i] = 40;
    	}
    }

#if (MEASURE_TIME==1)
	stop_timer(timer_ctrl);
	//Calculate the time for the operation
	xil_printf("Communication time %d us\n\r", (*timer_counter_l) / 333);
#endif

    OLED_Clear();
    OLED_Equalizer_128(oled_equalizer_buf);
}
    return 0;
}
Exemplo n.º 11
0
int main (int argc, char **argv)
{
    typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
    SBS *sb_sample;
    typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
    JSBS *j_sample;
    typedef double IN[2][HAN_SIZE];
    IN *win_que;
    typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
    SUB *subband;

    frame_info frame;
    frame_header header;
    char original_file_name[MAX_NAME_SIZE];
    char encoded_file_name[MAX_NAME_SIZE];
    short **win_buf;
    static short buffer[2][1152];
    static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
    static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
    static double smr[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
    // FLOAT snr32[32];
    short sam[2][1344];		/* was [1056]; */
    int model, nch, error_protection;
    static unsigned int crc;
    int sb, ch, adb;
    unsigned long frameBits, sentBits = 0;
    unsigned long num_samples;
    int lg_frame;
    int i;

    /* Used to keep the SNR values for the fast/quick psy models */
    static FLOAT smrdef[2][32];

    static int psycount = 0;
    extern int minimum;

    sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
    j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
    win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
    subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
    win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");

    /* clear buffers */
    memset ((char *) buffer, 0, sizeof (buffer));
    memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
    memset ((char *) scalar, 0, sizeof (scalar));
    memset ((char *) j_scale, 0, sizeof (j_scale));
    memset ((char *) scfsi, 0, sizeof (scfsi));
    memset ((char *) smr, 0, sizeof (smr));
    memset ((char *) lgmin, 0, sizeof (lgmin));
    memset ((char *) max_sc, 0, sizeof (max_sc));
    //memset ((char *) snr32, 0, sizeof (snr32));
    memset ((char *) sam, 0, sizeof (sam));

    global_init ();

    header.extension = 0;
    frame.header = &header;
    frame.tab_num = -1;		/* no table loaded */
    frame.alloc = NULL;
    header.version = MPEG_AUDIO_ID;	/* Default: MPEG-1 */

    programName = argv[0];
    if (argc == 1)		/* no command-line args */
        short_usage ();
    else
        parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
                    encoded_file_name);
    print_config (&frame, &model, original_file_name, encoded_file_name);

    /* this will load the alloc tables and do some other stuff */
    hdr_to_frps (&frame);
    nch = frame.nch;
    error_protection = header.error_protection;

    while (get_audio (musicin, buffer, num_samples, nch, &header) > 0) {
        if (glopts.verbosity > 1)
            if (++frameNum % 10 == 0)
                fprintf (stderr, "[%4u]\r", frameNum);
        fflush (stderr);
        win_buf[0] = &buffer[0][0];
        win_buf[1] = &buffer[1][0];

        adb = available_bits (&header, &glopts);
        lg_frame = adb / 8;
        if (header.dab_extension) {
            /* in 24 kHz we always have 4 bytes */
            if (header.sampling_frequency == 1)
                header.dab_extension = 4;
            /* You must have one frame in memory if you are in DAB mode                 */
            /* in conformity of the norme ETS 300 401 http://www.etsi.org               */
            /* see bitstream.c            */
            if (frameNum == 1)
                minimum = lg_frame + MINIMUM;
            adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
        }

        {
            int gr, bl, ch;
            /* New polyphase filter
            Combines windowing and filtering. Ricardo Feb'03 */
            for( gr = 0; gr < 3; gr++ )
                for ( bl = 0; bl < 12; bl++ )
                    for ( ch = 0; ch < nch; ch++ )
                        WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
                                             &(*sb_sample)[ch][gr][bl][0] );
        }

#ifdef REFERENCECODE
        {
            /* Old code. left here for reference */
            int gr, bl, ch;
            for (gr = 0; gr < 3; gr++)
                for (bl = 0; bl < SCALE_BLOCK; bl++)
                    for (ch = 0; ch < nch; ch++) {
                        window_subband (&win_buf[ch], &(*win_que)[ch][0], ch);
                        filter_subband (&(*win_que)[ch][0], &(*sb_sample)[ch][gr][bl][0]);
                    }
        }
#endif


#ifdef NEWENCODE
        scalefactor_calc_new(*sb_sample, scalar, nch, frame.sblimit);
        find_sf_max (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR_new (*sb_sample, *j_sample, frame.sblimit);
            scalefactor_calc_new (j_sample, &j_scale, 1, frame.sblimit);
        }
#else
        scale_factor_calc (*sb_sample, scalar, nch, frame.sblimit);
        pick_scale (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR (*sb_sample, *j_sample, frame.sblimit);
            scale_factor_calc (j_sample, &j_scale, 1, frame.sblimit);
        }
#endif



        if ((glopts.quickmode == TRUE) && (++psycount % glopts.quickcount != 0)) {
            /* We're using quick mode, so we're only calculating the model every
               'quickcount' frames. Otherwise, just copy the old ones across */
            for (ch = 0; ch < nch; ch++) {
                for (sb = 0; sb < SBLIMIT; sb++)
                    smr[ch][sb] = smrdef[ch][sb];
            }
        } else {
            /* calculate the psymodel */
            switch (model) {
            case -1:
                psycho_n1 (smr, nch);
                break;
            case 0:	/* Psy Model A */
                psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
                break;
            case 1:
                psycho_1 (buffer, max_sc, smr, &frame);
                break;
            case 2:
                for (ch = 0; ch < nch; ch++) {
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                }
                break;
            case 3:
                /* Modified psy model 1 */
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                break;
            case 4:
                /* Modified Psycho Model 2 */
                for (ch = 0; ch < nch; ch++) {
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                }
                break;
            case 5:
                /* Model 5 comparse model 1 and 3 */
                psycho_1 (buffer, max_sc, smr, &frame);
                fprintf(stdout,"1 ");
                smr_dump(smr,nch);
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                fprintf(stdout,"3 ");
                smr_dump(smr,nch);
                break;
            case 6:
                /* Model 6 compares model 2 and 4 */
                for (ch = 0; ch < nch; ch++)
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"2 ");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4 ");
                smr_dump(smr,nch);
                break;
            case 7:
                fprintf(stdout,"Frame: %i\n",frameNum);
                /* Dump the SMRs for all models */
                psycho_1 (buffer, max_sc, smr, &frame);
                fprintf(stdout,"1");
                smr_dump(smr, nch);
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                fprintf(stdout,"3");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"2");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4");
                smr_dump(smr,nch);
                break;
            case 8:
                /* Compare 0 and 4 */
                psycho_n1 (smr, nch);
                fprintf(stdout,"0");
                smr_dump(smr,nch);

                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4");
                smr_dump(smr,nch);
                break;
            default:
                fprintf (stderr, "Invalid psy model specification: %i\n", model);
                exit (0);
            }

            if (glopts.quickmode == TRUE)
                /* copy the smr values and reuse them later */
                for (ch = 0; ch < nch; ch++) {
                    for (sb = 0; sb < SBLIMIT; sb++)
                        smrdef[ch][sb] = smr[ch][sb];
                }

            if (glopts.verbosity > 4)
                smr_dump(smr, nch);




        }

#ifdef NEWENCODE
        sf_transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation_new (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        //main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);

        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);

        write_header (&frame, &bs);
        //encode_info (&frame, &bs);
        if (error_protection)
            putbits (&bs, crc, 16);
        write_bit_alloc (bit_alloc, &frame, &bs);
        //encode_bit_alloc (bit_alloc, &frame, &bs);
        write_scalefactors(bit_alloc, scfsi, scalar, &frame, &bs);
        //encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization_new (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                                  *subband, &frame);
        //subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
        //	  *subband, &frame);
        write_samples_new(*subband, bit_alloc, &frame, &bs);
        //sample_encoding (*subband, bit_alloc, &frame, &bs);
#else
        transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);
        encode_info (&frame, &bs);
        if (error_protection)
            encode_CRC (crc, &bs);
        encode_bit_alloc (bit_alloc, &frame, &bs);
        encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                              *subband, &frame);
        sample_encoding (*subband, bit_alloc, &frame, &bs);
#endif


        /* If not all the bits were used, write out a stack of zeros */
        for (i = 0; i < adb; i++)
            put1bit (&bs, 0);
        if (header.dab_extension) {
            /* Reserve some bytes for X-PAD in DAB mode */
            putbits (&bs, 0, header.dab_length * 8);

            for (i = header.dab_extension - 1; i >= 0; i--) {
                CRC_calcDAB (&frame, bit_alloc, scfsi, scalar, &crc, i);
                /* this crc is for the previous frame in DAB mode  */
                if (bs.buf_byte_idx + lg_frame < bs.buf_size)
                    bs.buf[bs.buf_byte_idx + lg_frame] = crc;
                /* reserved 2 bytes for F-PAD in DAB mode  */
                putbits (&bs, crc, 8);
            }
            putbits (&bs, 0, 16);
        }

        frameBits = sstell (&bs) - sentBits;

        if (frameBits % 8) {	/* a program failure */
            fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits,
                     frameBits / 8, frameBits % 8);
            fprintf (stderr, "If you are reading this, the program is broken\n");
            fprintf (stderr, "email [mfc at NOTplanckenerg.com] without the NOT\n");
            fprintf (stderr, "with the command line arguments and other info\n");
            exit (0);
        }

        sentBits += frameBits;
    }

    close_bit_stream_w (&bs);

    if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
        int i;
#ifdef NEWENCODE
        extern int vbrstats_new[15];
#else
        extern int vbrstats[15];
#endif
        fprintf (stdout, "VBR stats:\n");
        for (i = 1; i < 15; i++)
            fprintf (stdout, "%4i ", bitrate[header.version][i]);
        fprintf (stdout, "\n");
        for (i = 1; i < 15; i++)
#ifdef NEWENCODE
            fprintf (stdout,"%4i ",vbrstats_new[i]);
#else
            fprintf (stdout, "%4i ", vbrstats[i]);
#endif
        fprintf (stdout, "\n");
    }

    fprintf (stderr,
             "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
             (FLOAT) sentBits / (frameNum * 8),
             (FLOAT) sentBits / (frameNum * 1152),
             (FLOAT) sentBits / (frameNum * 1152) *
             s_freq[header.version][header.sampling_frequency]);

    if (fclose (musicin) != 0) {
        fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
        exit (2);
    }

    fprintf (stderr, "\nDone\n");
    exit (0);
}
Exemplo n.º 12
0
static int
lame_encoder_loop(lame_global_flags * gf, FILE * outf, int nogap, char *inPath, char *outPath)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    int     Buffer[2][1152];
    int     iread, imp3, owrite;
    size_t  id3v2_size;

    encoder_progress_begin(gf, inPath, outPath);

    id3v2_size = lame_get_id3v2_tag(gf, 0, 0);
    if (id3v2_size > 0) {
        unsigned char *id3v2tag = malloc(id3v2_size);
        if (id3v2tag != 0) {
            size_t  n_bytes = lame_get_id3v2_tag(gf, id3v2tag, id3v2_size);
            size_t  written = fwrite(id3v2tag, 1, n_bytes, outf);
            free(id3v2tag);
            if (written != n_bytes) {
                encoder_progress_end(gf);
                error_printf("Error writing ID3v2 tag \n");
                return 1;
            }
        }
    }
    else {
        unsigned char* id3v2tag = getOldTag(gf);
        id3v2_size = sizeOfOldTag(gf);
        if ( id3v2_size > 0 ) {
            size_t owrite = fwrite(id3v2tag, 1, id3v2_size, outf);
            if (owrite != id3v2_size) {
                encoder_progress_end(gf);
                error_printf("Error writing ID3v2 tag \n");
                return 1;
            }
        }
    }
    if (global_writer.flush_write == 1) {
        fflush(outf);
    }

    /* encode until we hit eof */
    do {
        /* read in 'iread' samples */
        iread = get_audio(gf, Buffer);

        if (iread >= 0) {
            encoder_progress(gf);

            /* encode */
            imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread,
                                          mp3buffer, sizeof(mp3buffer));

            /* was our output buffer big enough? */
            if (imp3 < 0) {
                if (imp3 == -1)
                    error_printf("mp3 buffer is not big enough... \n");
                else
                    error_printf("mp3 internal error:  error code=%i\n", imp3);
                return 1;
            }
            owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
            if (owrite != imp3) {
                error_printf("Error writing mp3 output \n");
                return 1;
            }
        }
        if (global_writer.flush_write == 1) {
            fflush(outf);
        }
    } while (iread > 0);

    if (nogap)
        imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */
    else
        imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */

    if (imp3 < 0) {
        if (imp3 == -1)
            error_printf("mp3 buffer is not big enough... \n");
        else
            error_printf("mp3 internal error:  error code=%i\n", imp3);
        return 1;

    }

    encoder_progress_end(gf);

    owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
    if (owrite != imp3) {
        error_printf("Error writing mp3 output \n");
        return 1;
    }
    if (global_writer.flush_write == 1) {
        fflush(outf);
    }
    imp3 = write_id3v1_tag(gf, outf);
    if (global_writer.flush_write == 1) {
        fflush(outf);
    }
    if (imp3) {
        return 1;
    }
    write_xing_frame(gf, outf, id3v2_size);
    if (global_writer.flush_write == 1) {
        fflush(outf);
    }
    if (global_ui_config.silent <= 0) {
        print_trailing_info(gf);
    }
    return 0;
}
Exemplo n.º 13
0
int
lame_main(lame_t gf, int argc, char **argv)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    char    inPath[PATH_MAX + 1];
    char    outPath[PATH_MAX + 1];
    int     Buffer[2][1152];

    int     maxx = 0, tmpx = 0;
    int     ret;
    int     wavsamples;
    int     mp3bytes;
    FILE   *outf;

    char    ip[16];
    unsigned int port = 5004;
    unsigned int ttl = 2;
    char    dummy;

    if (argc <= 2) {
        console_printf("Encode (via LAME) to mp3 with RTP streaming of the output\n"
                       "\n"
                       "    mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile\n"
                       "\n"
                       "    examples:\n"
                       "      arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null\n"
                       "      arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3\n"
                       "\n");
        return 1;
    }

    switch (sscanf(argv[1], "%11[.0-9]:%u:%u%c", ip, &port, &ttl, &dummy)) {
    case 1:
    case 2:
    case 3:
        break;
    default:
        error_printf("Illegal destination selector '%s', must be ip[:port[:ttl]]\n", argv[1]);
        return -1;
    }
    rtp_initialization();
    if (rtp_socket(ip, port, ttl)) {
        rtp_deinitialization();
        error_printf("fatal error during initialization\n");
        return 1;
    }

    lame_set_errorf(gf, &frontend_errorf);
    lame_set_debugf(gf, &frontend_debugf);
    lame_set_msgf(gf, &frontend_msgf);

    /* Remove the argumets that are rtp related, and then 
     * parse the command line arguments, setting various flags in the
     * struct pointed to by 'gf'.  If you want to parse your own arguments,
     * or call libmp3lame from a program which uses a GUI to set arguments,
     * skip this call and set the values of interest in the gf struct.  
     * (see lame.h for documentation about these parameters)
     */
    {
        int     i;
        int     argc_mod = argc-1; /* leaving out exactly one argument */
        char**  argv_mod = calloc(argc_mod, sizeof(char*));
        argv_mod[0] = argv[0];
        for (i = 2; i < argc; ++i) { /* leaving out argument number 1, parsed above */
            argv_mod[i-1] = argv[i];
        }
        parse_args(gf, argc_mod, argv_mod, inPath, outPath, NULL, NULL);
        free(argv_mod);
    }

    /* open the output file.  Filename parsed into gf.inPath */
    if (0 == strcmp(outPath, "-")) {
        lame_set_stream_binary_mode(outf = stdout);
    }
    else {
        if ((outf = lame_fopen(outPath, "wb+")) == NULL) {
            rtp_deinitialization();
            error_printf("Could not create \"%s\".\n", outPath);
            return 1;
        }
    }


    /* open the wav/aiff/raw pcm or mp3 input file.  This call will
     * open the file with name gf.inFile, try to parse the headers and
     * set gf.samplerate, gf.num_channels, gf.num_samples.
     * if you want to do your own file input, skip this call and set
     * these values yourself.  
     */
    if (init_infile(gf, inPath) < 0) {
        rtp_deinitialization();
        fclose(outf);
        error_printf("Can't init infile '%s'\n", inPath);
        return 1;
    }


    /* Now that all the options are set, lame needs to analyze them and
     * set some more options 
     */
    ret = lame_init_params(gf);
    if (ret < 0) {
        if (ret == -1)
            display_bitrates(stderr);
        rtp_deinitialization();
        fclose(outf);
        close_infile();
        error_printf("fatal error during initialization\n");
        return -1;
    }

    lame_print_config(gf); /* print useful information about options being used */

    if (global_ui_config.update_interval < 0.)
        global_ui_config.update_interval = 2.;

    /* encode until we hit EOF */
    while ((wavsamples = get_audio(gf, Buffer)) > 0) { /* read in 'wavsamples' samples */
        levelmessage(maxvalue(Buffer), &maxx, &tmpx);
        mp3bytes = lame_encode_buffer_int(gf, /* encode the frame */
                                          Buffer[0], Buffer[1], wavsamples,
                                          mp3buffer, sizeof(mp3buffer));
        rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
        fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */
    }

    mp3bytes = lame_encode_flush(gf, /* may return one or more mp3 frame */
                                 mp3buffer, sizeof(mp3buffer));
    rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
    fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */

    lame_mp3_tags_fid(gf, outf); /* add VBR tags to mp3 file */

    rtp_deinitialization();
    fclose(outf);
    close_infile();     /* close the sound input file */
    return 0;
}
Exemplo n.º 14
0
int
lame_encoder(lame_global_flags * gf, FILE * outf, int nogap, char *inPath,
             char *outPath)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    int     Buffer[2][1152];
    int     iread, imp3;
    static const char *mode_names[2][4] = {
        {"stereo", "j-stereo", "dual-ch", "single-ch"},
        {"stereo", "force-ms", "dual-ch", "single-ch"}
    };
    int     frames;

    if (silent < 10) {
        lame_print_config(gf); /* print useful information about options being used */

        fprintf(stderr, "Encoding %s%s to %s\n",
                strcmp(inPath, "-") ? inPath : "<stdin>",
                strlen(inPath) + strlen(outPath) < 66 ? "" : "\n     ",
                strcmp(outPath, "-") ? outPath : "<stdout>");

        fprintf(stderr,
                "Encoding as %g kHz ", 1.e-3 * lame_get_out_samplerate(gf));

	{
            const char *appendix = "";

            switch (lame_get_VBR(gf)) {
            case vbr_mt:
            case vbr_rh:
            case vbr_mtrh:
                appendix = "ca. ";
                fprintf(stderr, "VBR(q=%i)", lame_get_VBR_q(gf));
                break;
            case vbr_abr:
                fprintf(stderr, "average %d kbps",
                        lame_get_VBR_mean_bitrate_kbps(gf));
                break;
            default:
                fprintf(stderr, "%3d kbps", lame_get_brate(gf));
                break;
            }
            fprintf(stderr, " %s MPEG-%u%s Layer III (%s%gx) qval=%i\n",
                    mode_names[lame_get_force_ms(gf)][lame_get_mode(gf)],
                    2 - lame_get_version(gf),
                    lame_get_out_samplerate(gf) < 16000 ? ".5" : "",
                    appendix,
                    0.1 * (int) (10. * lame_get_compression_ratio(gf) + 0.5),
                    lame_get_quality(gf));
        }

        if (silent <= -10)
            lame_print_internals(gf);

        fflush(stderr);
    }


    /* encode until we hit eof */
    do {
        /* read in 'iread' samples */
        iread = get_audio(gf, Buffer);
        frames = lame_get_frameNum(gf);


 /********************** status display  *****************************/
        if (silent <= 0) {
            if (update_interval > 0) {
                timestatus_klemm(gf);
            }
            else {
                if (0 == frames % 50) {
#ifdef BRHIST
                    brhist_jump_back();
#endif
                    timestatus(lame_get_out_samplerate(gf),
                               frames,
                               lame_get_totalframes(gf),
                               lame_get_framesize(gf));
#ifdef BRHIST
                    if (brhist)
                        brhist_disp(gf);
#endif
                }
            }
        }

        /* encode */
        imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread,
                                      mp3buffer, sizeof(mp3buffer));

        /* was our output buffer big enough? */
        if (imp3 < 0) {
            if (imp3 == -1)
                fprintf(stderr, "mp3 buffer is not big enough... \n");
            else
                fprintf(stderr, "mp3 internal error:  error code=%i\n", imp3);
            return 1;
        }

        if (fwrite(mp3buffer, 1, imp3, outf) != imp3) {
            fprintf(stderr, "Error writing mp3 output \n");
            return 1;
        }

    } while (iread);

    if (nogap) 
        imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */
    else
        imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */

    if (imp3 < 0) {
        if (imp3 == -1)
            fprintf(stderr, "mp3 buffer is not big enough... \n");
        else
            fprintf(stderr, "mp3 internal error:  error code=%i\n", imp3);
        return 1;

    }

    if (silent <= 0) {
#ifdef BRHIST
        brhist_jump_back();
#endif
        frames = lame_get_frameNum(gf);
        timestatus(lame_get_out_samplerate(gf),
                   frames, lame_get_totalframes(gf), lame_get_framesize(gf));
#ifdef BRHIST
        if (brhist) {
            brhist_disp(gf);
        }
        brhist_disp_total(gf);
#endif
        timestatus_finish();
    }

    fwrite(mp3buffer, 1, imp3, outf);

    return 0;
}
Exemplo n.º 15
0
static int
lame_encoder(lame_global_flags * gf, FILE * outf, int nogap, char *inPath, char *outPath)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    int     Buffer[2][1152];
    int     iread, imp3, owrite, id3v2_size;

    imp3 = lame_get_id3v2_tag(gf, mp3buffer, sizeof(mp3buffer));
    if ((size_t)imp3 > sizeof(mp3buffer)) {
        error_printf("Error writing ID3v2 tag: buffer too small: buffer size=%d  ID3v2 size=%d\n"
                , sizeof(mp3buffer)
                , imp3
                    );
        return 1;
    }
    owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
    if (owrite != imp3) {
        error_printf("Error writing ID3v2 tag \n");
        return 1;
    }
    if (flush_write == 1) {
        fflush(outf);
    }    
    id3v2_size = imp3;
    /* encode until we hit eof */
    do {
        /* read in 'iread' samples. 
        The value of iread is 1152  (2010/07/11) on my nexus one.
        */
        iread = get_audio(gf, Buffer);
        //needed to add a multiplier of 2 to achieve bytes on my G1. 2010/01/09 
        totBytes += iread *2;
        rewind(progFile);
        fprintf(progFile,"%d",totBytes);

        if (iread >= 0) {
            //char buf[50];
            //sprintf(buf,"%i",iread);
            //LOGV(buf); 
            /* encode */
            imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread,
                                          mp3buffer, sizeof(mp3buffer));
 
            /* was our output buffer big enough? */
            if (imp3 < 0) {
                if (imp3 == -1)
                    error_printf("mp3 buffer is not big enough... \n");
                else
                    error_printf("mp3 X internal error:  error code=%i\n", imp3);
                return 1;
            }
            owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
            if (owrite != imp3) {
                error_printf("Error writing mp3 output \n");
                return 1;
            }
        }
        if (flush_write == 1) {
            fflush(outf);
        }
    } while (iread > 0 && userCancel != 1);

    if (nogap)
        imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */
    else
        imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */

    if (imp3 < 0) {
        if (imp3 == -1)
            error_printf("mp3 buffer is not big enough... \n");
        else
            error_printf("mp3 internal error:  error code=%i\n", imp3);
        return 1;

    }

    
    owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
    if (owrite != imp3) {
        error_printf("Error writing mp3 output \n");
        return 1;
    }
    if (flush_write == 1) {
        fflush(outf);
    }

    
    imp3 = lame_get_id3v1_tag(gf, mp3buffer, sizeof(mp3buffer));
    if ((size_t)imp3 > sizeof(mp3buffer)) {
        error_printf("Error writing ID3v1 tag: buffer too small: buffer size=%d  ID3v1 size=%d\n"
                , sizeof(mp3buffer)
                , imp3
                    );
    }
    else {
        if (imp3 > 0) {
            owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
            if (owrite != imp3) {
                error_printf("Error writing ID3v1 tag \n");
                return 1;
            }
            if (flush_write == 1) {
                fflush(outf);
            }
        }
    }
    
    if (silent <= 0) {
        print_lame_tag_leading_info(gf);
    }
    if (fseek(outf, id3v2_size, SEEK_SET) != 0) {
        error_printf("fatal error: can't update LAME-tag frame!\n");                    
    }
    else {
        write_xing_frame(gf, outf);
    }

    if (silent <= 0) {
        print_trailing_info(gf);
    }    
    return 0;
}
Exemplo n.º 16
0
static int
lame_encoder(lame_global_flags * gf, FILE * outf, int nogap, char *inPath, char *outPath)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    int     Buffer[2][1152];
    int     iread, imp3, owrite, id3v2_size;

    encoder_progress_begin(gf, inPath, outPath);

    imp3 = lame_get_id3v2_tag(gf, mp3buffer, sizeof(mp3buffer));
    if ((size_t)imp3 > sizeof(mp3buffer)) {
        encoder_progress_end(gf);
        error_printf("Error writing ID3v2 tag: buffer too small: buffer size=%d  ID3v2 size=%d\n"
                , sizeof(mp3buffer)
                , imp3
                    );
        return 1;
    }
    owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
    if (owrite != imp3) {
        encoder_progress_end(gf);
        error_printf("Error writing ID3v2 tag \n");
        return 1;
    }
    if (flush_write == 1) {
        fflush(outf);
    }    
    id3v2_size = imp3;
    
    /* encode until we hit eof */
    do {
        /* read in 'iread' samples */
        iread = get_audio(gf, Buffer);

        if (iread >= 0) {
            encoder_progress(gf);

            /* encode */
            imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread,
                                          mp3buffer, sizeof(mp3buffer));

            /* was our output buffer big enough? */
            if (imp3 < 0) {
                if (imp3 == -1)
                    error_printf("mp3 buffer is not big enough... \n");
                else
                    error_printf("mp3 internal error:  error code=%i\n", imp3);
                return 1;
            }
            owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
            if (owrite != imp3) {
                error_printf("Error writing mp3 output \n");
                return 1;
            }
        }
        if (flush_write == 1) {
            fflush(outf);
        }
    } while (iread > 0);

    if (nogap)
        imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */
    else
        imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */

    if (imp3 < 0) {
        if (imp3 == -1)
            error_printf("mp3 buffer is not big enough... \n");
        else
            error_printf("mp3 internal error:  error code=%i\n", imp3);
        return 1;

    }

    encoder_progress_end(gf);
    
    owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
    if (owrite != imp3) {
        error_printf("Error writing mp3 output \n");
        return 1;
    }
    if (flush_write == 1) {
        fflush(outf);
    }

    
    imp3 = lame_get_id3v1_tag(gf, mp3buffer, sizeof(mp3buffer));
    if ((size_t)imp3 > sizeof(mp3buffer)) {
        error_printf("Error writing ID3v1 tag: buffer too small: buffer size=%d  ID3v1 size=%d\n"
                , sizeof(mp3buffer)
                , imp3
                    );
    }
    else {
        if (imp3 > 0) {
            owrite = (int) fwrite(mp3buffer, 1, imp3, outf);
            if (owrite != imp3) {
                error_printf("Error writing ID3v1 tag \n");
                return 1;
            }
            if (flush_write == 1) {
                fflush(outf);
            }
        }
    }
    
    if (silent <= 0) {
        print_lame_tag_leading_info(gf);
    }
    if (fseek(outf, id3v2_size, SEEK_SET) != 0) {
        error_printf("fatal error: can't update LAME-tag frame!\n");                    
    }
    else {
        write_xing_frame(gf, outf);
    }

    if (silent <= 0) {
        print_trailing_info(gf);
    }    
    return 0;
}
Exemplo n.º 17
0
int main (int argc, char **argv)
{
    typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
    SBS *sb_sample;
    typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
    JSBS *j_sample;
#ifdef REFERENCECODE
    typedef double IN[2][HAN_SIZE];
    IN *win_que;
#endif
    typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
    SUB *subband;

    frame_info frame;
    frame_header header;
    char original_file_name[MAX_NAME_SIZE];
    char encoded_file_name[MAX_NAME_SIZE];
    short **win_buf;
    static short buffer[2][1152];
    static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
    static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
    static double smr[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
    // FLOAT snr32[32];
    short sam[2][1344];		/* was [1056]; */
    int model, nch, error_protection;
    static unsigned int crc;
    int sb, ch, adb;
    unsigned long frameBits, sentBits = 0;
    unsigned long num_samples;
    int lg_frame;
    int i;

    /* Keep track of peaks */
    int peak_left = 0;
    int peak_right = 0;

    char* mot_file = NULL;
    char* icy_file = NULL;

    /* Used to keep the SNR values for the fast/quick psy models */
    static FLOAT smrdef[2][32];

    static int psycount = 0;
    extern int minimum;

    sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
    j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
#ifdef REFERENCECODE
    win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
#endif
    subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
    win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");

    /* clear buffers */
    memset ((char *) buffer, 0, sizeof (buffer));
    memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
    memset ((char *) scalar, 0, sizeof (scalar));
    memset ((char *) j_scale, 0, sizeof (j_scale));
    memset ((char *) scfsi, 0, sizeof (scfsi));
    memset ((char *) smr, 0, sizeof (smr));
    memset ((char *) lgmin, 0, sizeof (lgmin));
    memset ((char *) max_sc, 0, sizeof (max_sc));
    //memset ((char *) snr32, 0, sizeof (snr32));
    memset ((char *) sam, 0, sizeof (sam));

    global_init ();

    header.extension = 0;
    frame.header = &header;
    frame.tab_num = -1;		/* no table loaded */
    frame.alloc = NULL;
    header.version = MPEG_AUDIO_ID;	/* Default: MPEG-1 */

    programName = argv[0];
    if (argc == 1)		/* no command-line args */
        short_usage ();
    else
        parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
                encoded_file_name, &mot_file, &icy_file);
    print_config (&frame, &model, original_file_name, encoded_file_name);

    uint8_t* xpad_data = NULL;
    if (mot_file) {
        if (header.dab_length <= 0) {
            fprintf(stderr, "Invalid XPAD length specified\n");
            return 1;
        }

        int err = xpad_init(mot_file, header.dab_length + 1);
        if (err == -1) {
            fprintf(stderr, "XPAD reader initialisation failed\n");
            return 1;
        }

        xpad_data = malloc(header.dab_length + 1);
    }

    /* this will load the alloc tables and do some other stuff */
    hdr_to_frps (&frame);
    nch = frame.nch;
    error_protection = header.error_protection;

    unsigned long samps_read;
    while ((samps_read = get_audio(&musicin, buffer, num_samples, nch, &header)) > 0) {
        /* Check if we have new PAD data
         */
        int xpad_len = 0;
        if (mot_file) {
            xpad_len = xpad_read_len(xpad_data, header.dab_length + 1);

            if (xpad_len == -1) {
                fprintf(stderr, "Error reading XPAD data\n");
                xpad_len = 0;
            }
            else if (xpad_len == 0) {
                // no PAD available
            }
            else if (xpad_len == header.dab_length + 1) {
                // everything OK
                xpad_len = xpad_data[header.dab_length];
            }
            else {
                fprintf(stderr, "xpad length=%d\n", xpad_len);
                abort();
            }
        }

        unsigned long j;
        for (j = 0; j < samps_read; j++) {
            peak_left  = MAX(peak_left,  buffer[0][j]);
        }
        for (j = 0; j < samps_read; j++) {
            peak_right = MAX(peak_right, buffer[1][j]);
        }

        // We can always set the zmq peaks, even if the output is not
        // used, it just writes some variables
        zmqoutput_set_peaks(peak_left, peak_right);

        if (glopts.verbosity > 1)
            if (++frameNum % 10 == 0) {

                fprintf(stderr, "[%4u", frameNum);

                if (mot_file) {
                    fprintf(stderr, " %s",
                        xpad_len > 0 ? "p" : " ");
                }

                if (glopts.show_level) {
                    fprintf(stderr, " (%6d|%-6d) ",
                            peak_left, peak_right);

                    fprintf(stderr, "] [%6s|%-6s]\r",
                            level(0, &peak_left),
                            level(1, &peak_right) );
                }
                else {
                    fprintf(stderr, "]\r");
                }
            }

        fflush(stderr);
        win_buf[0] = &buffer[0][0];
        win_buf[1] = &buffer[1][0];

        adb = available_bits (&header, &glopts);
        lg_frame = adb / 8;
        if (header.dab_extension) {
            /* in 24 kHz we always have 4 bytes */
            if (header.sampling_frequency == 1)
                header.dab_extension = 4;
            /* You must have one frame in memory if you are in DAB mode                 */
            /* in conformity of the norme ETS 300 401 http://www.etsi.org               */
            /* see bitstream.c            */
            if (frameNum == 1)
                minimum = lg_frame + MINIMUM;
            adb -= header.dab_extension * 8 + (xpad_len ? xpad_len : FPAD_LENGTH) * 8;
        }

        {
            int gr, bl, ch;
            /* New polyphase filter
               Combines windowing and filtering. Ricardo Feb'03 */
            for( gr = 0; gr < 3; gr++ )
                for ( bl = 0; bl < 12; bl++ )
                    for ( ch = 0; ch < nch; ch++ )
                        WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
                                &(*sb_sample)[ch][gr][bl][0] );
        }

#ifdef REFERENCECODE
        {
            /* Old code. left here for reference */
            int gr, bl, ch;
            for (gr = 0; gr < 3; gr++)
                for (bl = 0; bl < SCALE_BLOCK; bl++)
                    for (ch = 0; ch < nch; ch++) {
                        window_subband (&win_buf[ch], &(*win_que)[ch][0], ch);
                        filter_subband (&(*win_que)[ch][0], &(*sb_sample)[ch][gr][bl][0]);
                    }
        }
#endif


#ifdef NEWENCODE
        scalefactor_calc_new(*sb_sample, scalar, nch, frame.sblimit);
        find_sf_max (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR_new (*sb_sample, *j_sample, frame.sblimit);
            scalefactor_calc_new (j_sample, &j_scale, 1, frame.sblimit);
        }
#else
        scale_factor_calc (*sb_sample, scalar, nch, frame.sblimit);
        pick_scale (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR (*sb_sample, *j_sample, frame.sblimit);
            scale_factor_calc (j_sample, &j_scale, 1, frame.sblimit);
        }
#endif



        if ((glopts.quickmode == TRUE) && (++psycount % glopts.quickcount != 0)) {
            /* We're using quick mode, so we're only calculating the model every
               'quickcount' frames. Otherwise, just copy the old ones across */
            for (ch = 0; ch < nch; ch++) {
                for (sb = 0; sb < SBLIMIT; sb++)
                    smr[ch][sb] = smrdef[ch][sb];
            }
        } else {
            /* calculate the psymodel */
            switch (model) {
                case -1:
                    psycho_n1 (smr, nch);
                    break;
                case 0:	/* Psy Model A */
                    psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);	
                    break;
                case 1:
                    psycho_1 (buffer, max_sc, smr, &frame);
                    break;
                case 2:
                    for (ch = 0; ch < nch; ch++) {
                        psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    }
                    break;
                case 3:
                    /* Modified psy model 1 */
                    psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                    break;
                case 4:
                    /* Modified Psycho Model 2 */
                    for (ch = 0; ch < nch; ch++) {
                        psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    }
                    break;	
                case 5:
                    /* Model 5 comparse model 1 and 3 */
                    psycho_1 (buffer, max_sc, smr, &frame);
                    fprintf(stdout,"1 ");
                    smr_dump(smr,nch);
                    psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                    fprintf(stdout,"3 ");
                    smr_dump(smr,nch);
                    break;
                case 6:
                    /* Model 6 compares model 2 and 4 */
                    for (ch = 0; ch < nch; ch++) 
                        psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    fprintf(stdout,"2 ");
                    smr_dump(smr,nch);
                    for (ch = 0; ch < nch; ch++) 
                        psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    fprintf(stdout,"4 ");
                    smr_dump(smr,nch);
                    break;
                case 7:
                    fprintf(stdout,"Frame: %i\n",frameNum);
                    /* Dump the SMRs for all models */	
                    psycho_1 (buffer, max_sc, smr, &frame);
                    fprintf(stdout,"1");
                    smr_dump(smr, nch);
                    psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                    fprintf(stdout,"3");
                    smr_dump(smr,nch);
                    for (ch = 0; ch < nch; ch++) 
                        psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    fprintf(stdout,"2");
                    smr_dump(smr,nch);
                    for (ch = 0; ch < nch; ch++) 
                        psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    fprintf(stdout,"4");
                    smr_dump(smr,nch);
                    break;
                case 8:
                    /* Compare 0 and 4 */	
                    psycho_n1 (smr, nch);
                    fprintf(stdout,"0");
                    smr_dump(smr,nch);

                    for (ch = 0; ch < nch; ch++) 
                        psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                                (FLOAT) s_freq[header.version][header.sampling_frequency] *
                                1000, &glopts);
                    fprintf(stdout,"4");
                    smr_dump(smr,nch);
                    break;
                default:
                    fprintf (stderr, "Invalid psy model specification: %i\n", model);
                    exit (0);
            }

            if (glopts.quickmode == TRUE)
                /* copy the smr values and reuse them later */
                for (ch = 0; ch < nch; ch++) {
                    for (sb = 0; sb < SBLIMIT; sb++)
                        smrdef[ch][sb] = smr[ch][sb];
                }

            if (glopts.verbosity > 4) 
                smr_dump(smr, nch);




        }

#ifdef NEWENCODE
        sf_transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation_new (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        //main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);

        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);

        write_header (&frame, &bs);
        //encode_info (&frame, &bs);
        if (error_protection)
            putbits (&bs, crc, 16);
        write_bit_alloc (bit_alloc, &frame, &bs);
        //encode_bit_alloc (bit_alloc, &frame, &bs);
        write_scalefactors(bit_alloc, scfsi, scalar, &frame, &bs);
        //encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization_new (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                *subband, &frame);
        //subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
        //	  *subband, &frame);
        write_samples_new(*subband, bit_alloc, &frame, &bs);
        //sample_encoding (*subband, bit_alloc, &frame, &bs);
#else
        transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);
        encode_info (&frame, &bs);
        if (error_protection)
            encode_CRC (crc, &bs);
        encode_bit_alloc (bit_alloc, &frame, &bs);
        encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                *subband, &frame);
        sample_encoding (*subband, bit_alloc, &frame, &bs);
#endif


        /* If not all the bits were used, write out a stack of zeros */
        for (i = 0; i < adb; i++)
            put1bit (&bs, 0);


        if (xpad_len) {
            assert(xpad_len > 2);

            // insert available X-PAD
            for (i = header.dab_length - xpad_len; i < header.dab_length - FPAD_LENGTH; i++)
                putbits (&bs, xpad_data[i], 8);
        }


        for (i = header.dab_extension - 1; i >= 0; i--) {
            CRC_calcDAB (&frame, bit_alloc, scfsi, scalar, &crc, i);
            /* this crc is for the previous frame in DAB mode  */
            if (bs.buf_byte_idx + lg_frame < bs.buf_size)
                bs.buf[bs.buf_byte_idx + lg_frame] = crc;
            /* reserved 2 bytes for F-PAD in DAB mode  */
            putbits (&bs, crc, 8);
        }

        if (xpad_len) {
            /* The F-PAD is also given us by mot-encoder */
            putbits (&bs, xpad_data[header.dab_length - 2], 8);
            putbits (&bs, xpad_data[header.dab_length - 1], 8);
        }
        else {
            putbits (&bs, 0, 16); // FPAD is all-zero
        }

#if defined(VLC_INPUT)
        if (glopts.input_select == INPUT_SELECT_VLC) {
            vlc_in_write_icy();
        }
#endif


        frameBits = sstell (&bs) - sentBits;

        if (frameBits % 8) {	/* a program failure */
            fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits,
                    frameBits / 8, frameBits % 8);
            fprintf (stderr, "If you are reading this, the program is broken\n");
            fprintf (stderr, "Please report a bug.\n");
            exit(1);
        }

        sentBits += frameBits;

        // Reset peak measurement
        peak_left = 0;
        peak_right = 0;
    }

    fprintf(stdout, "Main loop has quit with samps_read = %zu\n", samps_read);

    close_bit_stream_w (&bs);

    if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
        int i;
#ifdef NEWENCODE
        extern int vbrstats_new[15];
#else
        extern int vbrstats[15];
#endif
        fprintf (stdout, "VBR stats:\n");
        for (i = 1; i < 15; i++)
            fprintf (stdout, "%4i ", bitrate[header.version][i]);
        fprintf (stdout, "\n");
        for (i = 1; i < 15; i++)
#ifdef NEWENCODE
            fprintf (stdout,"%4i ",vbrstats_new[i]);
#else
        fprintf (stdout, "%4i ", vbrstats[i]);
#endif
        fprintf (stdout, "\n");
    }

    fprintf (stderr,
            "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
            (FLOAT) sentBits / (frameNum * 8),
            (FLOAT) sentBits / (frameNum * 1152),
            (FLOAT) sentBits / (frameNum * 1152) *
            s_freq[header.version][header.sampling_frequency]);

    if (glopts.input_select == INPUT_SELECT_WAV) {
        if ( fclose (musicin.wav_input) != 0) {
            fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
            exit (2);
        }
    }

    fprintf (stderr, "\nDone\n");
    exit (0);
}
Exemplo n.º 18
0
int mlt_frame_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
{
	mlt_get_audio get_audio = mlt_frame_pop_audio( self );
	mlt_properties properties = MLT_FRAME_PROPERTIES( self );
	int hide = mlt_properties_get_int( properties, "test_audio" );
	mlt_audio_format requested_format = *format;

	if ( hide == 0 && get_audio != NULL )
	{
		get_audio( self, buffer, format, frequency, channels, samples );
		mlt_properties_set_int( properties, "audio_frequency", *frequency );
		mlt_properties_set_int( properties, "audio_channels", *channels );
		mlt_properties_set_int( properties, "audio_samples", *samples );
		mlt_properties_set_int( properties, "audio_format", *format );
		if ( self->convert_audio && *buffer && requested_format != mlt_audio_none )
			self->convert_audio( self, buffer, format, requested_format );
	}
	else if ( mlt_properties_get_data( properties, "audio", NULL ) )
	{
		*buffer = mlt_properties_get_data( properties, "audio", NULL );
		*format = mlt_properties_get_int( properties, "audio_format" );
		*frequency = mlt_properties_get_int( properties, "audio_frequency" );
		*channels = mlt_properties_get_int( properties, "audio_channels" );
		*samples = mlt_properties_get_int( properties, "audio_samples" );
		if ( self->convert_audio && *buffer && requested_format != mlt_audio_none )
			self->convert_audio( self, buffer, format, requested_format );
	}
	else
	{
		int size = 0;
		*samples = *samples <= 0 ? 1920 : *samples;
		*channels = *channels <= 0 ? 2 : *channels;
		*frequency = *frequency <= 0 ? 48000 : *frequency;
		mlt_properties_set_int( properties, "audio_frequency", *frequency );
		mlt_properties_set_int( properties, "audio_channels", *channels );
		mlt_properties_set_int( properties, "audio_samples", *samples );
		mlt_properties_set_int( properties, "audio_format", *format );

		switch( *format )
		{
			case mlt_image_none:
				size = 0;
				*buffer = NULL;
				break;
			case mlt_audio_s16:
				size = *samples * *channels * sizeof( int16_t );
				break;
			case mlt_audio_s32:
				size = *samples * *channels * sizeof( int32_t );
				break;
			case mlt_audio_float:
				size = *samples * *channels * sizeof( float );
				break;
			default:
				break;
		}
		if ( size )
			*buffer = mlt_pool_alloc( size );
		if ( *buffer )
			memset( *buffer, 0, size );
		mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
		mlt_properties_set_int( properties, "test_audio", 1 );
	}

	// TODO: This does not belong here
	if ( *format == mlt_audio_s16 && mlt_properties_get( properties, "meta.volume" ) )
	{
		double value = mlt_properties_get_double( properties, "meta.volume" );

		if ( value == 0.0 )
		{
			memset( *buffer, 0, *samples * *channels * 2 );
		}
		else if ( value != 1.0 )
		{
			int total = *samples * *channels;
			int16_t *p = *buffer;
			while ( total -- )
			{
				*p = *p * value;
				p ++;
			}
		}

		mlt_properties_set( properties, "meta.volume", NULL );
	}

	return 0;
}
Exemplo n.º 19
0
QmlVlcPlayerProxy::QmlVlcPlayerProxy( const std::shared_ptr<vlc::player>& player,
                                      QObject* parent /*= 0*/ )
    : QObject( parent ), m_player( player ), m_audio( *player ), m_input( *player ),
      m_playlist( this ), m_subtitle( *player ), m_video( *player ),
      m_currentMediaDesc( this )
{
    qRegisterMetaType<QmlVlcPlayerProxy::State>("QmlVlcPlayerProxy::State");

    m_errorTimer.setInterval( 1000 );
    m_errorTimer.setSingleShot( true );

    connect( this, SIGNAL( mediaPlayerPlaying() ),
             this, SIGNAL( playingChanged() ), Qt::QueuedConnection );
    connect( this, SIGNAL( mediaPlayerPaused() ),
             this, SIGNAL( playingChanged() ), Qt::QueuedConnection );
    connect( this, SIGNAL( mediaPlayerEncounteredError() ),
             this, SIGNAL( playingChanged() ), Qt::QueuedConnection );
    connect( this, SIGNAL( mediaPlayerEndReached() ),
             this, SIGNAL( playingChanged() ), Qt::QueuedConnection );
    connect( this, SIGNAL( mediaPlayerStopped() ),
             this, SIGNAL( playingChanged() ), Qt::QueuedConnection );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerNothingSpecial,
        [this] () {
            Q_EMIT stateChanged( NothingSpecial );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerOpening,
        [this] () {
            Q_EMIT stateChanged( Opening );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerBuffering,
        [this] ( float ) {
            Q_EMIT stateChanged( Buffering );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerPlaying,
        [this] () {
            Q_EMIT stateChanged( Playing );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerPaused,
        [this] () {
            Q_EMIT stateChanged( Paused );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerEncounteredError,
        [this] () {
            Q_EMIT stateChanged( Error );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerEndReached,
        [this] () {
            Q_EMIT stateChanged( Ended );
        } );

    connect( this, &QmlVlcPlayerProxy::mediaPlayerStopped,
        [this] () {
            Q_EMIT stateChanged( Stopped );
        } );


    connect( this, SIGNAL( mediaPlayerEncounteredError() ),
             &m_errorTimer, SLOT( start() ) );
    connect( &m_errorTimer, SIGNAL( timeout() ),
             this, SLOT( currentItemEndReached() ) );

    connect( this, SIGNAL( mediaPlayerEndReached() ),
             &m_errorTimer, SLOT( stop() ) );
    connect( this, SIGNAL( mediaPlayerEndReached() ),
             this, SLOT( currentItemEndReached() ) );

    connect( this, SIGNAL( mediaPlayerMediaChanged() ),
             &m_errorTimer, SLOT( stop() ) );
    connect( this, SIGNAL( mediaPlayerMediaChanged() ),
             &m_playlist, SIGNAL( currentItemChanged() ), Qt::QueuedConnection );
    connect( this, &QmlVlcPlayerProxy::mediaPlayerMediaChanged,
             get_subtitle(), &QmlVlcSubtitle::eraseLoaded );

    connect( this, SIGNAL( mediaPlayerTitleChanged() ),
             &m_currentMediaDesc, SIGNAL( titleChanged() ), Qt::QueuedConnection );

    connect( get_audio(), SIGNAL( volumeChanged() ),
             this, SIGNAL( volumeChanged() ), Qt::QueuedConnection );
}