voz veces varias una trae tiene sonido reproducir notas nota mensaje las grabar grabadora escuchan como iphone ios avfoundation core-audio audiotoolbox

iphone - veces - ¿Cómo generar audio en forma de ondas programáticamente mientras se graba la voz en iOS?



iphone 8 tiene grabadora de voz (3)

¿Cómo generar audio en forma de ondas programáticamente mientras se graba la voz en iOS?

m Estoy trabajando con modulación de voz en la frecuencia de audio en iOS ... todo funciona bien ... solo necesito una forma sencilla de generar ondas de audio en el ruido de detección ...

Por favor, no me refiera a los tutoriales de código de ... speakhere y auriotouch ... Necesito algunas de las mejores sugerencias de los desarrolladores de aplicaciones nativas.

He grabado el audio y lo hice reproducir después de la grabación. He creado la forma de onda y adjunto captura de pantalla. Pero tiene que dibujarse en la vista como grabación de audio en curso.

-(UIImage *) audioImageGraph:(SInt16 *) samples normalizeMax:(SInt16) normalizeMax sampleCount:(NSInteger) sampleCount channelCount:(NSInteger) channelCount imageHeight:(float) imageHeight { CGSize imageSize = CGSizeMake(sampleCount, imageHeight); UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); CGContextSetAlpha(context,1.0); CGRect rect; rect.size = imageSize; rect.origin.x = 0; rect.origin.y = 0; CGColorRef leftcolor = [[UIColor whiteColor] CGColor]; CGColorRef rightcolor = [[UIColor redColor] CGColor]; CGContextFillRect(context, rect); CGContextSetLineWidth(context, 1.0); float halfGraphHeight = (imageHeight / 2) / (float) channelCount ; float centerLeft = halfGraphHeight; float centerRight = (halfGraphHeight*3) ; float sampleAdjustmentFactor = (imageHeight/ (float) channelCount) / (float) normalizeMax; for (NSInteger intSample = 0 ; intSample < sampleCount ; intSample ++ ) { SInt16 left = *samples++; float pixels = (float) left; pixels *= sampleAdjustmentFactor; CGContextMoveToPoint(context, intSample, centerLeft-pixels); CGContextAddLineToPoint(context, intSample, centerLeft+pixels); CGContextSetStrokeColorWithColor(context, leftcolor); CGContextStrokePath(context); if (channelCount==2) { SInt16 right = *samples++; float pixels = (float) right; pixels *= sampleAdjustmentFactor; CGContextMoveToPoint(context, intSample, centerRight - pixels); CGContextAddLineToPoint(context, intSample, centerRight + pixels); CGContextSetStrokeColorWithColor(context, rightcolor); CGContextStrokePath(context); } } // Create new image UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // Tidy up UIGraphicsEndImageContext(); return newImage; }

A continuación, un método que toma un AVURLAsset y devuelve datos PNG.

- (NSData *) renderPNGAudioPictogramForAssett:(AVURLAsset *)songAsset { NSError * error = nil; AVAssetReader * reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error]; AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0]; NSDictionary* outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey, // [NSNumber numberWithInt:44100.0],AVSampleRateKey, /*Not Supported*/ // [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, /*Not Supported*/ [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, [NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved, nil]; AVAssetReaderTrackOutput* output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:outputSettingsDict]; [reader addOutput:output]; [output release]; UInt32 sampleRate,channelCount; NSArray* formatDesc = songTrack.formatDescriptions; for(unsigned int i = 0; i < [formatDesc count]; ++i) { CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i]; const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item); if(fmtDesc ) { sampleRate = fmtDesc->mSampleRate; channelCount = fmtDesc->mChannelsPerFrame; // NSLog(@"channels:%u, bytes/packet: %u, sampleRate %f",fmtDesc->mChannelsPerFrame, fmtDesc->mBytesPerPacket,fmtDesc->mSampleRate); } } UInt32 bytesPerSample = 2 * channelCount; SInt16 normalizeMax = 0; NSMutableData * fullSongData = [[NSMutableData alloc] init]; [reader startReading]; UInt64 totalBytes = 0; SInt64 totalLeft = 0; SInt64 totalRight = 0; NSInteger sampleTally = 0; NSInteger samplesPerPixel = sampleRate / 50; while (reader.status == AVAssetReaderStatusReading){ AVAssetReaderTrackOutput * trackOutput = (AVAssetReaderTrackOutput *)[reader.outputs objectAtIndex:0]; CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer]; if (sampleBufferRef){ CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef); size_t length = CMBlockBufferGetDataLength(blockBufferRef); totalBytes += length; NSAutoreleasePool *wader = [[NSAutoreleasePool alloc] init]; NSMutableData * data = [NSMutableData dataWithLength:length]; CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, data.mutableBytes); SInt16 * samples = (SInt16 *) data.mutableBytes; int sampleCount = length / bytesPerSample; for (int i = 0; i < sampleCount ; i ++) { SInt16 left = *samples++; totalLeft += left; SInt16 right; if (channelCount==2) { right = *samples++; totalRight += right; } sampleTally++; if (sampleTally > samplesPerPixel) { left = totalLeft / sampleTally; SInt16 fix = abs(left); if (fix > normalizeMax) { normalizeMax = fix; } [fullSongData appendBytes:&left length:sizeof(left)]; if (channelCount==2) { right = totalRight / sampleTally; SInt16 fix = abs(right); if (fix > normalizeMax) { normalizeMax = fix; } [fullSongData appendBytes:&right length:sizeof(right)]; } totalLeft = 0; totalRight = 0; sampleTally = 0; } } [wader drain]; CMSampleBufferInvalidate(sampleBufferRef); CFRelease(sampleBufferRef); } } NSData * finalData = nil; if (reader.status == AVAssetReaderStatusFailed || reader.status == AVAssetReaderStatusUnknown){ // Something went wrong. return nil return nil; } if (reader.status == AVAssetReaderStatusCompleted){ NSLog(@"rendering output graphics using normalizeMax %d",normalizeMax); UIImage *test = [self audioImageGraph:(SInt16 *) fullSongData.bytes normalizeMax:normalizeMax sampleCount:fullSongData.length / 4 channelCount:2 imageHeight:100]; finalData = imageToData(test); } [fullSongData release]; [reader release]; return finalData; }

