代码之家  ›  专栏  ›  技术社区  ›  vfn

Objective-c-如何将音频文件序列化为可以播放的小数据包?

  •  0
  • vfn  · 技术社区  · 16 年前

    所以,我想得到一个声音文件,并将其转换成数据包,然后发送到另一台计算机。我希望另一台计算机能够在数据包到达时播放它们。

    我正在使用AVAudioPlayer来尝试播放这些数据包,但是我找不到一个合适的方法来序列化peer1上的数据,peer2可以播放这些数据包。

    场景是,peer1有一个音频文件,将音频文件分成许多小包,将它们放在NSData上并发送给peer2。对等点2接收数据包并在它们到达时逐个播放。

    有人知道怎么做吗?或者即使有可能?

    编辑:

    
    // This code is part of the peer1, the one who sends the data
    - (void)sendData
    {
        int packetId = 0;
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"myAudioFile" ofType:@"wav"];
    
        NSData *soundData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
        NSMutableArray *arraySoundData = [[NSMutableArray alloc] init];
    
        // Spliting the audio in 2 pieces
        // This is only an illustration
        // The idea is to split the data into multiple pieces
        // dependin on the size of the file to be sent
        NSRange soundRange;
        soundRange.length = [soundData length]/2;
        soundRange.location = 0;
        [arraySoundData addObject:[soundData subdataWithRange:soundRange]];
        soundRange.length = [soundData length]/2;
        soundRange.location = [soundData length]/2;
        [arraySoundData addObject:[soundData subdataWithRange:soundRange]];
    
        for (int i=0; i<[arraySoundData count]; i++)
        {
            NSData *soundPacket = [arraySoundData objectAtIndex:i];
    
            if(soundPacket == nil)
            {
                NSLog(@"soundData is nil");
                return;
            }       
    
            NSMutableData* message = [[NSMutableData alloc] init];
            NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:message];
            [archiver encodeInt:packetId++ forKey:PACKET_ID];
            [archiver encodeObject:soundPacket forKey:PACKET_SOUND_DATA];
            [archiver finishEncoding];      
    
            NSError* error = nil;
            [connectionManager sendMessage:message error:&error];
            if (error) NSLog (@"send greeting failed: %@" , [error localizedDescription]);
    
            [message release];
            [archiver release];
        }
    
        [soundData release];
        [arraySoundData release];
    }
    
    
    // This is the code on peer2 that would receive and play the piece of audio on each packet
    
    - (void) receiveData:(NSData *)data
    {
    
        NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
        if ([unarchiver containsValueForKey:PACKET_ID])
            NSLog(@"DECODED PACKET_ID: %i", [unarchiver decodeIntForKey:PACKET_ID]);
    
        if ([unarchiver containsValueForKey:PACKET_SOUND_DATA])
        {
            NSLog(@"DECODED sound");
            NSData *sound = (NSData *)[unarchiver decodeObjectForKey:PACKET_SOUND_DATA];
    
            if (sound == nil)
            {
                NSLog(@"sound is nil!");
    
            }
            else
            {
                NSLog(@"sound is not nil!");
    
                AVAudioPlayer *audioPlayer = [AVAudioPlayer alloc];
    
                if ([audioPlayer initWithData:sound error:nil])
                {
                    [audioPlayer prepareToPlay];
                    [audioPlayer play];
                } else {
                    [audioPlayer release];
                    NSLog(@"Player couldn't load data");
                }   
            }
        }
    
        [unarchiver release];
    }
    

    这将是一种流媒体。是的,现在我不担心接收或播放数据包的顺序…我只需要将声音切片,他们就可以播放每一个片段,每一个片段,而不需要等待整个文件被peer2接收。

    谢谢!

    3 回复  |  直到 16 年前
        1
  •  5
  •   Vladimir    16 年前

    看来你解决了一个错误的任务,因为 只能播放整个音频文件。你应该使用 音频队列服务 从 音频工具箱 框架,而是在分组的基础上播放音频。实际上,您不需要将audiofile划分为真正的声音包,您可以使用任何数据块,如上面的示例所示,但是您应该使用 或 音频文件流服务 函数(从 音频工具箱 )并将它们送入音频队列缓冲区。

    使用 打开audiofile并获取其所有属性 AudioFileGetProperty属性 功能。基本上,您只需要kAudioFilePropertyDataFormat和kaudiofilePropertyAudioDataPackageCount属性:

    AudioFileID  fileID;    // the identifier for the audio file
    CFURLRef     fileURL = ...; // file URL
    AudioStreamBasicDescription format; // structure containing audio header info
        UInt64  packetsCount;
    
    AudioFileOpenURL(fileURL, 
        0x01, //fsRdPerm,                       // read only
        0, //no hint
        &fileID
    );
    
    UInt32 sizeOfPlaybackFormatASBDStruct = sizeof format;
    AudioFileGetProperty (
        fileID, 
        kAudioFilePropertyDataFormat,
        &sizeOfPlaybackFormatASBDStruct,
        &format
    );
    
    propertySize = sizeof(packetsCount);
    AudioFileGetProperty(fileID, kAudioFilePropertyAudioDataPacketCount, &propertySize, &packetsCount);
    

    然后,您可以获取任意范围的音频数据包:

       OSStatus AudioFileReadPackets (
           AudioFileID                  inAudioFile,
           Boolean                      inUseCache,
           UInt32                       *outNumBytes,
           AudioStreamPacketDescription *outPacketDescriptions,
           SInt64                       inStartingPacket,
           UInt32                       *ioNumPackets,
           void                         *outBuffer
        );
    
        2
  •  8
  •   Vladimir    14 年前

    请注意,您可以从任何点播放它(只需设置currentPacketNumber)

    #import <Foundation/Foundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    
    @interface AudioFile : NSObject {
        AudioFileID                     fileID;     // the identifier for the audio file to play
        AudioStreamBasicDescription     format;
        UInt64                          packetsCount;           
        UInt32                          maxPacketSize;  
    }
    
    @property (readwrite)           AudioFileID                 fileID;
    @property (readwrite)           UInt64                      packetsCount;
    @property (readwrite)           UInt32                      maxPacketSize;
    
    - (id) initWithURL: (CFURLRef) url;
    - (AudioStreamBasicDescription *)audioFormatRef;
    
    @end
    
    
    //  AudioFile.m
    
    #import "AudioFile.h"
    
    
    @implementation AudioFile
    
    @synthesize fileID;
    @synthesize format;
    @synthesize maxPacketSize;
    @synthesize packetsCount;
    
    - (id)initWithURL:(CFURLRef)url{
        if (self = [super init]){       
            AudioFileOpenURL(
                             url,
                             0x01, //fsRdPerm, read only
                             0, //no hint
                             &fileID
                             );
    
            UInt32 sizeOfPlaybackFormatASBDStruct = sizeof format;
            AudioFileGetProperty (
                                  fileID, 
                                  kAudioFilePropertyDataFormat,
                                  &sizeOfPlaybackFormatASBDStruct,
                                  &format
                                  );
    
            UInt32 propertySize = sizeof (maxPacketSize);
    
            AudioFileGetProperty (
                                  fileID, 
                                  kAudioFilePropertyMaximumPacketSize,
                                  &propertySize,
                                  &maxPacketSize
                                  );
    
            propertySize = sizeof(packetsCount);
            AudioFileGetProperty(fileID, kAudioFilePropertyAudioDataPacketCount, &propertySize, &packetsCount);
        }
        return self;
    } 
    
    -(AudioStreamBasicDescription *)audioFormatRef{
        return &format;
    }
    
    - (void) dealloc {
        AudioFileClose(fileID);
        [super dealloc];
    }
    
    
    
    //  AQPlayer.h
    
    #import <Foundation/Foundation.h>
    #import "AudioFile.h"
    
    #define AUDIOBUFFERS_NUMBER     3
    #define MAX_PACKET_COUNT    4096
    
    @interface AQPlayer : NSObject {
    @public
        AudioQueueRef                   queue;
        AudioQueueBufferRef             buffers[AUDIOBUFFERS_NUMBER];
        NSInteger                       bufferByteSize;
        AudioStreamPacketDescription    packetDescriptions[MAX_PACKET_COUNT];
    
        AudioFile * audioFile;
        SInt64  currentPacketNumber;
        UInt32  numPacketsToRead;
    }
    
    @property (nonatomic)               SInt64          currentPacketNumber;
    @property (nonatomic, retain)       AudioFile       * audioFile;
    
    -(id)initWithFile:(NSString *)file;
    -(NSInteger)fillBuffer:(AudioQueueBufferRef)buffer;
    -(void)play;
    
    @end 
    
    //  AQPlayer.m
    
    #import "AQPlayer.h"
    
    static void AQOutputCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
        AQPlayer * aqp = (AQPlayer *)inUserData;
        [aqp fillBuffer:(AudioQueueBufferRef)inBuffer];
    }
    
    @implementation AQPlayer
    
    @synthesize currentPacketNumber;
    @synthesize audioFile;
    
    -(id)initWithFile:(NSString *)file{
        if ([self init]){
            audioFile = [[AudioFile alloc] initWithURL:[NSURL fileURLWithPath:file]];
            currentPacketNumber = 0;
            AudioQueueNewOutput ([audioFile audioFormatRef], AQOutputCallback, self, CFRunLoopGetCurrent (), kCFRunLoopCommonModes, 0, &queue);
            bufferByteSize = 4096;
            if (bufferByteSize < audioFile.maxPacketSize) bufferByteSize = audioFile.maxPacketSize; 
            numPacketsToRead = bufferByteSize/audioFile.maxPacketSize;
            for(int i=0; i<AUDIOBUFFERS_NUMBER; i++){
                AudioQueueAllocateBuffer (queue, bufferByteSize, &buffers[i]);
            }
        }
        return self;
    }
    
    -(void) dealloc{
        [audioFile release];
        if (queue){
            AudioQueueDispose(queue, YES);
            queue = nil;
        }
        [super dealloc];
    }
    
    - (void)play{
        for (int bufferIndex = 0; bufferIndex < AUDIOBUFFERS_NUMBER; ++bufferIndex){
            [self fillBuffer:buffers[bufferIndex]];
        }
        AudioQueueStart (queue, NULL);
    
    }
    
    -(NSInteger)fillBuffer:(AudioQueueBufferRef)buffer{
        UInt32 numBytes;
        UInt32 numPackets = numPacketsToRead;
        BOOL isVBR = [audioFile audioFormatRef]->mBytesPerPacket == 0 ? YES : NO;
        AudioFileReadPackets(
                             audioFile.fileID,
                             NO,
                             &numBytes,
                             isVBR ? packetDescriptions : 0,
                             currentPacketNumber,
                             &numPackets, 
                             buffer->mAudioData
                             );
    
        if (numPackets > 0) {
            buffer->mAudioDataByteSize = numBytes;      
            AudioQueueEnqueueBuffer (
                                     queue,
                                     buffer,
                                     isVBR ? numPackets : 0,
                                     isVBR ? packetDescriptions : 0
                                     );
    
    
        } 
        else{
            // end of present data, check if all packets are played
            // if yes, stop play and dispose queue
            // if no, pause queue till new data arrive then start it again
        }
        return  numPackets;
    }
    
        3
  •  0
  •   sbooth    16 年前

    苹果已经写了一些可以做到这一点的东西:AUNetSend和AUNetReceive。AUNetSend是一种效果音频单元,它将音频发送到另一台计算机上的AUNetReceive音频单元。

    不幸的是,这些AU在iPhone上不可用。

    推荐文章