Ignore:
Timestamp:
11/01/08 22:09:22 (15 years ago)
Author:
r2d
Message:

updated mpcgain to fill the chapters' gain / peak

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpc/trunk/mpcgain/mpcgain.c

    r404 r413  
    5050#define MPCGAIN_VERSION cat(MPCGAIN_MAJOR,MPCGAIN_MINOR,MPCGAIN_BUILD)
    5151
    52 const char    About []        = "mpcgain - Musepack (MPC) ReplayGain calculator v" MPCGAIN_VERSION " (C) 2006-2007 MDT\nBuilt " __DATE__ " " __TIME__ "\n";
     52#define MAX_HEAD_SIZE 20 // maximum size of the packet header before chapter gain (2 + 9 + 9)
     53
     54const char    About []        = "mpcgain - Musepack (MPC) ReplayGain calculator v" MPCGAIN_VERSION " (C) 2006-2008 MDT\nBuilt " __DATE__ " " __TIME__ "\n";
    5355
    5456
     
    5658{
    5759        printf("Usage: %s <infile.mpc> [<infile2.mpc> <infile3.mpc> ... ]\n", exename);
     60}
     61
     62static mpc_inline MPC_SAMPLE_FORMAT _max(MPC_SAMPLE_FORMAT a, MPC_SAMPLE_FORMAT b)
     63{
     64        if (a > b)
     65                return a;
     66        return b;
     67}
     68
     69static mpc_inline MPC_SAMPLE_FORMAT max_abs(MPC_SAMPLE_FORMAT a, MPC_SAMPLE_FORMAT b)
     70{
     71        if (b < 0)
     72                b = -b;
     73        if (b > a)
     74                return b;
     75        return a;
     76}
     77
     78static MPC_SAMPLE_FORMAT analyze_get_max(MPC_SAMPLE_FORMAT * sample_buffer, int sample_nb)
     79{
     80        Float_t left_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
     81        Float_t right_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
     82        MPC_SAMPLE_FORMAT max = 0;
     83        int i;
     84
     85        for (i = 0; i < sample_nb; i++){
     86                left_samples[i] = sample_buffer[2 * i] * (1 << 15);
     87                right_samples[i] = sample_buffer[2 * i + 1] * (1 << 15);
     88                max = max_abs(max, sample_buffer[2 * i]);
     89                max = max_abs(max, sample_buffer[2 * i + 1]);
     90        }
     91        gain_analyze_samples(left_samples, right_samples, sample_nb, 2);
     92
     93        return max;
     94}
     95
     96
     97
     98static void write_chaps_gain(mpc_demux * demux, const char * file_name,
     99                             mpc_uint16_t * chap_gain, mpc_uint16_t * chap_peak)
     100{
     101        unsigned char buffer[MAX_HEAD_SIZE];
     102        mpc_bits_reader r;
     103        mpc_block b;
     104        mpc_uint64_t size, dummy;
     105        FILE * file;
     106        int chap = 0;
     107        long next_chap_pos = demux->chap_pos >> 3;
     108
     109
     110        file = fopen( file_name, "r+");
     111        if (file == 0) {
     112                fprintf(stderr, "Can't open file \"%s\" for writing\n", file_name);
     113                return;
     114        }
     115
     116        while (1) {
     117                fseek(file, next_chap_pos, SEEK_SET);
     118                fread(buffer, 1, MAX_HEAD_SIZE, file);
     119                r.buff = buffer;
     120                r.count = 8;
     121                size = mpc_bits_get_block(&r, &b);
     122
     123                if (memcmp(b.key, "CT", 2) != 0)
     124                        break;
     125
     126                b.size += size;
     127                size += mpc_bits_get_size(&r, &dummy);
     128
     129                fseek(file, next_chap_pos + size, SEEK_SET);
     130                buffer[0] = chap_gain[chap] >> 8;
     131                buffer[1] = chap_gain[chap] & 0xFF;
     132                buffer[2] = chap_peak[chap] >> 8;
     133                buffer[3] = chap_peak[chap] & 0xFF;
     134                fwrite(buffer, 1, 4, file); // writing chapter gain / peak
     135
     136                chap++;
     137                next_chap_pos += b.size;
     138        }
     139
     140        fclose(file);
    58141}
    59142
     
    70153        printf(About);
    71154
    72         if(argc < 2) {
     155        if (argc < 2) {
    73156                usage(argv[0]);
    74157                return 0;
     
    79162        header_pos = (mpc_uint32_t *) (title_peak + (argc - 1));
    80163
    81         for( j = 1; j < argc; j++){
     164        for (j = 1; j < argc; j++) {
    82165                MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
    83                 Float_t left_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
    84                 Float_t right_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
    85                 MPC_SAMPLE_FORMAT title_max = 0;
     166                MPC_SAMPLE_FORMAT title_max = 0, chap_max;
     167                mpc_uint16_t * chap_gain, * chap_peak;
    86168                mpc_reader reader;
    87169                mpc_demux* demux;
    88170                mpc_streaminfo si;
    89171                mpc_status err;
     172                int chap_nb, chap = 0;
     173                mpc_uint64_t cur_sample = 1, next_chap_sample = mpc_int64_max;
    90174
    91175                err = mpc_reader_init_stdio(&reader, argv[j]);
    92                 if(err < 0) return !MPC_STATUS_OK;
     176                if (err < 0) return !MPC_STATUS_OK;
    93177
    94178                demux = mpc_demux_init(&reader);
    95                 if(!demux) return !MPC_STATUS_OK;
     179                if (!demux) return !MPC_STATUS_OK;
    96180                mpc_demux_get_info(demux,  &si);
    97181
     182                chap_nb = mpc_demux_chap_nb(demux);
     183                mpc_demux_seek_sample(demux, 0);
     184                if (chap_nb > 0) {
     185                        mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
     186                        next_chap_sample = chap_info->sample;
     187                        chap_gain = malloc(sizeof(mpc_uint16_t) * 2 * chap_nb);
     188                        chap_peak = chap_gain + chap_nb;
     189                }
     190
    98191                if (j == 1) gain_init_analysis ( si.sample_freq );
    99192
    100                 while(1) {
     193                while (1) {
    101194                        mpc_frame_info frame;
    102                         int i;
     195                        int i = 0;
    103196
    104197                        frame.buffer = sample_buffer;
    105198                        mpc_demux_decode(demux, &frame);
    106                         if(frame.bits == -1) break;
    107 
    108                         for( i = 0; i < frame.samples; i++){
    109                                 left_samples[i] = sample_buffer[2 * i] * (1 << 15);
    110                                 right_samples[i] = sample_buffer[2 * i + 1] * (1 << 15);
    111                                 title_max = maxf(title_max, sample_buffer[2 * i]);
    112                                 title_max = maxf(title_max, sample_buffer[2 * i + 1]);
     199                        if (frame.bits == -1) break;
     200
     201                        while (next_chap_sample < cur_sample + frame.samples) {
     202                                int sample_nb = (int)(next_chap_sample - cur_sample);
     203
     204                                chap_max = _max(chap_max, analyze_get_max(sample_buffer + 2 * i, sample_nb));
     205
     206                                if (chap == 0) // first samples are not in a chapter
     207                                        gain_get_chapter();
     208                                else {
     209                                        chap_gain[chap - 1] = (mpc_uint16_t) (gain_get_chapter() * 256);
     210                                        chap_peak[chap - 1] = (mpc_uint16_t) (log10(chap_max * (1 << 15)) * 20 * 256);
     211                                }
     212                                chap++;
     213                                title_max = _max(title_max, chap_max);
     214                                chap_max = 0;
     215                                i += sample_nb;
     216                                cur_sample = next_chap_sample;
     217                                if (chap < chap_nb) {
     218                                        mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
     219                                        next_chap_sample = chap_info->sample;
     220                                } else
     221                                        next_chap_sample = mpc_int64_max;
    113222                        }
    114223
    115                         gain_analyze_samples(left_samples, right_samples, frame.samples, si.channels);
    116                 }
     224                        chap_max = _max(chap_max, analyze_get_max(sample_buffer + 2 * i, frame.samples - i));
     225                        cur_sample += frame.samples - i;
     226                }
     227
     228                if (chap_nb > 0) {
     229                        chap_gain[chap - 1] = (mpc_uint16_t) (gain_get_chapter() * 256);
     230                        chap_peak[chap - 1] = (mpc_uint16_t) (log10(chap_max * (1 << 15)) * 20 * 256);
     231                        write_chaps_gain(demux, argv[j], chap_gain, chap_peak);
     232                }
     233
     234                title_max = _max(title_max, chap_max);
     235                album_max = _max(album_max, title_max);
    117236
    118237                title_gain[j-1] = (mpc_uint16_t) (gain_get_title() * 256);
     
    120239                header_pos[j-1] = si.header_position + 4;
    121240
    122                 album_max = maxf(album_max, title_max);
    123 
    124241                mpc_demux_exit(demux);
    125242                mpc_reader_exit_stdio(&reader);
     243                if (chap_nb > 0)
     244                        free(chap_gain);
    126245        }
    127246
     
    129248        album_peak = (mpc_uint16_t) (log10(album_max * (1 << 15)) * 20 * 256);
    130249
    131         for( j = 0; j < argc - 1; j++) {
     250        for (j = 0; j < argc - 1; j++) {
    132251                unsigned char buffer[64];
    133252                mpc_bits_reader r;
     
    153272                size = mpc_bits_get_block(&r, &b);
    154273
    155                 while( memcmp(b.key, "RG", 2) != 0 ) {
     274                while (memcmp(b.key, "RG", 2) != 0 ) {
    156275                        header_pos[j] += b.size + size;
    157276                        fseek(file, header_pos[j], SEEK_SET);
Note: See TracChangeset for help on using the changeset viewer.