yo tengo


Debería consultar EZAudio ( https://github.com/syedhali/EZAudio ), específicamente el EZRecorder y el EZAudioPlot (o EZAudioPlotGL acelerado por GPU).

También hay un proyecto de ejemplo que hace exactamente lo que quiere, https://github.com/syedhali/EZAudio/tree/master/EZAudioExamples/iOS/EZAudioRecordExample

EDITAR: Aquí está el código en línea

/// In your interface /** Use a OpenGL based plot to visualize the data coming in */ @property (nonatomic,weak) IBOutlet EZAudioPlotGL *audioPlot; /** The microphone component */ @property (nonatomic,strong) EZMicrophone *microphone; /** The recorder component */ @property (nonatomic,strong) EZRecorder *recorder; ... /// In your implementation // Create an instance of the microphone and tell it to use this view controller instance as the delegate -(void)viewDidLoad { self.microphone = [EZMicrophone microphoneWithDelegate:self startsImmediately:YES]; } // EZMicrophoneDelegate will provide these callbacks -(void)microphone:(EZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels { dispatch_async(dispatch_get_main_queue(),^{ // Updates the audio plot with the waveform data [self.audioPlot updateBuffer:buffer[0] withBufferSize:bufferSize]; }); } -(void)microphone:(EZMicrophone *)microphone hasAudioStreamBasicDescription:(AudioStreamBasicDescription)audioStreamBasicDescription { // The AudioStreamBasicDescription of the microphone stream. This is useful when configuring the EZRecorder or telling another component what audio format type to expect. // We can initialize the recorder with this ASBD self.recorder = [EZRecorder recorderWithDestinationURL:[self testFilePathURL] andSourceFormat:audioStreamBasicDescription]; } -(void)microphone:(EZMicrophone *)microphone hasBufferList:(AudioBufferList *)bufferList withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels { // Getting audio data as a buffer list that can be directly fed into the EZRecorder. This is happening on the audio thread - any UI updating needs a GCD main queue block. This will keep appending data to the tail of the audio file. if( self.isRecording ){ [self.recorder appendDataFromBufferList:bufferList withBufferSize:bufferSize]; } }


Estaba buscando lo mismo. (Haciendo wave a partir de los datos de la grabadora de audio). Encontré una biblioteca que podría ser útil y vale la pena revisar el código para entender la lógica detrás de esto.

El cálculo está todo basado en el pecado y la fórmula matemática. ¡Esto es mucho más simple si le echas un vistazo al código!

https://github.com/stefanceriu/SCSiriWaveformView

o

https://github.com/raffael/SISinusWaveView

Estos son solo algunos ejemplos que puedes encontrar en la web.


Si desea gráficos en tiempo real derivados de la entrada de micrófono, use la unidad de audio RemoteIO, que es la que la mayoría de los desarrolladores de aplicaciones iOS nativas utilizan para audio de baja latencia, y Metal o Open GL para dibujar formas de onda, lo que le dará las tasas de cuadros más altas. . Necesitará un código completamente diferente del proporcionado en su pregunta para hacerlo, ya que AVAssetRecording, el dibujo de líneas del Core Graphic y el renderizado de png son demasiado lentos para usar.

Actualización: con iOS 8 y más reciente, la API de Metal puede representar visualizaciones gráficas con un rendimiento aún mayor que OpenGL.

Uodate 2: Aquí hay algunos fragmentos de código para grabar audio en vivo usando Audio Units y dibujar mapas de bits usando Metal en Swift 3: https://gist.github.com/hotpaw2/f108a3c785c7287293d7e1e81390c20b