【问题标题】:How to create single sound file from multiple chunks of sounds in Android如何从 Android 中的多个声音块创建单个声音文件
【发布时间】:2015-08-10 13:29:40
【问题描述】:

我正在创建一个鼓应用程序,我必须在其中做两件事

  1. 播放各种声音

  2. 如果用户选择录制声音,我必须制作用户播放过的任何音乐的音频文件

该应用程序的工作原理是这样的 - 当用户点击按钮 A 时,我播放 SoundA。如果用户点击 B,我会播放 SoundB,同样...

所以要播放而不是录制声音,我在想的是,当用户单击“开始录制”按钮时 - 如果我保留用户单击按钮时的时间戳,我可以通过混合我已经拥有的声音来重现质量更好的最终输出。

例如用户在 00:01 秒点击按钮 A,在 00:05 秒点击按钮 B,以此类推

我不确定的是 - 如何创建音频文件并在特定时间段插入这些声音,即声音 A 在 00:01 秒,声音 B 在 00:05 秒......依此类推。

对此的任何帮助都会有很大帮助。

提前谢谢你。

【问题讨论】:

    标签: android audio audio-recording


    【解决方案1】:

    两部分答案:

    解决方案 1:想到的一个想法是,与其尝试录制生成的音频,不如录制他们按下的按钮,并在他们播放文件时重新创建序列。

    所以你的“录音”文件应该是这样的:

    • A 0001(1 秒播放 A)
    • B 0005
    • A 0006(添加更多我的 拥有)
    • C 0008
    • ...
    • B 0194(使用纯秒而不是分钟/秒)

    因此,当他们想要播放“录音”时,您可以设置一个计时器,等待适当的时间,然后播放适当的声音。因此,对于我上面的示例,您将:

    • 等待一秒钟,然后播放声音 A
    • 再等四秒钟,然后播放声音 B
    • 再等一秒钟,再次播放声音 A
    • 等待两秒钟,然后播放声音 C

    解决方案 2:如果您希望能够将录音导出到您的应用之外,因此需要一个真实的音频文件,这会更合适。

    这假设您拥有原始格式的声音(此处可以使用 WAV,但必须先解码 MP3 或其他格式)。这还假设所有声音在样本/秒、立体声/单声道、8 位/16 位样本等方面都是一致的。然后当它们完成录制时,分配一个适当的数组(或两个,如果你做立体声)尺寸。因此,如果录制时间为 11 秒,并且您的声音是 44100 样本/秒的 16 位单声道,则您分配一个由 485100 个短片组成的数组(485100=11*44100)。如果它太大,那么您可能需要在较短的片段中执行此操作(例如一次一秒),并准备好让您的声音在片段之间交叉。 16 位值是有符号的短裤,因此您应该能够将数组初始化为 0,并在适当的位置添加每个声音。请注意,8 位值是无符号的,带有偏移量,因此您必须在添加样本时调整偏移量。然后以一种格式写入文件(可能是 WAV,有关 WAV 文件格式的说明,请参阅http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html)。如果您希望文件为 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 */
    }
    

    【讨论】:

    • 嗨,尤金,感谢您的回答。我或多或少清楚我关于如何播放声音的逻辑 - 但不清楚如何在 Android 技术上实现它 - 所以任何有关这方面的参考都会非常有帮助。谢谢。
    • 我认为播放声音最好的办法是使用 SoundPool 类 - 最适合同时播放多个声音,特别是如果它们是短声音。这似乎最适合您的应用。
    • 感谢 Eugene 回复评论。我录制的声音现在播放良好,但我在本地播放,即我没有创建文件并播放声音。如果您查看我的问题,我更热衷于学习如何制作单个声音文件(mp3、wav)或类似的任何文件?
    • 添加了一个创建 .wav 文件的程序示例(在 C for Windows 中)。希望它不会太难适应 Android/Java。我没有任何直接创建 .mp3 文件的示例。
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多