/** * Convert to 24 bit packed samples (aka S24_3LE / S24_3BE). */ static const void * pcm_convert_24_packed(struct pcm_convert_state *state, const struct audio_format *src_format, const void *src_buffer, size_t src_size, const struct audio_format *dest_format, size_t *dest_size_r, GError **error_r) { assert(dest_format->format == SAMPLE_FORMAT_S24); /* use the normal 24 bit conversion first */ struct audio_format audio_format; audio_format_init(&audio_format, dest_format->sample_rate, SAMPLE_FORMAT_S24_P32, dest_format->channels); const int32_t *buffer; size_t buffer_size; buffer = pcm_convert_24(state, src_format, src_buffer, src_size, &audio_format, &buffer_size, error_r); if (buffer == NULL) return NULL; /* now convert to packed 24 bit */ unsigned num_samples = buffer_size / 4; size_t dest_size = num_samples * 3; uint8_t *dest = pcm_buffer_get(&state->pack_buffer, dest_size); pcm_pack_24(dest, buffer, num_samples, dest_format->reverse_endian); *dest_size_r = dest_size; return dest; }
bool audio_format_parse(struct audio_format *dest, const char *src, bool mask, GError **error_r) { uint32_t rate; enum sample_format sample_format; uint8_t channels; audio_format_clear(dest); /* parse sample rate */ #if GCC_CHECK_VERSION(4,7) /* workaround -Wmaybe-uninitialized false positive */ rate = 0; #endif if (!parse_sample_rate(src, mask, &rate, &src, error_r)) return false; if (*src++ != ':') { g_set_error(error_r, audio_parser_quark(), 0, "Sample format missing"); return false; } /* parse sample format */ #if GCC_CHECK_VERSION(4,7) /* workaround -Wmaybe-uninitialized false positive */ sample_format = SAMPLE_FORMAT_UNDEFINED; #endif if (!parse_sample_format(src, mask, &sample_format, &src, error_r)) return false; if (*src++ != ':') { g_set_error(error_r, audio_parser_quark(), 0, "Channel count missing"); return false; } /* parse channel count */ if (!parse_channel_count(src, mask, &channels, &src, error_r)) return false; if (*src != 0) { g_set_error(error_r, audio_parser_quark(), 0, "Extra data after channel count: %s", src); return false; } audio_format_init(dest, rate, sample_format, channels); assert(mask ? audio_format_mask_valid(dest) : audio_format_valid(dest)); return true; }
bool audio_format_init_checked(struct audio_format *af, unsigned long sample_rate, enum sample_format sample_format, unsigned channels, GError **error_r) { if (audio_check_sample_rate(sample_rate, error_r) && audio_check_sample_format(sample_format, error_r) && audio_check_channel_count(channels, error_r)) { audio_format_init(af, sample_rate, sample_format, channels); assert(audio_format_valid(af)); return true; } else return false; }
bool audio_format_parse(struct audio_format *dest, const char *src, bool mask, GError **error_r) { uint32_t rate; enum sample_format sample_format; uint8_t channels; audio_format_clear(dest); /* parse sample rate */ if (!parse_sample_rate(src, mask, &rate, &src, error_r)) return false; if (*src++ != ':') { g_set_error(error_r, audio_parser_quark(), 0, "Sample format missing"); return false; } /* parse sample format */ if (!parse_sample_format(src, mask, &sample_format, &src, error_r)) return false; if (*src++ != ':') { g_set_error(error_r, audio_parser_quark(), 0, "Channel count missing"); return false; } /* parse channel count */ if (!parse_channel_count(src, mask, &channels, &src, error_r)) return false; if (*src != 0) { g_set_error(error_r, audio_parser_quark(), 0, "Extra data after channel count: %s", src); return false; } audio_format_init(dest, rate, sample_format, channels); assert(mask ? audio_format_mask_valid(dest) : audio_format_valid(dest)); return true; }
static void fluidsynth_file_decode(struct decoder *decoder, const char *path_fs) { char setting_sample_rate[] = "synth.sample-rate"; /* char setting_verbose[] = "synth.verbose"; char setting_yes[] = "yes"; */ fluid_settings_t *settings; fluid_synth_t *synth; fluid_player_t *player; int ret; enum decoder_command cmd; /* set up fluid settings */ settings = new_fluid_settings(); if (settings == NULL) return; fluid_settings_setnum(settings, setting_sample_rate, sample_rate); /* fluid_settings_setstr(settings, setting_verbose, setting_yes); */ /* create the fluid synth */ synth = new_fluid_synth(settings); if (synth == NULL) { delete_fluid_settings(settings); return; } ret = fluid_synth_sfload(synth, soundfont_path, true); if (ret < 0) { g_warning("fluid_synth_sfload() failed"); delete_fluid_synth(synth); delete_fluid_settings(settings); return; } /* create the fluid player */ player = new_fluid_player(synth); if (player == NULL) { delete_fluid_synth(synth); delete_fluid_settings(settings); return; } ret = fluid_player_add(player, path_fs); if (ret != 0) { g_warning("fluid_player_add() failed"); delete_fluid_player(player); delete_fluid_synth(synth); delete_fluid_settings(settings); return; } /* start the player */ ret = fluid_player_play(player); if (ret != 0) { g_warning("fluid_player_play() failed"); delete_fluid_player(player); delete_fluid_synth(synth); delete_fluid_settings(settings); return; } /* initialization complete - announce the audio format to the MPD core */ struct audio_format audio_format; audio_format_init(&audio_format, sample_rate, SAMPLE_FORMAT_S16, 2); decoder_initialized(decoder, &audio_format, false, -1); while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) { int16_t buffer[2048]; const unsigned max_frames = G_N_ELEMENTS(buffer) / 2; /* read samples from fluidsynth and send them to the MPD core */ ret = fluid_synth_write_s16(synth, max_frames, buffer, 0, 2, buffer, 1, 2); if (ret != 0) break; cmd = decoder_data(decoder, NULL, buffer, sizeof(buffer), 0); if (cmd != DECODE_COMMAND_NONE) break; } /* clean up */ fluid_player_stop(player); fluid_player_join(player); delete_fluid_player(player); delete_fluid_synth(synth); delete_fluid_settings(settings); }
static void mod_decode(struct decoder *decoder, struct input_stream *is) { ModPlugFile *f; ModPlug_Settings settings; GByteArray *bdatas; struct audio_format audio_format; int ret; char audio_buffer[MODPLUG_FRAME_SIZE]; enum decoder_command cmd = DECODE_COMMAND_NONE; bdatas = mod_loadfile(decoder, is); if (!bdatas) { g_warning("could not load stream\n"); return; } ModPlug_GetSettings(&settings); /* alter setting */ settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; /* RESAMP */ settings.mChannels = 2; settings.mBits = 16; settings.mFrequency = 44100; /* insert more setting changes here */ ModPlug_SetSettings(&settings); f = ModPlug_Load(bdatas->data, bdatas->len); g_byte_array_free(bdatas, TRUE); if (!f) { g_warning("could not decode stream\n"); return; } audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); assert(audio_format_valid(&audio_format)); decoder_initialized(decoder, &audio_format, is->seekable, ModPlug_GetLength(f) / 1000.0); do { ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE); if (ret <= 0) break; cmd = decoder_data(decoder, NULL, audio_buffer, ret, 0); if (cmd == DECODE_COMMAND_SEEK) { float where = decoder_seek_where(decoder); ModPlug_Seek(f, (int)(where * 1000.0)); decoder_command_finished(decoder); } } while (cmd != DECODE_COMMAND_STOP); ModPlug_Unload(f); }
int main(int argc, char **argv) { GError *error = NULL; struct audio_format audio_format; bool ret; const char *encoder_name; const struct encoder_plugin *plugin; struct encoder *encoder; struct config_param *param; static char buffer[32768]; ssize_t nbytes; /* parse command line */ if (argc > 3) { g_printerr("Usage: run_encoder [ENCODER] [FORMAT] <IN >OUT\n"); return 1; } if (argc > 1) encoder_name = argv[1]; else encoder_name = "vorbis"; audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); /* create the encoder */ plugin = encoder_plugin_get(encoder_name); if (plugin == NULL) { g_printerr("No such encoder: %s\n", encoder_name); return 1; } param = config_new_param(NULL, -1); config_add_block_param(param, "quality", "5.0", -1); encoder = encoder_init(plugin, param, &error); if (encoder == NULL) { g_printerr("Failed to initialize encoder: %s\n", error->message); g_error_free(error); return 1; } /* open the encoder */ if (argc > 2) { ret = audio_format_parse(&audio_format, argv[2], false, &error); if (!ret) { g_printerr("Failed to parse audio format: %s\n", error->message); g_error_free(error); return 1; } } ret = encoder_open(encoder, &audio_format, &error); if (encoder == NULL) { g_printerr("Failed to open encoder: %s\n", error->message); g_error_free(error); return 1; } /* do it */ while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { ret = encoder_write(encoder, buffer, nbytes, &error); if (!ret) { g_printerr("encoder_write() failed: %s\n", error->message); g_error_free(error); return 1; } encoder_to_stdout(encoder); } ret = encoder_flush(encoder, &error); if (!ret) { g_printerr("encoder_flush() failed: %s\n", error->message); g_error_free(error); return 1; } encoder_to_stdout(encoder); }
int main(int argc, char **argv) { struct audio_output ao; struct audio_format audio_format; struct audio_format_string af_string; bool success; GError *error = NULL; char buffer[4096]; ssize_t nbytes; size_t frame_size, length = 0, play_length, consumed; if (argc < 3 || argc > 4) { g_printerr("Usage: run_output CONFIG NAME [FORMAT] <IN\n"); return 1; } audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); g_thread_init(NULL); /* read configuration file (mpd.conf) */ config_global_init(); success = config_read_file(argv[1], &error); if (!success) { g_printerr("%s:", error->message); g_error_free(error); return 1; } /* initialize the audio output */ if (!load_audio_output(&ao, argv[2])) return 1; /* parse the audio format */ if (argc > 3) { success = audio_format_parse(&audio_format, argv[3], false, &error); if (!success) { g_printerr("Failed to parse audio format: %s\n", error->message); g_error_free(error); return 1; } } /* open the audio output */ success = ao_plugin_open(ao.plugin, ao.data, &audio_format, &error); if (!success) { g_printerr("Failed to open audio output: %s\n", error->message); g_error_free(error); return 1; } g_printerr("audio_format=%s\n", audio_format_to_string(&audio_format, &af_string)); frame_size = audio_format_frame_size(&audio_format); /* play */ while (true) { if (length < sizeof(buffer)) { nbytes = read(0, buffer + length, sizeof(buffer) - length); if (nbytes <= 0) break; length += (size_t)nbytes; } play_length = (length / frame_size) * frame_size; if (play_length > 0) { consumed = ao_plugin_play(ao.plugin, ao.data, buffer, play_length, &error); if (consumed == 0) { g_printerr("Failed to play: %s\n", error->message); g_error_free(error); return 1; } assert(consumed <= length); assert(consumed % frame_size == 0); length -= consumed; memmove(buffer, buffer + consumed, length); } } /* cleanup and exit */ ao_plugin_close(ao.plugin, ao.data); ao_plugin_finish(ao.plugin, ao.data); g_mutex_free(ao.mutex); config_global_finish(); return 0; }
int main(int argc, char **argv) { struct audio_format audio_format; struct audio_format_string af_string; bool success; GError *error = NULL; struct filter *filter; const struct audio_format *out_audio_format; char buffer[4096]; size_t frame_size; if (argc < 3 || argc > 4) { g_printerr("Usage: run_filter CONFIG NAME [FORMAT] <IN\n"); return 1; } audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); /* initialize GLib */ g_thread_init(NULL); g_log_set_default_handler(my_log_func, NULL); /* read configuration file (mpd.conf) */ config_global_init(); success = config_read_file(argv[1], &error); if (!success) { g_printerr("%s:", error->message); g_error_free(error); return 1; } /* parse the audio format */ if (argc > 3) { success = audio_format_parse(&audio_format, argv[3], false, &error); if (!success) { g_printerr("Failed to parse audio format: %s\n", error->message); g_error_free(error); return 1; } } /* initialize the filter */ filter = load_filter(argv[2]); if (filter == NULL) return 1; /* open the filter */ out_audio_format = filter_open(filter, &audio_format, &error); if (out_audio_format == NULL) { g_printerr("Failed to open filter: %s\n", error->message); g_error_free(error); filter_free(filter); return 1; } g_printerr("audio_format=%s\n", audio_format_to_string(out_audio_format, &af_string)); frame_size = audio_format_frame_size(&audio_format); /* play */ while (true) { ssize_t nbytes; size_t length; const void *dest; nbytes = read(0, buffer, sizeof(buffer)); if (nbytes <= 0) break; dest = filter_filter(filter, buffer, (size_t)nbytes, &length, &error); if (dest == NULL) { g_printerr("Filter failed: %s\n", error->message); filter_close(filter); filter_free(filter); return 1; } nbytes = write(1, dest, length); if (nbytes < 0) { g_printerr("Failed to write: %s\n", strerror(errno)); filter_close(filter); filter_free(filter); return 1; } } /* cleanup and exit */ filter_close(filter); filter_free(filter); config_global_finish(); return 0; }