📄 audiochain.cc
字号:
// This file contains no license whatsoever. It is provided in the public // domain as an example of how to use the audio library//// Bruce Forsberg// forsberg@tns.net// ///* For pitch function */#include "aflib/aflibAudioPitch.h"/* For convert, pitch, mix and id functions */#include "aflib/aflibAudioFile.h"/* To print current time */#include "aflib/aflibDateTime.h"/* For mix function */#include "aflib/aflibAudioMixer.h"/* For formats function */#include "aflib/aflibFileItem.h"#include "aflib/aflibFile.h"/* For printing info, warnings and errors */#include "aflib/aflibDebug.h"/* For getopts */#include <unistd.h>#include "getopt.h"/* For getchar() */#include <stdio.h>/* For errno */#include <errno.h>/* For strerror() */#include <string.h>#include "audioChain.h"intaudioChain::exec(int argc, char* argv[]){ opterr = 0; int optchar; optchar = getopt(argc, argv, "+pimaht"); switch(optchar) { case 'p': optind--; pitch(argc, argv); break; case 'i': id(argc,argv); break; case 'm': mix(argc,argv); break; case 't': testall_formats(argc, argv); break; case 'a': formats(argc,argv); break; case 'h': help(argc, argv); date(argc, argv); break; case '?': optind--; convert(argc,argv); break; case EOF: default: convert(argc,argv); break; } return 0;}/* * This function will open a file for reading and open a file for writing. * * This is an example of the basics of what the osalp library can do. * * First a file is opened for reading. This can be a file of any of * osalp supported file type including a soundcard device. * * Second a file is opened for writing and at the same time the input * file is added as a parent. * * Once these two objects are created the process function is called which * reads the input and writes it to the output. * * The osalp library by default will perform any conversions needed to * write one file to the other. This includes data size, endianess, sample * rate and number of channels. * * i.e. if a user wants to convert a 16bit signed, 2 channel wave file with * a rate of 44100 to an 8bit unsigned, 1 channel au file with a rate of * 8000 they would use the following command line and this function would * do that for them. * * osalp filename1.wav -B -c1 -r8000 -f AU filename.au * * */voidaudioChain::convert(int argc, char **argv) { aflibConfig in_config, out_config; aflibStatus status = AFLIB_SUCCESS; string in_file, out_file; string in_format, out_format; aflibAudioFile* input; aflibAudioFile* output; /* by default try to auto detect the input file */ in_format = "AUTO"; /* let the user override default values if they would like */ parseConfigArgs(argc,argv,in_config,in_format); /* get the output file name */ if(optind >= argc) { /* aflib_fatal calls exit(1) */ aflib_fatal("No input file!"); } in_file = argv[optind]; optind++; /* get input aflibAudioFile object * this call will fill in_config with the appropriate values if * the filetype supports that */ input = new aflibAudioFile(in_format,in_file,&in_config,&status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening input file %s", in_file.c_str()); } /* copy the input config to the output config */ out_config = in_config; out_file = "/dev/audio"; out_format = "DEVICE"; /* let the user override these values if they would like */ parseConfigArgs(argc,argv,out_config,out_format); /* assume the last arg is a user specified output file */ if(optind < argc) { out_file = argv[optind]; } /* get output aflibAudioFile object. * The constructor will try to set the byte, endianess and rate from the * config passed to this constructor. * However this is only a suggestion and if the format doesn't support * the config then it will use default values. */ output = new aflibAudioFile(*input,out_format, out_file, &out_config, &status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening output file %s", out_file.c_str()); } /* process it */ process(output); delete output; delete input;}/* This is an example of how to use the aflibAudioPitch class. */voidaudioChain::pitch(int argc, char **argv) { aflibConfig in_config, out_config; aflibStatus status = AFLIB_SUCCESS; string in_file, out_file; string in_format, out_format; aflibAudioFile* input; aflibAudioPitch* pitch; aflibAudioFile* output; double pitch_factor = 1.0; int linear_interpolation = 0; int high_quality = 0; int filter_interpolation = 0; int optchar; static struct option long_options[] = { {"linear",0,&linear_interpolation,1}, {"high",0,&high_quality,1}, {"filter",0,&filter_interpolation,1} }; do { optchar = getopt_long(argc, argv, "+p:",long_options,NULL); switch(optchar) { case 'p': if(optind > argc){ aflib_fatal("Must specify a pitch value",argv[0]); } else { pitch_factor = atof(optarg); } break; case '?': optind--; break; case EOF: default: break; } } while(optchar != '?' && optchar != EOF); /* by default try to auto detect the input file */ in_format = "AUTO"; /* let the user override default values if they would like */ parseConfigArgs(argc,argv,in_config,in_format); /* get the output file name */ if(optind >= argc) { /* aflib_fatal calls exit(1) */ aflib_fatal("No input file!"); } in_file = argv[optind]; optind++; /* get input aflibAudioFile object * this call will fill in_config with the appropriate values if * the filetype supports that */ input = new aflibAudioFile(in_format,in_file,&in_config,&status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening input file %s", in_file.c_str()); } /* copy the input config to the output config */ out_config = in_config; out_file = "/dev/audio"; out_format = "DEVICE"; /* let the user override these values if they would like */ parseConfigArgs(argc,argv,out_config,out_format); /* assume the last arg is a user specified output file */ if(optind < argc) { out_file = argv[optind]; } /* Create the pitch object. */ pitch = new aflibAudioPitch(*input,pitch_factor,linear_interpolation,high_quality,filter_interpolation); /* get output aflibAudioFile object */ output = new aflibAudioFile(*pitch,out_format, out_file, &out_config, &status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening output file %s", out_file.c_str()); } /* process it */ process(output); delete output; delete input;}voidaudioChain::mix( int argc, char* argv[] ) { aflibConfig in_config1, in_config2, out_config; aflibStatus status = AFLIB_SUCCESS; string in_file1, in_file2, out_file; string in_format1, in_format2, out_format; aflibAudioFile* input1; aflibAudioFile* input2; aflibAudioMixer* mix; aflibAudioFile* output;/* Get first files format and name*/ /* by default try to auto detect the input file */ in_format1 = "AUTO"; /* let the user override default values if they would like */ parseConfigArgs(argc,argv,in_config1,in_format1); /* get the output file name */ if(optind >= argc) { /* aflib_fatal calls exit(1) */ aflib_fatal("Missing first input file!"); } in_file1 = argv[optind]; optind++; /* Get second files format and name*/ /* by default try to auto detect the input file */ in_format2 = "AUTO"; /* let the user override default values if they would like */ parseConfigArgs(argc,argv,in_config2,in_format2); /* get the output file name */ if(optind >= argc) { /* aflib_fatal calls exit(1) */ aflib_fatal("Missing second input file!"); } in_file2 = argv[optind]; optind++; /* Create input aflibAudioFile objects * this call will fill in_config with the appropriate values if * the filetype supports that */ input1 = new aflibAudioFile(in_format1,in_file1,&in_config1,&status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening input file %s", in_file1.c_str()); } else if(_verbose) { aflib_info("%s type: %s", in_file1.c_str(), input1->getFormat().c_str()); aflib_info("rate: %d, channels: %d, samples: %d", in_config1.getSamplesPerSecond(), in_config1.getChannels(), in_config1.getTotalSamples() ); } input2 = new aflibAudioFile(in_format2,in_file2,&in_config2,&status); if(status != AFLIB_SUCCESS) { aflib_fatal("Error opening input file %s", in_file2.c_str()); } else if(_verbose) { aflib_info("%s type: %s", in_file2.c_str(), input2->getFormat().c_str()); aflib_info("rate: %d, channels: %d, samples: %d", in_config2.getSamplesPerSecond(), in_config2.getChannels(), in_config2.getTotalSamples() ); } /* copy the input config to the output config */ out_config = in_config1; out_file = "/dev/audio"; out_format = "DEVICE"; /* let the user override these values if they would like */ parseConfigArgs(argc,argv,out_config,out_format); /* assume the last arg is a user specified output file */ if(optind < argc) { out_file = argv[optind]; } int output_channels = out_config.getChannels(); if(output_channels > 2) { aflib_fatal("mix function currently only supports one or two output channels"); } mix = new aflibAudioMixer(); // First add input1 to the mix int id = mix->addParent(*input1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -