我正在创建一个鼓应用程序,我必须做两件事
播放各种声音
如果用户选择录制声音,我必须制作用户播放的音乐的音频文件
该应用程序的工作原理如下 - 当用户点击按钮A时,我会播放SoundA。如果用户点击B,我会播放SoundB,同样......
所以要播放而不是录制声音,我在想的是,当用户点击“开始录制”按钮时 - 如果我保持用户点击按钮的时间戳,我可以通过混合我已经拥有的声音来重现质量更好的最终输出。
e.g。用户在00:01秒按下按钮A,在00:05秒按下按钮B,依此类推
我不确定的是 - 如何创建音频文件并在特定时间段插入这些声音,即声音A在00:01秒,声音B在00:05秒......等等。
对此的任何帮助都会有很大的帮助。
提前谢谢。
答案 0 :(得分:0)
两部分答案:
解决方案1 :想到的一个想法是,不是尝试录制生成的音频,而是可以录制他们按下的按钮,并在播放文件时重新创建序列。
所以你的录音"文件将是这样的:
所以,当他们想要播放"录音"你设置了一个等待适当时间的计时器,然后播放适当的声音。因此,通过上面的示例,您将:
解决方案2 :如果您希望能够在应用程序之外导出录制内容,这样更合适,因此需要一个真实的音频文件。
这假设您以原始格式提供声音(此处可使用WAV,但必须首先解码MP3或其他格式)。这也假设所有声音在采样/秒,立体声/单声道,8位/ 16位采样等方面都是一致的。然后当他们完成录制时,分配一个阵列(或两个,如果你做立体声)相应的尺寸。因此,如果录制时间是11秒,并且您的声音是44位采样/秒的16位单声道,那么您将分配485100个短路阵列(485100 = 11 * 44100)。如果它太大,那么您可能需要在较短的段(例如一次一秒)中执行此操作,并准备好让您的声音在段之间交叉。 16位值是带符号的短路,因此您应该能够将数组初始化为0,并在适当的位置添加每个声音。请注意,8位值是带有偏移的无符号值,因此在添加样本时必须调整偏移量。然后以格式编写文件(可能是WAV,请参阅http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html以获取WAV文件格式的描述)。如果您希望文件是MP3或其他压缩格式,那么几乎肯定有类可以为您进行压缩,但我在这方面无法帮助您。
编辑:创建.WAV文件的代码示例(在C for Windows中):
/* Create a .WAV file with a specified set of chirps */
#include<stdio.h>
#include<sys\stat.h>
#include<ctype.h>
/* length of one bit in 44100Hz units */
#define BIT_LEN 88.5
/* length of one half-cycle */
#define HALF_CYCLE (BIT_LEN/8)
/* samples per second */
#define SAMP_RATE 44100
/* seconds before chirps start */
#define LEAD_SILENCE 5
struct {
unsigned long ID; /* FILE ID "RIFF" */
unsigned long Len; /* file length (excluding 8-byte header) */
unsigned long DataType; /* Data type 'WAVE' (start of main file) */
} FileHeader;
struct {
unsigned long ID; /* chunk ID "DATA" */
unsigned long Len; /* chunk length */
} DataHeader;
struct {
unsigned long ID; /* chunk ID "FMT " */
unsigned long Len; /* chunk length */
short FormatTag; /* format (1 if uncompressed) */
unsigned short Channels; /* # of channels (mono, stereo, etc) */
unsigned long SampPerSec; /* # of samples per second */
unsigned long AvgBytesPerSec; /* # of bytes played per second */
unsigned short BlockAlign; /* Size of a 'frame' in bytes */
unsigned short BitsPerSamp; /* bits per sample */
} FormatHeader;
#define ID_RIFF 0x46464952 /* 'RIFF' (FFIR) */
#define ID_WAVE 0x45564157 /* 'WAVE' (EVAW) */
#define ID_DATA 0x61746164 /* 'data' (atad) */
#define ID_FMT 0x20746d66 /* 'fmt ' ( tmf)*/
/* count # of pos/negative parts done */
int pcount=0;
/* how many will fit on a normal line? */
#define PCOUNT_MAX 14
void disp_id( char * lbl, long ID )
{
union {
long l;
char c[6];
} u;
u.l = ID;
u.c[4] = 0;
printf( lbl, u.c );
}
/* note where the samples begin */
unsigned long SampleStart; /* where do samples begin? */
/* one-second chirp segment */
short NewChirp[SAMP_RATE], /* 1-second block with chirp */
Silence[SAMP_RATE]; /* 1-second silence */
/* insert a byte into the chirp */
/* location is specified in bit time units */
void insert_byte( int location, int value )
{
int i, j;
int data; /* value to write to item */
int dloc; /* where we are within the byte */
int cycle;
printf("Inserting byte %d", value);
/* insert bits in little-endian order */
data = (value<<2) + 2; /* add "10" to the number */
/* round cycle low/high times to 11 samples low, 11 samples high */
/* use BIT_LEN to put it in the proper place */
for( i=0; i<10; i++ )
if ( data & (1<<i) )
{
printf(" %d: ", i );
/* have a 1-bit to insert, where should it be? */
/* insert 4 cycles */
for( cycle=0; cycle<4; cycle++ )
{
/* note: this may leave 0 samples between cycles or bits */
dloc = (int)(location + i*BIT_LEN + cycle*(BIT_LEN/4) + 0.5);
printf(" %d", dloc );
/* insert 11 low samples */
for( j=0; j<11; j++ )
{
NewChirp[ dloc+j ] = -30000;
}
/* insert 11 high samples */
for( j=11; j<22; j++ )
{
NewChirp[ dloc+j ] = 30000;
}
}
}
printf("\n");
} /* insert_byte */
/* insert a chirp with the given value */
void insert_chirp( FILE *f, float speed, float incline )
{
int i, i_speed, i_incline, i_chksum;
/* convert values to integers */
i_speed = (int)( speed*10 + 0.5 );
i_incline = (int)( incline*10 + 0.5 );
i_chksum = (i_speed + i_incline) & 0x0ff;
/* set everything to 0 in advance */
memset( NewChirp, 0, SAMP_RATE );
/* start inserting chirp parts */
insert_byte( 0, i_speed );
insert_byte( (int)(10*BIT_LEN+0.5), i_incline );
insert_byte( (int)(20*BIT_LEN+0.5), i_chksum );
insert_byte( (int)(40*BIT_LEN+0.5), i_speed );
insert_byte( (int)(50*BIT_LEN+0.5), i_incline );
insert_byte( (int)(60*BIT_LEN+0.5), i_chksum );
/* write the chirp */
fwrite( NewChirp, 1, SAMP_RATE, f );
} /* insert_chirp */
/* maximum # of chirps per file we will permit */
#define CHIRP_MAX 50
int main(int argc, char *argv[])
{
FILE *f; /* workout information file */
FILE *audio; /* output WAV file */
/* hold information about the chirps */
struct {
int second_count; /* where does this occur in seconds */
float speed; /* speed to use */
float incline; /* treadmill incline */
} chirp_list[CHIRP_MAX+1];
int chirp_count; /* # of chirps in the current workout */
int cur_chirp; /* which chirp are we working on? */
int cur_time; /* current time we are writing */
char wname[100]; /* workout name from the file */
int len;
char dbuf[100]; /* one chirp line from the file */
/* for reading one chirp */
int sec;
int seqno;
float speed, incline;
int sec_length; /* length of workout in seconds */
printf("initializaing\n");
/* build our 1 second of silence */
memset( Silence, 0, SAMP_RATE );
printf("opening input file %s\n", argv[1] );
/* open the description file */
fopen_s( &f, argv[1], "r" );
if ( f == NULL )
{
printf("Unable to open file %s\n", argv[1] );
return 1;
}
/* read one workout from the file */
while( 1==1 )
{
printf("reading one line from the workout\n");
/* at end of file? */
if ( feof(f) )
{
return 0;
}
/* get the header line */
fgets( wname, 90, f );
/* did we now detect end of file? */
if ( feof(f) )
{
return 0;
}
/* read the workout stages */
chirp_count = 0;
sec_length = 45; /* account for lead and trail time */
while( 1==1) {
fgets( dbuf, 90, f );
printf("read line %s", dbuf );
/* did we now detect end of file? - Shouldn't happen, but check */
if ( feof(f) || strlen(dbuf)<3 )
{
break;
}
if ( ! isdigit( dbuf[0] ) )
{
/* should have only numbers here */
return 3;
}
/* extract the information */
sscanf_s( dbuf, "%d %f %f %d", &seqno, &speed, &incline, &sec );
printf("speed=%3.1f, incline=%3.1f, time=%d\n", speed, incline, sec );
/* save it for later */
chirp_list[chirp_count].second_count = sec;
sec_length += sec;
chirp_list[chirp_count].speed = speed;
chirp_list[chirp_count].incline = incline;
/* move to next chirp */
chirp_count++;
}
/* replace ".txt" with ".wav" for the output filename */
if ( strlen( argv[1] ) > 90 )
{
printf("Error: path/filename too long\n");
exit(1);
}
strcpy( wname, argv[1] );
strcpy( wname+strlen(wname)-3, "wav" );
/* start creating our WAV file */
printf("Creating output file %s\n", wname );
fopen_s( &audio, wname, "wb" );
printf("Overall length: %d seconds\n", sec_length );
/* create header */
FileHeader.ID = ID_RIFF;
FileHeader.DataType = ID_WAVE;
/* create our format header */
FormatHeader.ID = ID_FMT;
FormatHeader.Len = 16; /* chunk length */
FormatHeader.FormatTag = 1; /* format (1 if uncompressed) */
FormatHeader.Channels = 1; /* # of channels */
FormatHeader.SampPerSec = SAMP_RATE; /* # of samples per second */
FormatHeader.AvgBytesPerSec = SAMP_RATE*2; /* # of bytes played per second */
FormatHeader.BlockAlign = 1; /* Size of a 'frame' in bytes */
FormatHeader.BitsPerSamp = 16; /* bits per sample */
/* create our data header */
DataHeader.ID = ID_DATA;
DataHeader.Len = (sec_length+LEAD_SILENCE) * SAMP_RATE;
/* update overall length, account for various headers except main RIFF part */
/* -- include 'WAVE' part of main header */
FileHeader.Len = DataHeader.Len + 4 +
+ sizeof(FormatHeader) + sizeof( DataHeader );
/* write it to the file */
fwrite( &FileHeader, sizeof(FileHeader), 1, audio );
/* write it to the file */
fwrite( &FormatHeader, sizeof(FormatHeader), 1, audio );
/* write it to the file */
fwrite( &DataHeader, sizeof(DataHeader), 1, audio );
/* start with silence */
printf("Writing lead-in\n" );
for( cur_time = 0; cur_time < LEAD_SILENCE; cur_time++ )
{
fwrite( &Silence, 1, SAMP_RATE, audio );
}
/* now do the chirps */
for( cur_chirp=0; cur_chirp < chirp_count; cur_chirp++ )
{
printf("Chirp #%d, time=%d, speed=%4.1f, incline=%4.1f\n",
cur_chirp+1, chirp_list[cur_chirp].second_count,
chirp_list[cur_chirp].speed, chirp_list[cur_chirp].incline );
/* add the chirp */
insert_chirp( audio, chirp_list[cur_chirp].speed,
chirp_list[cur_chirp].incline );
/* add enough silence between this chirp and the next */
for( cur_time = 1;
cur_time < chirp_list[cur_chirp].second_count; cur_time++ )
{
/* add another second of silence */
fwrite( &Silence, 1, SAMP_RATE, audio );
}
}
/* add the stop chirp */
printf("Stop chirp\n");
insert_chirp( audio, 25.2, 25.2 );
/* end with 30 seconds of silence */
printf("Writing 30 seconds lead-out\n" );
for( cur_time = 1; cur_time < 30; cur_time++ )
{
fwrite( &Silence, 1, SAMP_RATE, audio );
}
/* done with this workout */
fclose( audio );
} /* while reading workouts */
}