int osd_start_audio_stream(int stereo) { #if LOG_SOUND sound_log = fopen("sound.log", "w"); #endif // attempt to initialize directsound if (dsound_init()) return 1; // set the startup volume osd_set_mastervolume(attenuation); // determine the number of samples per frame samples_per_frame = (double)Machine->sample_rate / (double)Machine->screen[0].refresh; // compute how many samples to generate the first frame samples_left_over = samples_per_frame; samples_this_frame = (UINT32)samples_left_over; samples_left_over -= (double)samples_this_frame; // create wav file if( wavwrite != NULL ) { wavptr = wav_open( wavwrite, Machine->sample_rate, 2); } else { wavptr = NULL; } // return the samples to play the first frame return samples_this_frame; }
int sound_init(running_machine *machine) { /* handle -nosound */ nosound_mode = (Machine->sample_rate == 0); if (nosound_mode) Machine->sample_rate = 11025; /* count the speakers */ for (totalspeakers = 0; Machine->drv->speaker[totalspeakers].tag; totalspeakers++) ; VPRINTF(("total speakers = %d\n", totalspeakers)); /* initialize the OSD layer */ VPRINTF(("osd_start_audio_stream\n")); samples_this_frame = osd_start_audio_stream(1); if (!samples_this_frame) return 1; /* allocate memory for mix buffers */ leftmix = auto_malloc(Machine->sample_rate * sizeof(*leftmix)); rightmix = auto_malloc(Machine->sample_rate * sizeof(*rightmix)); finalmix = auto_malloc(Machine->sample_rate * sizeof(*finalmix)); /* allocate a global timer for sound timing */ sound_update_timer = mame_timer_alloc(NULL); /* initialize the streams engine */ VPRINTF(("streams_init\n")); streams_init(); /* now start up the sound chips and tag their streams */ VPRINTF(("start_sound_chips\n")); if (start_sound_chips()) return 1; /* then create all the speakers */ VPRINTF(("start_speakers\n")); if (start_speakers()) return 1; /* finally, do all the routing */ VPRINTF(("route_sound\n")); if (route_sound()) return 1; if (MAKE_WAVS) wavfile = wav_open("finalmix.wav", Machine->sample_rate, 2); /* enable sound by default */ global_sound_enabled = TRUE; /* register callbacks */ config_register("mixer", sound_load, sound_save); add_pause_callback(machine, sound_pause); add_reset_callback(machine, sound_reset); add_exit_callback(machine, sound_exit); return 0; }
void sound_init(running_machine *machine) { attotime update_frequency = SOUND_UPDATE_FREQUENCY; const char *filename; /* handle -nosound */ nosound_mode = !options_get_bool(mame_options(), OPTION_SOUND); if (nosound_mode) Machine->sample_rate = 11025; /* count the speakers */ for (totalspeakers = 0; Machine->drv->speaker[totalspeakers].tag; totalspeakers++) ; VPRINTF(("total speakers = %d\n", totalspeakers)); /* allocate memory for mix buffers */ leftmix = auto_malloc(Machine->sample_rate * sizeof(*leftmix)); rightmix = auto_malloc(Machine->sample_rate * sizeof(*rightmix)); finalmix = auto_malloc(Machine->sample_rate * sizeof(*finalmix)); /* allocate a global timer for sound timing */ sound_update_timer = timer_alloc(sound_update, NULL); timer_adjust(sound_update_timer, update_frequency, 0, update_frequency); /* initialize the streams engine */ VPRINTF(("streams_init\n")); streams_init(machine, update_frequency.attoseconds); /* now start up the sound chips and tag their streams */ VPRINTF(("start_sound_chips\n")); start_sound_chips(); /* then create all the speakers */ VPRINTF(("start_speakers\n")); start_speakers(); /* finally, do all the routing */ VPRINTF(("route_sound\n")); route_sound(); /* open the output WAV file if specified */ filename = options_get_string(mame_options(), OPTION_WAVWRITE); if (filename[0] != 0) wavfile = wav_open(filename, machine->sample_rate, 2); /* enable sound by default */ global_sound_enabled = TRUE; sound_muted = FALSE; sound_set_attenuation(options_get_int(mame_options(), OPTION_VOLUME)); /* register callbacks */ config_register("mixer", sound_load, sound_save); add_pause_callback(machine, sound_pause); add_reset_callback(machine, sound_reset); add_exit_callback(machine, sound_exit); }
int encode_to_wav(FILE *payload, const char *out_fname, const quiet_encoder_options *opt) { SNDFILE *wav = wav_open(out_fname, sample_rate); if (wav == NULL) { printf("failed to open wav file for writing\n"); return 1; } quiet_encoder *e = quiet_encoder_create(opt, sample_rate); size_t block_len = 16384; uint8_t *readbuf = malloc(block_len * sizeof(uint8_t)); size_t samplebuf_len = 16384; quiet_sample_t *samplebuf = malloc(samplebuf_len * sizeof(quiet_sample_t)); quiet_encoder_clamp_frame_len(e, samplebuf_len); bool done = false; if (readbuf == NULL) { return 1; } if (samplebuf == NULL) { return 1; } while (!done) { size_t nread = fread(readbuf, sizeof(uint8_t), block_len, payload); if (nread == 0) { break; } else if (nread < block_len) { done = true; } size_t frame_len = quiet_encoder_get_frame_len(e); for (size_t i = 0; i < nread; i += frame_len) { frame_len = (frame_len > (nread - i)) ? (nread - i) : frame_len; quiet_encoder_send(e, readbuf + i, frame_len); } size_t written = samplebuf_len; while (written == samplebuf_len) { written = quiet_encoder_emit(e, samplebuf, samplebuf_len); wav_write(wav, samplebuf, written); } } quiet_encoder_destroy(e); free(readbuf); free(samplebuf); wav_close(wav); return 0; }
int main(int ac, char** av) { wav_handle_t iw; wav_handle_t ow; cmd_handle_t cmd; int err = -1; if (cmd_init(&cmd, ac - 1, av + 1)) { PERROR(); goto on_error_0; } if ((cmd.flags & CMD_FLAG_IPATH) == 0) { PERROR(); goto on_error_0; } if ((cmd.flags & CMD_FLAG_OPATH) == 0) { PERROR(); goto on_error_0; } if (wav_open(&iw, cmd.ipath)) { PERROR(); goto on_error_0; } if (wav_copy(&ow, &iw)) { PERROR(); goto on_error_1; } if (wav_write(&ow, cmd.opath)) { PERROR(); goto on_error_2; } err = 0; on_error_2: wav_close(&ow); on_error_1: wav_close(&iw); on_error_0: return err; }
int osd_start_audio_stream(int stereo) { char buf[256]; if (current_testcase.wavwrite) { snprintf(buf, sizeof(buf) / sizeof(buf[0]), "snap/_%s.wav", current_testcase.name); wavptr = wav_open(buf, Machine->sample_rate, 2); } else { wavptr = NULL; } samples_this_frame = (int) ((double)Machine->sample_rate / (double)Machine->refresh_rate); return samples_this_frame; }
int osd_start_audio_stream(int stereo) { char buf[256]; if (current_testcase.wavwrite) { snprintf(buf, ARRAY_LENGTH(buf), "snap/_%s.wav", current_testcase.name); wavptr = wav_open(buf, machine.sample_rate(), 2); } else { wavptr = NULL; } samples_this_frame = (int) ((double)machine.sample_rate() / (double)machine.screen[0].refresh); return samples_this_frame; }
static int isowavPlayLBA(int LBA) { isowavLBA = LBA; for (isowavTrack = isowavTOC->FirstTrack - 1; isowavTrack < isowavTOC->LastTrack; isowavTrack++) { if (isowavLBA < isowavMSFToLBA(isowavTOC->TrackData[isowavTrack + 1].Address)) { break; } } if (isowavTrack >= isowavTOC->LastTrack) { return 1; } bprintf(PRINT_IMPORTANT, _T(" playing track %2i - %s\n"), isowavTrack + 1, isowavTOC->TrackData[isowavTrack].Filename); isowavFile = _tfopen(isowavTOC->TrackData[isowavTrack].Filename, _T("rb")); if (isowavFile == NULL) { return 1; } if( _tcsstr(isowavTOC->TrackData[isowavTrack].Filename, _T(".wav")) || _tcsstr(isowavTOC->TrackData[isowavTrack].Filename, _T(".WAV"))) { // is a wav, no need to keep this file pointer if (isowavFile) { fclose(isowavFile); isowavFile = NULL; } if(wav_open(isowavTOC->TrackData[isowavTrack].Filename)) { wav_play(); } else { // error creating the WAV stream return 1; } } //dprintf(_T("*** WAV: wBitsPerSample: %d \n"), wav->g_pStreamingSound->m_pWaveFile->m_pwfx->wBitsPerSample); //dprintf(_T("*** WAV: nAvgBytesPerSec: %d \n"), wav->g_pStreamingSound->m_pWaveFile->m_pwfx->nAvgBytesPerSec); //dprintf(_T("*** WAV: m_dwSize: %d \n"), wav->g_pStreamingSound->m_pWaveFile->m_dwSize); //dprintf(_T("*** WAV: nBlockAlign: %d \n"), wav->g_pStreamingSound->m_pWaveFile->m_pwfx->nBlockAlign); isowavLBA = isowavMSFToLBA(isowavTOC->TrackData[isowavTrack].Address); CDEmuStatus = playing; return 0; }
void play_wav_file(const uint8_t *fname) { if (! wav_open((const char *)fname)) return; // Don't enable yet. Do that in play_fill_buffer() below after we've filled the first 2 buffers gState = STATE_PLAYING_FROM_SD; gCtrlFlags = CTRL_FLAG_KICKSTART; // Tell play_fill_buffer() handler below to fill buffers then start DMA dma_begin(DMA_CFG_PLAY, (gWAVInfo.mChannels==2)); gActiveDMABuffer = 1; // Trust me, it's right (look at how play_fill_buffer() works on kickstarting) gDMABufferDone = 1; // Trust me, it's right (look at how play_fill_buffer() works on kickstarting) #if 0 // r1: let user fully control OutputEnable to avoid clicks and pops I2C_shutdown_enable(0); #endif }
int main(int argc, char **argv) { int width, height; double colour[3]; struct wav_file *wav; float samples[CHUNK_SIZE]; int sample_count; struct graph *gr; cairo_surface_t *surface; /* Validate and parse command-line arguments */ if(parse_arguments(argc, argv, &width, &height, colour) != 0) return 1; /* Open input file */ if( !(wav = wav_open(argv[1]))) { fprintf(stderr, "Unable to open input audio file %s\n", argv[1]); return 1; } /* Buffer all samples from input file. */ gr = graph_init(); while((sample_count = wav_read_samples(wav, samples, CHUNK_SIZE)) > 0) graph_buffer_samples(gr, samples, sample_count); /* Close input file */ wav_close(wav); /* Draw graph and output to PNG file using Cairo */ surface = graph_draw(gr, width, height, colour); if(cairo_surface_write_to_png(surface, argv[2]) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "Error writing graph to PNG file\n"); return 1; } graph_surface_destroy(surface); graph_destroy(gr); return 0; }
void sound_init(running_machine *machine) { sound_private *global; const char *filename; machine->sound_data = global = auto_alloc_clear(machine, sound_private); /* handle -nosound */ global->nosound_mode = !options_get_bool(machine->options(), OPTION_SOUND); if (global->nosound_mode) machine->sample_rate = 11025; /* count the speakers */ VPRINTF(("total speakers = %d\n", speaker_output_count(machine->config))); /* allocate memory for mix buffers */ global->leftmix = auto_alloc_array(machine, INT32, machine->sample_rate); global->rightmix = auto_alloc_array(machine, INT32, machine->sample_rate); global->finalmix = auto_alloc_array(machine, INT16, machine->sample_rate); /* allocate a global timer for sound timing */ global->update_timer = timer_alloc(machine, sound_update, NULL); timer_adjust_periodic(global->update_timer, STREAMS_UPDATE_ATTOTIME, 0, STREAMS_UPDATE_ATTOTIME); /* finally, do all the routing */ VPRINTF(("route_sound\n")); route_sound(machine); /* open the output WAV file if specified */ filename = options_get_string(machine->options(), OPTION_WAVWRITE); if (filename[0] != 0) global->wavfile = wav_open(filename, machine->sample_rate, 2); /* enable sound by default */ global->enabled = TRUE; global->muted = FALSE; sound_set_attenuation(machine, options_get_int(machine->options(), OPTION_VOLUME)); /* register callbacks */ config_register(machine, "mixer", sound_load, sound_save); machine->add_notifier(MACHINE_NOTIFY_PAUSE, sound_pause); machine->add_notifier(MACHINE_NOTIFY_RESUME, sound_resume); machine->add_notifier(MACHINE_NOTIFY_RESET, sound_reset); machine->add_notifier(MACHINE_NOTIFY_EXIT, sound_exit); }
static int yw_open_audio(YWPrivateData *pd, const char *filename, vob_t *vob) { WAVError err; int rate; pd->wav = wav_open(filename, WAV_WRITE, &err); if (!pd->wav) { tc_log_error(MOD_NAME, "failed to open audio stream file '%s'" " (reason: %s)", filename, wav_strerror(err)); return TC_ERROR; } rate = (vob->mp3frequency != 0) ?vob->mp3frequency :vob->a_rate; wav_set_bits(pd->wav, vob->dm_bits); wav_set_rate(pd->wav, rate); wav_set_bitrate(pd->wav, vob->dm_chan * rate * vob->dm_bits/8); wav_set_channels(pd->wav, vob->dm_chan); return TC_OK; }
static int yw_open_audio(YWPrivateData *pd, vob_t *vob) { WAVError err; if (!vob->audio_in_file || !strcmp(vob->video_in_file, vob->audio_in_file)) { tc_log_error(MOD_NAME, "missing or bad audio source file," " please specify it"); return(TC_IMPORT_ERROR); } pd->wav = wav_open(vob->audio_in_file, WAV_READ, &err); if (!pd->wav) { tc_log_error(MOD_NAME, "can't open audio source '%s'" " (reason: %s)", vob->audio_in_file, wav_strerror(err)); } else { if (verbose >= TC_DEBUG) { tc_log_info(MOD_NAME, "using audio source: %s", vob->audio_in_file); } } return(TC_IMPORT_OK); }
static pcm_reader_t *open_input(aacenc_param_ex_t *params) { pcm_io_context_t io = { 0 }; pcm_reader_t *reader = 0; struct stat stb = { 0 }; if ((params->input_fp = aacenc_fopen(params->input_filename, "rb")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params->input_filename, strerror(errno)); goto END; } io.cookie = params->input_fp; if (fstat(fileno(params->input_fp), &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFREG) io.vtbl = &pcm_io_vtbl; else io.vtbl = &pcm_io_vtbl_noseek; if (params->is_raw) { int bytes_per_channel; pcm_sample_description_t desc = { 0 }; if (parse_raw_spec(params->raw_format, &desc) < 0) { fprintf(stderr, "ERROR: invalid raw-format spec\n"); goto END; } desc.sample_rate = params->raw_rate; desc.channels_per_frame = params->raw_channels; bytes_per_channel = (desc.bits_per_channel + 7) / 8; desc.bytes_per_frame = params->raw_channels * bytes_per_channel; if ((reader = raw_open(&io, &desc)) == 0) { fprintf(stderr, "ERROR: failed to open raw input\n"); goto END; } } else { int c; ungetc(c = getc(params->input_fp), params->input_fp); switch (c) { case 'R': if ((reader = wav_open(&io, params->ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } break; case 'c': params->source_tag_ctx.add = aacenc_add_tag_entry_to_store; params->source_tag_ctx.add_ctx = ¶ms->source_tags; if ((reader = caf_open(&io, aacenc_translate_generic_text_tag, ¶ms->source_tag_ctx)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } break; default: fprintf(stderr, "ERROR: unsupported input file\n"); goto END; } } if ((reader = pcm_open_sint16_converter(reader)) != 0) reader = extrapolater_open(reader); return reader; END: return 0; }
int discrete_sh_start (const struct MachineSound *msound) { struct discrete_sound_block *intf; int loop=0,loop2=0,search=0,failed=0; #ifdef DISCRETE_WAVELOG wav_file = wav_open("discrete.wav", Machine->sample_rate, ((Machine->drv->sound_attributes&SOUND_SUPPORTS_STEREO) == SOUND_SUPPORTS_STEREO) ? 2: 1); #endif #ifdef DISCRETE_DEBUGLOG if(!disclogfile) disclogfile=fopen("discrete.log", "w"); #endif /* Initialise */ intf=msound->sound_interface; node_count=0; /* Sanity check and node count */ discrete_log("discrete_sh_start() - Doing node list sanity check"); while(1) { /* Check the node parameter is a valid node */ if(intf[node_count].node<NODE_START || intf[node_count].node>NODE_END) { logerror("discrete_sh_start() - Invalid node number on node %02d descriptor\n",node_count); return 1; } if(intf[node_count].type>DSO_OUTPUT) { logerror("discrete_sh_start() - Invalid function type on node %02d descriptor\n",node_count); return 1; } /* Node count must include the NULL node as well */ if(intf[node_count].type==DSS_NULL) { node_count++; break; } node_count++; /* Sanity check */ if(node_count>DISCRETE_MAX_NODES) { logerror("discrete_sh_start() - Upper limit of %d nodes exceeded, have you terminated the interface block.",DISCRETE_MAX_NODES); return 1; } } discrete_log("discrete_sh_start() - Sanity check counted %d nodes", node_count); /* Allocate memory for the context array and the node execution order array */ if((running_order=malloc(node_count*sizeof(struct node_description*)))==NULL) { logerror("discrete_sh_start() - Failed to allocate running order array.\n"); return 1; } else { /* Initialise memory */ memset(running_order,0,node_count*sizeof(struct node_description*)); } if((node_list=malloc(node_count*sizeof(struct node_description)))==NULL) { logerror("discrete_sh_start() - Failed to allocate context list array.\n"); return 1; } else { /* Initialise memory */ memset(node_list,0,node_count*sizeof(struct node_description)); /* Initialise structs */ for(loop=0;loop<node_count;loop++) { for(loop2=0;loop2<DISCRETE_MAX_INPUTS;loop2++) { node_list[loop].input[loop2]=0.0; node_list[loop].input_node[loop2]=NULL; } } } discrete_log("discrete_sh_start() - Malloc completed", node_count); /* Work out the execution order */ /* FAKE IT FOR THE MOMENT, EXECUTE IN ORDER */ for(loop=0;loop<node_count;loop++) { running_order[loop]=&node_list[loop]; } discrete_log("discrete_sh_start() - Running order sort completed", node_count); /* Configure the input node pointers, the find_node function wont work without the node ID setup beforehand */ for(loop=0;loop<node_count;loop++) node_list[loop].node=intf[loop].node; failed=0; /* Duplicate node number test */ for(loop=0;loop<node_count;loop++) { for(loop2=0;loop2<node_count;loop2++) { if(node_list[loop].node==node_list[loop2].node && loop!=loop2) { logerror("discrete_sh_start - Node NODE_%02d defined more than once\n",node_list[loop].node-NODE_00); failed=1; } } } /* Initialise and start all of the objects */ for(loop=0;loop<node_count;loop++) { /* Configure the input node pointers */ node_list[loop].node=intf[loop].node; node_list[loop].output=0; node_list[loop].active_inputs=intf[loop].active_inputs; for(loop2=0;loop2<intf[loop].active_inputs;loop2++) { node_list[loop].input[loop2]=intf[loop].initial[loop2]; node_list[loop].input_node[loop2]=find_node(intf[loop].input_node[loop2]); } node_list[loop].name=intf[loop].name; node_list[loop].custom=intf[loop].custom; /* Check that all referenced nodes have actually been found */ for(loop2=0;loop2<intf[loop].active_inputs;loop2++) { if(node_list[loop].input_node[loop2]==NULL && intf[loop].input_node[loop2]>=NODE_START && intf[loop].input_node[loop2]<=NODE_END) { logerror("discrete_sh_start - Node NODE_%02d referenced a non existant node NODE_%02d\n",node_list[loop].node-NODE_00,intf[loop].input_node[loop2]-NODE_00); failed=1; } } /* Try to find the simulation module in the module list table */ search=0; while(1) { if(module_list[search].type==intf[loop].type) { node_list[loop].module=search; discrete_log("discrete_sh_start() - Calling init for %s",module_list[search].name); if(module_list[search].init) if(((*module_list[search].init)(&node_list[loop]))==1) failed=1; break; } else if(module_list[search].type==DSS_NULL) { if(intf[loop].type==DSS_NULL) break; else { logerror("discrete_sh_start() - Invalid DSS/DST/DSO module type specified in interface, item %02d\n",loop+1); failed=1; break; } } search++; } } /* Setup the output node */ if((output_node=find_node(NODE_OP))==NULL) { logerror("discrete_sh_start() - Couldn't find an output node"); failed=1; } discrete_log("discrete_sh_start() - Nodes initialised", node_count); /* Different setup for Mono/Stereo systems */ if ((Machine->drv->sound_attributes&SOUND_SUPPORTS_STEREO) == SOUND_SUPPORTS_STEREO) { int vol[2]; const char *stereo_names[2] = { "Discrete Left", "Discrete Right" }; vol[0] = MIXER((int)output_node->input[2],MIXER_PAN_LEFT); vol[1] = MIXER((int)output_node->input[2],MIXER_PAN_RIGHT); /* Initialise a stereo, stream, we always use stereo even if only a mono system */ discrete_stream=stream_init_multi(2,stereo_names,vol,Machine->sample_rate,0,discrete_stream_update_stereo); discrete_log("discrete_sh_start() - Stereo Audio Stream Initialised", node_count); discrete_stereo=1; } else { int vol = (int)output_node->input[2]; /* Initialise a stereo, stream, we always use stereo even if only a mono system */ discrete_stream=stream_init("Discrete Sound",vol,Machine->sample_rate,0,discrete_stream_update_mono); discrete_log("discrete_sh_start() - Mono Audio Stream Initialised", node_count); } if(discrete_stream==-1) { logerror("discrete_sh_start - Stream init returned an error\n"); failed=1; } /* Report success or fail */ if(!failed) init_ok=1; /* Now reset the system to a sensible state */ discrete_sh_reset(); discrete_log("discrete_sh_start() - Nodes reset", node_count); return failed; }
int main(int argc, char **argv) { wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; m4af_io_callbacks_t m4af_io = { read_callback, write_callback, seek_callback, tell_callback }; aacenc_param_ex_t params = { 0 }; int result = 2; FILE *ifp = 0; FILE *ofp = 0; char *output_filename = 0; #ifdef USE_LIBSNDFILE SNDFILE* snd = NULL; SF_INFO snd_info; pcm_sample_description_t snd_desc = { 0 }; #else wav_reader_t *wavf = 0; #endif HANDLE_AACENCODER encoder = 0; AACENC_InfoStruct aacinfo = { 0 }; m4af_ctx_t *m4af = 0; const pcm_sample_description_t *sample_format; int downsampled_timescale = 0; int frame_count = 0; struct stat stb = { 0 }; setlocale(LC_CTYPE, ""); setbuf(stderr, 0); if (parse_options(argc, argv, ¶ms) < 0) return 1; #ifdef USE_LIBSNDFILE if ((snd = sf_open (params.input_filename, SFM_READ, &snd_info)) == NULL) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } #ifdef USE_LIBSAMPLERATE if(params.resample) { snd_desc.sample_rate = params.resample; printf("resampling to %dhz\n", snd_desc.sample_rate); } else { snd_desc.sample_rate = snd_info.samplerate; } snd_desc.sample_type = PCM_TYPE_FLOAT; // always -- libsndfile does the conversion for us snd_desc.bits_per_channel = sizeof(float)*8; #else snd_desc.sample_rate = snd_info.samplerate; snd_desc.sample_type = PCM_TYPE_SINT; // always -- libsndfile does the conversion for us snd_desc.bits_per_channel = sizeof(short)*8; #endif snd_desc.channels_per_frame = snd_info.channels; snd_desc.bytes_per_frame = snd_info.channels * (snd_desc.bits_per_channel / 8); snd_desc.channel_mask = 0; sample_format = &snd_desc; #else if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename, strerror(errno)); goto END; } if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) { wav_io.seek = 0; wav_io.tell = 0; } if (!params.is_raw) { if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } } else { int bytes_per_channel; pcm_sample_description_t desc = { 0 }; if (parse_raw_spec(params.raw_format, &desc) < 0) { fprintf(stderr, "ERROR: invalid raw-format spec\n"); goto END; } desc.sample_rate = params.raw_rate; desc.channels_per_frame = params.raw_channels; bytes_per_channel = (desc.bits_per_channel + 7) / 8; desc.bytes_per_frame = params.raw_channels * bytes_per_channel; if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) { fprintf(stderr, "ERROR: failed to open raw input\n"); goto END; } } sample_format = wav_get_format(wavf); #endif if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, &aacinfo) < 0) goto END; if (!params.output_filename) { const char *ext = params.transport_format ? ".aac" : ".m4a"; output_filename = generate_output_filename(params.input_filename, ext); params.output_filename = output_filename; } if ((ofp = aacenc_fopen(params.output_filename, "wb+")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, strerror(errno)); goto END; } handle_signals(); if (!params.transport_format) { uint32_t scale; unsigned framelen = aacinfo.frameLength; int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE); if (sbr_mode && !sig_mode) downsampled_timescale = 1; scale = sample_format->sample_rate >> downsampled_timescale; if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0) goto END; m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf, aacinfo.confSize); m4af_set_fixed_frame_duration(m4af, 0, framelen >> downsampled_timescale); m4af_set_priming_mode(m4af, params.gapless_mode + 1); m4af_begin_write(m4af); }