Ignore:
Timestamp:
10/12/06 23:34:50 (16 years ago)
Author:
zorg
Message:

Add wav output

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpcdec/branches/zorg/mpcdec/mpcdec.c

    r70 r75  
    3636#include <time.h>
    3737#include <mpcdec/mpcdec.h>
     38#include <libwaveformat.h>
    3839
    3940#ifdef WIN32
     
    4142#endif
    4243
     44t_wav_uint32 mpc_wav_output_write(void* p_user_data, void const* p_buffer, t_wav_uint32 p_bytes)
     45{
     46    FILE* p_handle = (FILE*) p_user_data;
     47    return (t_wav_uint32) fwrite(p_buffer, 1, p_bytes, p_handle);
     48}
     49
     50t_wav_uint32 mpc_wav_output_seek(void* p_user_data, t_wav_uint32 p_position)
     51{
     52    FILE* p_handle = (FILE*) p_user_data;
     53    return (t_wav_uint32) !fseek(p_handle, p_position, SEEK_SET);
     54}
     55
    4356static void
    4457usage(const char *exename)
    4558{
    46     printf("Usage: %s <infile.mpc>\n", exename);
     59    printf("Usage: %s <infile.mpc> [<outfile.wav>]\n", exename);
    4760}
    4861
     
    5265    mpc_reader reader; mpc_streaminfo si; mpc_decoder* decoder; mpc_status err;
    5366    MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
    54     clock_t begin, end, sum; int total_samples;
     67    clock_t begin, end, sum; int total_samples; t_wav_output_file wav_output;
     68    mpc_bool_t is_wav_output;
    5569
    56     if(argc != 2)
     70    if(3 < argc && argc < 2)
    5771    {
    5872        usage(argv[0]);
     
    6983    if(err < 0) return !MPC_STATUS_OK;
    7084
     85    is_wav_output = argc > 2;
     86    if(is_wav_output)
     87    {
     88        t_wav_output_file_callback wavo_fc;
     89        memset(&wav_output, 0, sizeof wav_output);
     90        wavo_fc.m_seek      = mpc_wav_output_seek;
     91        wavo_fc.m_write     = mpc_wav_output_write;
     92        wavo_fc.m_user_data = fopen(argv[2], "wb");
     93        if(!wavo_fc.m_user_data) return !MPC_STATUS_OK;
     94        err = waveformat_output_open(&wav_output, wavo_fc, si.channels, 16, 0, si.sample_freq, (t_wav_uint32) si.pcm_samples * 2);
     95        if(!err) return !MPC_STATUS_OK;
     96    }
     97
    7198    sum = total_samples = 0;
    7299    while(TRUE)
    73100    {
    74101        mpc_uint32_t samples;
    75         begin = clock();
     102
     103        begin   = clock();
    76104        samples = mpc_decoder_decode(decoder, sample_buffer, 0, 0);
    77         end = clock();
     105        end     = clock();
     106
    78107        if(samples <= 0) break;
    79         if(!samples) break;
     108        if(!samples)     break;
    80109        total_samples += samples;
    81110        sum           += end - begin;
     111
     112        if(is_wav_output)
     113            if(waveformat_output_process_float32(&wav_output, sample_buffer, samples*2) < 0)
     114                break;
    82115    }
    83116
     
    92125    mpc_decoder_exit(decoder);
    93126    mpc_reader_exit_stdio(&reader);
     127    if(is_wav_output)
     128    {
     129        waveformat_output_close(&wav_output);
     130        fclose(wav_output.m_callback.m_user_data);
     131    }
    94132
    95133#ifdef WIN32
Note: See TracChangeset for help on using the changeset viewer.