static void wav_data_init (struct wavpack_data *data) { data->sample_num = WavpackGetNumSamples (data->wpc); data->sample_rate = WavpackGetSampleRate (data->wpc); data->channels = WavpackGetReducedChannels (data->wpc); data->duration = data->sample_num / data->sample_rate; data->mode = WavpackGetMode (data->wpc); data->avg_bitrate = WavpackGetAverageBitrate (data->wpc, 1) / 1000; data->ok = 1; debug ("File opened. S_n %d. S_r %d. Time %d. Avg_Bitrate %d.", data->sample_num, data->sample_rate, data->duration, data->avg_bitrate ); }
bool attach(const char *filename) { ctx = WavpackOpenFileInput(filename, error_buff, OPEN_TAGS | OPEN_WVC | OPEN_NORMALIZE, 0); if (ctx == NULL) { return false; } sample_rate = WavpackGetSampleRate(ctx); num_channels = WavpackGetNumChannels(ctx); bytes_per_sample = WavpackGetBytesPerSample(ctx); input = (int32_t *)calloc(BUFFER_SIZE, num_channels * sizeof(int32_t)); output = (int16_t *)calloc(BUFFER_SIZE, num_channels * sizeof(int16_t)); memset (shaping_error, 0, sizeof (shaping_error)); mod->set_info(generate_title(filename, ctx), (int) (WavpackGetNumSamples(ctx) / sample_rate) * 1000, (int) WavpackGetAverageBitrate(ctx, true), (int) sample_rate, num_channels); play_gain = calculate_gain (ctx); DBG("gain value = %g\n", play_gain); return true; }
gboolean Wavpack_Header_Read_File_Info(gchar *filename, ET_File_Info *ETFileInfo) { WavpackContext *wpc; wpc = WavpackOpenFileInput(filename, NULL, 0, 0); if ( wpc == NULL ) { return FALSE; } ETFileInfo->version = WavpackGetVersion(wpc); /* .wvc correction file not counted */ ETFileInfo->bitrate = WavpackGetAverageBitrate(wpc, 0)/1000; ETFileInfo->samplerate = WavpackGetSampleRate(wpc); ETFileInfo->mode = WavpackGetNumChannels(wpc); ETFileInfo->size = WavpackGetFileSize(wpc); ETFileInfo->duration = WavpackGetNumSamples(wpc)/ETFileInfo->samplerate; WavpackCloseFile(wpc); return TRUE; }
double WavpackGetInstantBitrate (WavpackContext *wpc) { if (wpc && wpc->stream3) return WavpackGetAverageBitrate (wpc, TRUE); if (wpc && wpc->streams && wpc->streams [0] && wpc->streams [0]->wphdr.block_samples) { double output_time = (double) wpc->streams [0]->wphdr.block_samples / wpc->config.sample_rate; double input_size = 0; int si; for (si = 0; si < wpc->num_streams; ++si) { if (wpc->streams [si]->blockbuff) input_size += ((WavpackHeader *) wpc->streams [si]->blockbuff)->ckSize; if (wpc->streams [si]->block2buff) input_size += ((WavpackHeader *) wpc->streams [si]->block2buff)->ckSize; } if (output_time > 0.0 && input_size >= 1.0) return input_size * 8.0 / output_time; } return 0.0; }
static bool_t wv_play (InputPlayback * playback, const char * filename, VFSFile * file, int start_time, int stop_time, bool_t pause) { if (file == NULL) return FALSE; int32_t *input = NULL; void *output = NULL; int sample_rate, num_channels, bits_per_sample; unsigned num_samples; WavpackContext *ctx = NULL; VFSFile *wvc_input = NULL; bool_t error = FALSE; if (! wv_attach (filename, file, & wvc_input, & ctx, NULL, OPEN_TAGS | OPEN_WVC)) { fprintf (stderr, "Error opening Wavpack file '%s'.", filename); error = TRUE; goto error_exit; } sample_rate = WavpackGetSampleRate(ctx); num_channels = WavpackGetNumChannels(ctx); bits_per_sample = WavpackGetBitsPerSample(ctx); num_samples = WavpackGetNumSamples(ctx); if (!playback->output->open_audio(SAMPLE_FMT(bits_per_sample), sample_rate, num_channels)) { fprintf (stderr, "Error opening audio output."); error = TRUE; goto error_exit; } if (pause) playback->output->pause(TRUE); input = malloc(BUFFER_SIZE * num_channels * sizeof(uint32_t)); output = malloc(BUFFER_SIZE * num_channels * SAMPLE_SIZE(bits_per_sample)); if (input == NULL || output == NULL) goto error_exit; playback->set_gain_from_playlist(playback); pthread_mutex_lock (& mutex); playback->set_params(playback, (int) WavpackGetAverageBitrate(ctx, num_channels), sample_rate, num_channels); seek_value = (start_time > 0) ? start_time : -1; stop_flag = FALSE; playback->set_pb_ready(playback); pthread_mutex_unlock (& mutex); while (!stop_flag && (stop_time < 0 || playback->output->written_time () < stop_time)) { int ret; unsigned samples_left; /* Handle seek and pause requests */ pthread_mutex_lock (& mutex); if (seek_value >= 0) { playback->output->flush (seek_value); WavpackSeekSample (ctx, (int64_t) seek_value * sample_rate / 1000); seek_value = -1; } pthread_mutex_unlock (& mutex); /* Decode audio data */ samples_left = num_samples - WavpackGetSampleIndex(ctx); ret = WavpackUnpackSamples(ctx, input, BUFFER_SIZE); if (samples_left == 0) stop_flag = TRUE; else if (ret < 0) { fprintf (stderr, "Error decoding file.\n"); break; } else { /* Perform audio data conversion and output */ unsigned i; int32_t *rp = input; int8_t *wp = output; int16_t *wp2 = output; int32_t *wp4 = output; if (bits_per_sample == 8) { for (i = 0; i < ret * num_channels; i++, wp++, rp++) *wp = *rp & 0xff; } else if (bits_per_sample == 16) { for (i = 0; i < ret * num_channels; i++, wp2++, rp++) *wp2 = *rp & 0xffff; } else if (bits_per_sample == 24 || bits_per_sample == 32) { for (i = 0; i < ret * num_channels; i++, wp4++, rp++) *wp4 = *rp; } playback->output->write_audio(output, ret * num_channels * SAMPLE_SIZE(bits_per_sample)); } } error_exit: free(input); free(output); wv_deattach (wvc_input, ctx); stop_flag = TRUE; return ! error; }
static DB_playItem_t * wv_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { DB_FILE *fp = deadbeef->fopen (fname); if (!fp) { return NULL; } char error[80]; #ifdef TINYWV WavpackContext *ctx = WavpackOpenFileInput (wv_read_stream, fp, error); #else WavpackContext *ctx = WavpackOpenFileInputEx (&wsr, fp, NULL, error, 0, 0); #endif if (!ctx) { fprintf (stderr, "wavpack error: %s\n", error); deadbeef->fclose (fp); return NULL; } int totalsamples = WavpackGetNumSamples (ctx); int samplerate = WavpackGetSampleRate (ctx); float duration = (float)totalsamples / samplerate; DB_playItem_t *it = deadbeef->pl_item_alloc_init (fname, plugin.plugin.id); deadbeef->pl_add_meta (it, ":FILETYPE", "wv"); deadbeef->plt_set_item_duration (plt, it, duration); trace ("wv: totalsamples=%d, samplerate=%d, duration=%f\n", totalsamples, samplerate, duration); #if 0 int num = WavpackGetNumTagItems (ctx); trace ("num tag items: %d\n", num); for (int i = 0; i < num; i++) { char str[1024]; WavpackGetTagItemIndexed (ctx, i, str, sizeof (str)); trace ("tag item: %s\n", str); } #endif int apeerr = deadbeef->junk_apev2_read (it, fp); if (!apeerr) { trace ("wv: ape tag found\n"); } int v1err = deadbeef->junk_id3v1_read (it, fp); if (!v1err) { trace ("wv: id3v1 tag found\n"); } deadbeef->pl_add_meta (it, "title", NULL); char s[100]; snprintf (s, sizeof (s), "%lld", deadbeef->fgetlength (fp)); deadbeef->pl_add_meta (it, ":FILE_SIZE", s); snprintf (s, sizeof (s), "%d", WavpackGetBytesPerSample (ctx) * 8); deadbeef->pl_add_meta (it, ":BPS", s); snprintf (s, sizeof (s), "%d", WavpackGetNumChannels (ctx)); deadbeef->pl_add_meta (it, ":CHANNELS", s); snprintf (s, sizeof (s), "%d", WavpackGetSampleRate (ctx)); deadbeef->pl_add_meta (it, ":SAMPLERATE", s); snprintf (s, sizeof (s), "%d", (int)(WavpackGetAverageBitrate (ctx, 1) / 1000)); deadbeef->pl_add_meta (it, ":BITRATE", s); snprintf (s, sizeof (s), "%s", (WavpackGetMode (ctx) & MODE_FLOAT) ? "FLOAT" : "INTEGER"); deadbeef->pl_add_meta (it, ":WAVPACK_MODE", s); // embedded cue deadbeef->pl_lock (); const char *cuesheet = deadbeef->pl_find_meta (it, "cuesheet"); if (cuesheet) { trace ("found cuesheet: %s\n", cuesheet); DB_playItem_t *last = deadbeef->plt_insert_cue_from_buffer (plt, after, it, cuesheet, strlen (cuesheet), totalsamples, samplerate); if (last) { deadbeef->pl_unlock (); deadbeef->fclose (fp); WavpackCloseFile (ctx); deadbeef->pl_item_unref (it); deadbeef->pl_item_unref (last); return last; } } deadbeef->pl_unlock (); // cue file on disc DB_playItem_t *cue_after = deadbeef->plt_insert_cue (plt, after, it, totalsamples, samplerate); if (cue_after) { deadbeef->fclose (fp); WavpackCloseFile (ctx); deadbeef->pl_item_unref (it); deadbeef->pl_item_unref (cue_after); return cue_after; } after = deadbeef->plt_insert_item (plt, after, it); deadbeef->pl_item_unref (it); deadbeef->fclose (fp); WavpackCloseFile (ctx); return after; }
static gboolean xmms_wavpack_init (xmms_xform_t *xform) { xmms_wavpack_data_t *data; xmms_sample_format_t sample_format; gint samplerate; /* the maximum length of error really isn't defined... stupid */ gchar error[1024]; g_return_val_if_fail (xform, FALSE); if (!xmms_apetag_read (xform)) { XMMS_DBG ("Failed to read APEv2 tag"); } data = g_new0 (xmms_wavpack_data_t, 1); g_return_val_if_fail (data, FALSE); xmms_xform_private_data_set (xform, data); data->reader.read_bytes = wavpack_read_bytes; data->reader.get_pos = wavpack_get_pos; data->reader.set_pos_abs = wavpack_set_pos_abs; data->reader.set_pos_rel = wavpack_set_pos_rel; data->reader.push_back_byte = wavpack_push_back_byte; data->reader.get_length = wavpack_get_length; data->reader.can_seek = wavpack_can_seek; data->ctx = WavpackOpenFileInputEx (&data->reader, xform, xform, error, OPEN_TAGS, 0); if (!data->ctx) { xmms_log_error ("Unable to open wavpack file: %s", error); xmms_xform_private_data_set (xform, NULL); xmms_wavpack_free_data (data); return FALSE; } data->channels = WavpackGetNumChannels (data->ctx); data->bits_per_sample = WavpackGetBitsPerSample (data->ctx); data->num_samples = WavpackGetNumSamples (data->ctx); samplerate = WavpackGetSampleRate (data->ctx); xmms_xform_metadata_set_int (xform, XMMS_MEDIALIB_ENTRY_PROPERTY_DURATION, (int) (1000.0 * data->num_samples / samplerate)); xmms_xform_metadata_set_int (xform, XMMS_MEDIALIB_ENTRY_PROPERTY_SAMPLERATE, samplerate); xmms_xform_metadata_set_int (xform, XMMS_MEDIALIB_ENTRY_PROPERTY_BITRATE, (int) WavpackGetAverageBitrate (data->ctx, FALSE)); switch (data->bits_per_sample) { case 8: sample_format = XMMS_SAMPLE_FORMAT_S8; break; case 12: case 16: sample_format = XMMS_SAMPLE_FORMAT_S16; break; case 24: case 32: sample_format = XMMS_SAMPLE_FORMAT_S32; break; default: xmms_log_error ("Unsupported bits-per-sample in wavpack file: %d", data->bits_per_sample); xmms_xform_private_data_set (xform, NULL); xmms_wavpack_free_data (data); return FALSE; } xmms_xform_outdata_type_add (xform, XMMS_STREAM_TYPE_MIMETYPE, "audio/pcm", XMMS_STREAM_TYPE_FMT_FORMAT, sample_format, XMMS_STREAM_TYPE_FMT_CHANNELS, data->channels, XMMS_STREAM_TYPE_FMT_SAMPLERATE, samplerate, XMMS_STREAM_TYPE_END); return TRUE; }
static gboolean wv_play (InputPlayback * playback, const gchar * filename, VFSFile * file, gint start_time, gint stop_time, gboolean pause) { if (file == NULL) return FALSE; gint32 *input = NULL; void *output = NULL; gint sample_rate, num_channels, bits_per_sample; guint num_samples; WavpackContext *ctx = NULL; VFSFile *wvc_input = NULL; gboolean error = FALSE; if (! wv_attach (filename, file, & wvc_input, & ctx, NULL, OPEN_TAGS | OPEN_WVC)) { g_warning("Error opening Wavpack file '%s'.", filename); error = TRUE; goto error_exit; } sample_rate = WavpackGetSampleRate(ctx); num_channels = WavpackGetNumChannels(ctx); bits_per_sample = WavpackGetBitsPerSample(ctx); num_samples = WavpackGetNumSamples(ctx); if (!playback->output->open_audio(SAMPLE_FMT(bits_per_sample), sample_rate, num_channels)) { g_warning("Error opening audio output."); error = TRUE; goto error_exit; } if (pause) playback->output->pause(TRUE); input = g_malloc(BUFFER_SIZE * num_channels * sizeof(guint32)); output = g_malloc(BUFFER_SIZE * num_channels * SAMPLE_SIZE(bits_per_sample)); if (input == NULL || output == NULL) goto error_exit; playback->set_gain_from_playlist(playback); g_mutex_lock(ctrl_mutex); playback->set_params(playback, (gint) WavpackGetAverageBitrate(ctx, num_channels), sample_rate, num_channels); seek_value = (start_time > 0) ? start_time : -1; stop_flag = FALSE; playback->set_pb_ready(playback); g_mutex_unlock(ctrl_mutex); while (!stop_flag && (stop_time < 0 || playback->output->written_time () < stop_time)) { gint ret; guint samples_left; /* Handle seek and pause requests */ g_mutex_lock(ctrl_mutex); if (seek_value >= 0) { playback->output->flush (seek_value); WavpackSeekSample (ctx, (gint64) seek_value * sample_rate / 1000); seek_value = -1; g_cond_signal(ctrl_cond); } g_mutex_unlock(ctrl_mutex); /* Decode audio data */ samples_left = num_samples - WavpackGetSampleIndex(ctx); ret = WavpackUnpackSamples(ctx, input, BUFFER_SIZE); if (samples_left == 0) stop_flag = TRUE; else if (ret < 0) { g_warning("Error decoding file.\n"); break; } else { /* Perform audio data conversion and output */ guint i; gint32 *rp = input; gint8 *wp = output; gint16 *wp2 = output; gint32 *wp4 = output; if (bits_per_sample == 8) { for (i = 0; i < ret * num_channels; i++, wp++, rp++) *wp = *rp & 0xff; } else if (bits_per_sample == 16) { for (i = 0; i < ret * num_channels; i++, wp2++, rp++) *wp2 = *rp & 0xffff; } else if (bits_per_sample == 24 || bits_per_sample == 32) { for (i = 0; i < ret * num_channels; i++, wp4++, rp++) *wp4 = *rp; } playback->output->write_audio(output, ret * num_channels * SAMPLE_SIZE(bits_per_sample)); } } /* Flush buffer */ g_mutex_lock(ctrl_mutex); while (!stop_flag && playback->output->buffer_playing()) g_usleep(20000); g_cond_signal(ctrl_cond); g_mutex_unlock(ctrl_mutex); error_exit: g_free(input); g_free(output); wv_deattach (wvc_input, ctx); stop_flag = TRUE; playback->output->close_audio(); return ! error; }