iOS/iPhoneOS Equalizer with libsox - doing some effects

原文地址:http://uberblo.gs/2011/04/iosiphoneos-equalizer-with-libsox-doing-effects


Today i will show you how to do sound effects from within iOS using libsox.

It is pretty straight forward, the code is here:

#import "sox.h"

- (NSString *)transformAudioFileAtPath:(NSString *)path profile:(int)tprofile {
	static sox_format_t *in, *out; /* input and output files */
	sox_effects_chain_t * chain;
	sox_effect_t * e;
	char *args[10];

	NSString *target = [path stringByReplacingOccurrencesOfString:@".0.wav" withString:[NSString stringWithFormat:@".%i.wav", tprofile]];
	target = [target stringByReplacingOccurrencesOfString:@"Hoersimulator.app" withString:@"Documents"];
	[target retain];

	if ([[NSFileManager alloc] fileExistsAtPath:target])
		return target;

	/* All libSoX applications must start by initialising the SoX library */
	assert(sox_init() == SOX_SUCCESS);

	/* Open the input file (with default parameters) */
	assert(in = sox_open_read([path UTF8String], NULL, NULL, NULL));

	/* Open the output file; we must specify the output signal characteristics.
	 * Since we are using only simple effects, they are the same as the input
	 * file characteristics */
	assert(out = sox_open_write([target UTF8String], &in->signal, NULL, NULL, NULL, NULL));

	/* Create an effects chain; some effects need to know about the input
	 * or output file encoding so we provide that information here */
	chain = sox_create_effects_chain(&in->encoding, &out->encoding);

	/* The first effect in the effect chain must be something that can source
	 * samples; in this case, we use the built-in handler that inputs
	 * data from an audio file */
	e = sox_create_effect(sox_find_effect("input"));
	args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
	/* This becomes the first `effect' in the chain */
	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

	if (tprofile == 1) {

		e = sox_create_effect(sox_find_effect("lowpass"));
		args[0] = "2000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
		assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

		e = sox_create_effect(sox_find_effect("gain"));
		args[0] = "-10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
		assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
	}

	if (tprofile == 2) {

		e = sox_create_effect(sox_find_effect("lowpass"));
		args[0] = "1000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
		assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

		e = sox_create_effect(sox_find_effect("gain"));
		args[0] = "-25", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
		assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
	}


	/* Create the `vol' effect, and initialise it with the desired parameters: */
	e = sox_create_effect(sox_find_effect("vol"));
	args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
	/* Add the effect to the end of the effects processing chain: */
	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

	/* Create the `flanger' effect, and initialise it with default parameters: */
	e = sox_create_effect(sox_find_effect("flanger"));
	assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
	/* Add the effect to the end of the effects processing chain: */
	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);


	/* The last effect in the effect chain must be something that only consumes
	 * samples; in this case, we use the built-in handler that outputs
	 * data to an audio file */
	e = sox_create_effect(sox_find_effect("output"));
	args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

	/* Flow samples through the effects processing chain until EOF is reached */
	sox_flow_effects(chain, NULL, NULL);

	/* All done; tidy up: */
	sox_delete_effects_chain(chain);
	sox_close(out);
	sox_close(in);
	sox_quit();

	return target;
}

I've added a variable (int)tprofile which can be used for different effects without having to rewrite or copy everything. Simply apply your effects in an if-clause and be the king of the hill! At least know i am! :)

best regards fellow Apple survivors


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值