2016-12-07 04:39:12 +01:00
|
|
|
// This file is part of the NRGrip project.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2016 Matteo Cypriani <mcy@lm7.fr>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
2016-12-07 03:10:38 +01:00
|
|
|
//! Module to extract the raw audio data from an NRG image file.
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Seek, SeekFrom, Read, Write};
|
2016-12-07 22:06:10 +01:00
|
|
|
use std::path::PathBuf;
|
2016-12-07 03:10:38 +01:00
|
|
|
|
|
|
|
use ::error::NrgError;
|
|
|
|
use ::metadata::metadata::NrgMetadata;
|
|
|
|
|
2016-12-08 20:34:45 +01:00
|
|
|
const RAW_SEC_SIZE: u16 = 2352;
|
|
|
|
const RAW96_SEC_SIZE: u16 = 2448;
|
|
|
|
|
2016-12-07 03:10:38 +01:00
|
|
|
|
|
|
|
/// Extracts the raw audio data from an NRG image.
|
|
|
|
///
|
|
|
|
/// - `in_fd` is the handler to the NRG image file.
|
2016-12-07 22:06:10 +01:00
|
|
|
/// - `img_path` is the name of the input NRG file.
|
|
|
|
/// - `metadata` is the metadata extracted from `img_path` by nrgrip::metadata.
|
2016-12-07 03:10:38 +01:00
|
|
|
///
|
2016-12-07 22:06:10 +01:00
|
|
|
/// The output file's name is derived from `img_path`.
|
2016-12-07 03:10:38 +01:00
|
|
|
pub fn extract_nrg_raw_audio(in_fd: &mut File,
|
2016-12-08 23:10:20 +01:00
|
|
|
img_path: &str,
|
2016-12-07 03:10:38 +01:00
|
|
|
metadata: &NrgMetadata)
|
|
|
|
-> Result<(), NrgError> {
|
2016-12-07 22:06:10 +01:00
|
|
|
// Seek to the first audio byte
|
2016-12-08 01:34:20 +01:00
|
|
|
let first_audio_byte = metadata.first_audio_byte();
|
2016-12-08 01:12:29 +01:00
|
|
|
try!(in_fd.seek(SeekFrom::Start(first_audio_byte)));
|
|
|
|
|
2016-12-07 22:06:10 +01:00
|
|
|
// Open output file
|
|
|
|
let audio_name = try!(make_output_file_name(img_path));
|
|
|
|
let mut out_fd = try!(File::create(audio_name));
|
2016-12-07 03:10:38 +01:00
|
|
|
|
2016-12-08 19:37:26 +01:00
|
|
|
// Copy the audio data
|
2016-12-08 20:34:45 +01:00
|
|
|
let count = metadata.last_audio_byte() - first_audio_byte;
|
|
|
|
let bytes_read = match metadata.sector_size() {
|
|
|
|
RAW96_SEC_SIZE => try!(copy_raw96_audio(in_fd, &mut out_fd, count)),
|
|
|
|
0 => return Err(NrgError::AudioReadError),
|
|
|
|
_ => try!(copy_raw_audio(in_fd, &mut out_fd, count)),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(count, bytes_read);
|
2016-12-08 19:37:26 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Reads `count` bytes from `in_fd` and write them to `out_fd`.
|
|
|
|
///
|
|
|
|
/// The offsets of `in_fd` and `out_fd` are not reset prior to reading and
|
|
|
|
/// writing.
|
|
|
|
///
|
2016-12-08 20:34:45 +01:00
|
|
|
/// Returns the number of bytes read/written.
|
2016-12-08 19:37:26 +01:00
|
|
|
fn copy_raw_audio(in_fd: &mut File, out_fd: &mut File, count: u64)
|
|
|
|
-> Result<u64, NrgError> {
|
|
|
|
// The buffer size (~4,6 MiB) is a multiple of the standard audio CD sector
|
|
|
|
// size, i.e. 2352 bytes (it doesn't have to be, though).
|
2016-12-08 20:34:45 +01:00
|
|
|
const BUF_SIZE: usize = RAW_SEC_SIZE as usize * 1024 * 2;
|
2016-12-08 19:37:26 +01:00
|
|
|
|
2016-12-07 22:06:10 +01:00
|
|
|
// Read/write audio data
|
2016-12-08 19:37:26 +01:00
|
|
|
let mut bytes_read = 0;
|
|
|
|
while bytes_read + BUF_SIZE as u64 <= count {
|
2016-12-08 00:34:31 +01:00
|
|
|
let mut audio_buf = [0u8; BUF_SIZE];
|
2016-12-07 03:10:38 +01:00
|
|
|
|
|
|
|
let mut nbytes = try!(in_fd.read(&mut audio_buf));
|
2016-12-08 00:34:31 +01:00
|
|
|
if nbytes != BUF_SIZE {
|
2016-12-07 03:10:38 +01:00
|
|
|
return Err(NrgError::AudioReadError);
|
|
|
|
}
|
2016-12-08 19:37:26 +01:00
|
|
|
bytes_read += nbytes as u64;
|
2016-12-07 03:10:38 +01:00
|
|
|
|
|
|
|
nbytes = try!(out_fd.write(&audio_buf));
|
2016-12-08 00:34:31 +01:00
|
|
|
if nbytes != BUF_SIZE {
|
2016-12-07 03:10:38 +01:00
|
|
|
return Err(NrgError::AudioWriteError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 00:34:31 +01:00
|
|
|
// Read/write the last bytes
|
2016-12-08 19:37:26 +01:00
|
|
|
let remaining: usize = (count - bytes_read) as usize;
|
2016-12-08 00:34:31 +01:00
|
|
|
let mut audio_buf = vec![0u8; remaining];
|
|
|
|
let mut nbytes = try!(in_fd.read(&mut audio_buf));
|
|
|
|
if nbytes != remaining {
|
|
|
|
return Err(NrgError::AudioReadError);
|
|
|
|
}
|
2016-12-08 19:37:26 +01:00
|
|
|
bytes_read += nbytes as u64;
|
2016-12-08 00:34:31 +01:00
|
|
|
nbytes = try!(out_fd.write(&audio_buf));
|
|
|
|
if nbytes != remaining {
|
|
|
|
return Err(NrgError::AudioWriteError);
|
|
|
|
}
|
|
|
|
|
2016-12-08 19:37:26 +01:00
|
|
|
Ok(bytes_read)
|
2016-12-07 03:10:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-08 20:34:45 +01:00
|
|
|
/// Reads `count` bytes from `in_fd` and write them to `out_fd` after stripping
|
|
|
|
/// the sub-channel bytes.
|
|
|
|
///
|
|
|
|
/// `in_fd` is read by chunks of 2448 bytes, then the first 2352 bytes are
|
|
|
|
/// written to `out_fd`, leaving out the 96 sub-channel bytes.
|
|
|
|
///
|
|
|
|
/// The offsets of `in_fd` and `out_fd` are not reset prior to reading and
|
|
|
|
/// writing.
|
|
|
|
///
|
|
|
|
/// Returns the number of bytes read (not written).
|
|
|
|
fn copy_raw96_audio(in_fd: &mut File, out_fd: &mut File, count: u64)
|
|
|
|
-> Result<u64, NrgError> {
|
|
|
|
const IN_BUF_SIZE: usize = RAW96_SEC_SIZE as usize;
|
|
|
|
const OUT_BUF_SIZE: usize = RAW_SEC_SIZE as usize;
|
|
|
|
|
|
|
|
// Read/write audio data
|
|
|
|
let mut bytes_read = 0;
|
|
|
|
while bytes_read < count {
|
|
|
|
let mut audio_buf = vec![0u8; IN_BUF_SIZE];
|
|
|
|
|
|
|
|
let mut nbytes = try!(in_fd.read(&mut audio_buf));
|
|
|
|
if nbytes != IN_BUF_SIZE {
|
|
|
|
return Err(NrgError::AudioReadError);
|
|
|
|
}
|
|
|
|
bytes_read += nbytes as u64;
|
|
|
|
|
|
|
|
audio_buf.truncate(OUT_BUF_SIZE);
|
|
|
|
nbytes = try!(out_fd.write(&audio_buf));
|
|
|
|
if nbytes != OUT_BUF_SIZE {
|
|
|
|
return Err(NrgError::AudioWriteError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(bytes_read)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-07 03:10:38 +01:00
|
|
|
/// Generates the output file's name from the NRG image's name.
|
|
|
|
///
|
2016-12-07 22:06:10 +01:00
|
|
|
/// The output file's name will be `img_path`'s base name stripped for its
|
|
|
|
/// extension (if any), with a ".raw" extension.
|
2016-12-08 23:10:20 +01:00
|
|
|
fn make_output_file_name(img_path: &str) -> Result<String, NrgError> {
|
2016-12-07 22:06:10 +01:00
|
|
|
let mut name = PathBuf::from(img_path);
|
|
|
|
name.set_extension("raw");
|
2016-12-08 05:20:45 +01:00
|
|
|
let name = try!(name.file_name().ok_or(
|
|
|
|
NrgError::FileName(name.to_string_lossy().into_owned())));
|
2016-12-07 22:06:10 +01:00
|
|
|
|
|
|
|
// Make sure the new name and the original name are different
|
2016-12-08 23:10:20 +01:00
|
|
|
if name == img_path {
|
2016-12-08 05:20:45 +01:00
|
|
|
return Err(NrgError::FileName("Input and output file are identical"
|
|
|
|
.to_string()));
|
2016-12-07 03:10:38 +01:00
|
|
|
}
|
2016-12-07 22:06:10 +01:00
|
|
|
|
|
|
|
Ok(name.to_string_lossy().into_owned())
|
2016-12-07 03:10:38 +01:00
|
|
|
}
|