Changeset 1009

Show
Ignore:
Timestamp:
10/17/07 09:10:58 (15 months ago)
Author:
stephen_booth
Message:

Use FILE* for io

Location:
trunk/Audio/Decoders
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/Audio/Decoders/MPEGDecoder.h

    r1008 r1009  
    2626@interface MPEGDecoder : AudioDecoder 
    2727{ 
    28         int                                     _fd; 
     28        FILE                            *_file; 
    2929        unsigned char           *_inputBuffer; 
    3030         
  • trunk/Audio/Decoders/MPEGDecoder.m

    r1008 r1009  
    9494                NSAssert(NULL != _inputBuffer, @"Unable to allocate memory"); 
    9595                 
    96                 _fd = open([[[self URL] path] fileSystemRepresentation], O_RDONLY); 
    97                 if(-1 == _fd) { 
     96                _file = fopen([[[self URL] path] fileSystemRepresentation], "r"); 
     97                if(NULL == _file) { 
    9898                        if(nil != error) { 
    99                                  
     99                                NSMutableDictionary *errorDictionary = [NSMutableDictionary dictionary]; 
     100                                 
     101//                              [errorDictionary setObject:[NSString stringWithFormat:NSLocalizedStringFromTable(@"The format of the file \"%@\" was not recognized.", @"Errors", @""), [[NSFileManager defaultManager] displayNameAtPath:path]] forKey:NSLocalizedDescriptionKey]; 
     102//                              [errorDictionary setObject:NSLocalizedStringFromTable(@"File Format Not Recognized", @"Errors", @"") forKey:NSLocalizedFailureReasonErrorKey]; 
     103//                              [errorDictionary setObject:NSLocalizedStringFromTable(@"The file's extension may not match the file's type.", @"Errors", @"") forKey:NSLocalizedRecoverySuggestionErrorKey]; 
     104                                 
     105                                *error = [NSError errorWithDomain:NSPOSIXErrorDomain  
     106                                                                                         code:errno  
     107                                                                                 userInfo:errorDictionary]; 
    100108                        } 
    101109                         
     
    150158         
    151159        free(_inputBuffer), _inputBuffer = NULL; 
    152         close(_fd), _fd = -1; 
     160        fclose(_file), _file = NULL; 
    153161         
    154162        if(_bufferList) { 
     
    171179{ 
    172180        double  fraction        = (double)frame / [self totalFrames]; 
    173         off_t   seekPoint       = 0; 
     181        long    seekPoint       = 0; 
    174182         
    175183        // If a Xing header was found, interpolate in TOC 
     
    188196                         
    189197                        double x = firstOffset + (secondOffset - firstOffset) * (percent - firstIndex); 
    190                         seekPoint = (off_t)((1.0 / 256.0) * x * _fileBytes);  
     198                        seekPoint = (long)((1.0 / 256.0) * x * _fileBytes);  
    191199        } 
    192200        else 
    193                 seekPoint = (off_t)_fileBytes * fraction; 
    194          
    195         off_t result = lseek(_fd, seekPoint, SEEK_SET); 
    196         if(-1 != result) { 
     201                seekPoint = (long)_fileBytes * fraction; 
     202         
     203        int result = fseek(_file, seekPoint, SEEK_SET); 
     204        if(0 == result) { 
    197205                mad_stream_buffer(&_mad_stream, NULL, 0); 
    198206                 
     
    270278                if(NULL == _mad_stream.buffer || MAD_ERROR_BUFLEN == _mad_stream.error) { 
    271279                        if(NULL != _mad_stream.next_frame) { 
    272                                 bytesRemaining          = _mad_stream.bufend - _mad_stream.next_frame; 
    273                                  
     280                                bytesRemaining = _mad_stream.bufend - _mad_stream.next_frame; 
    274281                                memmove(_inputBuffer, _mad_stream.next_frame, bytesRemaining); 
    275282                                 
     
    284291                         
    285292                        // Read raw bytes from the MP3 file 
    286                         ssize_t bytesRead = read(_fd, readStartPointer, bytesToRead); 
    287                          
    288                         if(-1 == bytesRead) { 
     293                        size_t bytesRead = fread(readStartPointer, 1, bytesToRead, _file); 
     294                        if(ferror(_file)) { 
    289295#if DEBUG 
    290296                                NSLog(@"Read error: %s.", strerror(errno)); 
     
    294300                         
    295301                        // MAD_BUFFER_GUARD zeroes are required to decode the last frame of the file 
    296                         if(0 == bytesRead) { 
     302                        if(0 == bytesRead && feof(_file)) { 
    297303                                memset(readStartPointer + bytesRead, 0, MAD_BUFFER_GUARD); 
    298304                                bytesRead       += MAD_BUFFER_GUARD; 
     
    307313                int result = mad_frame_decode(&_mad_frame, &_mad_stream); 
    308314                if(-1 == result) { 
    309                          
    310315                        if(MAD_RECOVERABLE(_mad_stream.error)) { 
    311                                  
    312316                                // Prevent ID3 tags from reporting recoverable frame errors 
    313317                                const uint8_t   *buffer                 = _mad_stream.this_frame; 
     
    324328                                        mad_stream_skip(&_mad_stream, id3_length); 
    325329                                } 
    326                                 else { 
    327330#if DEBUG 
     331                                else 
    328332                                        NSLog(@"Recoverable frame level error (%s)", mad_stream_errorstr(&_mad_stream)); 
    329333#endif 
    330                                 } 
    331334                                 
    332335                                continue; 
     
    352355                 
    353356                // Skip any samples that remain from last frame 
    354                 // This can happen if the LAME encoder delay is greater than the number of samples in a frame 
    355                 // This normally won't happen, but is possible for MP3s that have been gaplessly cut 
     357                // This can happen if the encoder delay is greater than the number of samples in a frame 
     358                // This normally won't happen, but is possible 
    356359                unsigned startingSample = _samplesToSkipInNextFrame; 
    357360                 
     
    401404         
    402405        _currentFrame += framesRead; 
     406         
    403407        return framesRead; 
    404408} 
     
    412416        uint32_t                        framesDecoded = 0; 
    413417        UInt32                          bytesToRead, bytesRemaining; 
    414         ssize_t                         bytesRead; 
     418        size_t                          bytesRead; 
    415419        unsigned char           *readStartPointer; 
    416420        BOOL                            readEOF; 
     
    429433        readEOF = NO; 
    430434         
    431         result = fstat(_fd, &stat); 
     435        result = fstat(fileno(_file), &stat); 
    432436        if(-1 == result) 
    433437                return NO; 
     
    437441        for(;;) { 
    438442                if(NULL == stream.buffer || MAD_ERROR_BUFLEN == stream.error) { 
    439                          
    440443                        if(stream.next_frame) { 
    441                                 bytesRemaining          = stream.bufend - stream.next_frame; 
    442                                  
     444                                bytesRemaining = stream.bufend - stream.next_frame; 
    443445                                memmove(_inputBuffer, stream.next_frame, bytesRemaining); 
    444446                                 
     
    453455                         
    454456                        // Read raw bytes from the MP3 file 
    455                         bytesRead = read(_fd, readStartPointer, bytesToRead); 
    456                          
    457                         if(-1 == bytesRead) { 
     457                        bytesRead = fread(readStartPointer, 1, bytesToRead, _file); 
     458                        if(ferror(_file)) { 
    458459#if DEBUG 
    459460                                NSLog(@"Read error: %s.", strerror(errno)); 
     
    463464                         
    464465                        // MAD_BUFFER_GUARD zeroes are required to decode the last frame of the file 
    465                         if(0 == bytesRead) { 
     466                        if(0 == bytesRead && feof(_file)) { 
    466467                                memset(readStartPointer + bytesRead, 0, MAD_BUFFER_GUARD); 
    467468                                bytesRead       += MAD_BUFFER_GUARD; 
     
    474475                 
    475476                result = mad_frame_decode(&frame, &stream); 
    476                  
    477477                if(-1 == result) { 
    478                          
    479478                        if(MAD_RECOVERABLE(stream.error)) { 
    480                                  
    481479                                // Prevent ID3 tags from reporting recoverable frame errors 
    482480                                const uint8_t   *buffer                 = stream.this_frame; 
     
    492490                                        mad_stream_skip(&stream, id3_length); 
    493491                                } 
    494                                 else { 
    495492#if DEBUG 
     493                                else 
    496494                                        NSLog(@"Recoverable frame level error (%s)", mad_stream_errorstr(&stream)); 
    497495#endif 
    498                                 } 
    499496                                 
    500497                                continue; 
     
    518515                // Reference http://www.codeproject.com/audio/MPEGAudioInfo.asp 
    519516                if(1 == framesDecoded) { 
    520                          
    521517                        _format.mSampleRate                     = frame.header.samplerate; 
    522518                        _format.mChannelsPerFrame       = MAD_NCHANNELS(&frame.header); 
     
    549545                         
    550546                        if('Xing' == magic || 'Info' == magic) { 
    551                                 //                      if((('X' << 24) | ('i' << 16) | ('n' << 8) | ('g')) == magic) { 
    552                                  
    553547                                unsigned        i; 
    554548                                uint32_t        flags = 0, frames = 0, bytes = 0, vbrScale = 0; 
     
    670664                                         
    671665                                } 
    672                                 } 
    673                         } 
     666                        } 
     667                } 
    674668                else { 
    675669                        // Just estimate the number of frames based on the file's size 
     
    679673                        break; 
    680674                }                
    681                 } 
     675        } 
    682676         
    683677        // Clean up 
     
    686680         
    687681        // Rewind to the beginning of file 
    688         if(-1 == lseek(_fd, 0, SEEK_SET)) 
     682        if(-1 == fseek(_file, 0, SEEK_SET)) 
    689683                return NO; 
    690684         
    691685        return YES; 
    692         } 
     686} 
    693687 
    694688@end