- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
tôi ở đây Nguồn
文件夹中有一个 Active wave 录音 wave-file.wav
。我需要使用新名称 wave-file-copy.wav
将此文件复制到 Destination
文件夹。
记录和复制应该并行进行。我已经实现了一个计划作业,它将每 10 分钟运行一次并将 nguồn
文件复制到 điểm đến
。
private static void CopyWaveFile(string destinationFile, string sourceFile){
using (var fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
using (var reader = new WaveFileReader(fs)){
using (var writer = new WaveFileWriter(destinationFile, reader.WaveFormat)){
reader.Position = 0;
var endPos = (int)reader.Length;
var buffer = new byte[1024];
while (reader.Position < endPos){
var bytesRequired = (int)(endPos - reader.Position);
if (bytesRequired <= 0) continue;
var bytesToRead = Math.Min(bytesRequired, buffer.Length);
var bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0){
writer.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
复制操作工作正常,即使源文件不断更新。
复制操作所花费的时间呈线性增加,因为我每次都复制整个文件。
我正在尝试实现一个新函数 ConcatenateWavFiles()
,它应该使用源记录的最新可用字节更新目标文件的内容。
我尝试了一些示例代码 - 我使用的方法是:
chiều dài
为源文件waveReader
củareader.Position
从位置开始读取源文件直到结束。
public static void ConcatenateWavFiles(string destinationFile, string sourceFile){
WaveFileWriter waveFileWriter = null;
var sourceReadOffset = GetWaveFileSize(destinationFile);
thử{
using (var fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new WaveFileReader(fs))
{
waveFileWriter = new WaveFileWriter(destinationFile, reader.WaveFormat);
if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat)){
throw new InvalidOperationException(
"Can't append WAV Files that don't share the same format");
}
var startPos = sourceReadOffset - sourceReadOffset % reader.WaveFormat.BlockAlign;
var endPos = (int) reader.Length;
reader.Position = startPos;
var bytesRequired = (int)(endPos - reader.Position);
var buffer = new byte[bytesRequired];
if (bytesRequired > 0)
{
var bytesToRead = Math.Min(bytesRequired, buffer.Length);
var bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0)
{
waveFileWriter.Write(buffer, startPos, bytesRead);
}
}
}
}
}
finally{
if (waveFileWriter != null){
waveFileWriter.Dispose();
}
}
}
我能够获得新内容。
是否可以将最新内容附加到现有目标文件?
如果可能的话,我在代码中做错了什么?
我的代码抛出以下异常 - 偏移量和长度超出数组范围或计数大于从索引到源集合末尾的元素数。
câu trả lời hay nhất
我找不到使用 NAudio 库复制 wave 音频文件的解决方案。
但我已经使用 C# MemoryStreams 和 FileStreams 实现了一个解决方案。
定期重复此追加操作。
public void ReplicateFile(string destinationFile, string sourceFile){
if (!Directory.Exists(GetRoutePathFromFile(sourceFile)))
trở lại;
if (!File.Exists(sourceFile))
trở lại;
if (Directory.Exists(GetRoutePathFromFile(destinationFile))){
if (File.Exists(destinationFile)){
UpdateLatestWaveFileContent(destinationFile, sourceFile);
}khác{
CopyWaveFile(destinationFile, sourceFile);
}
}khác{
Directory.CreateDirectory(GetRoutePathFromFile(destinationFile));
CopyWaveFile(destinationFile, sourceFile);
}
}
private static string GetRoutePathFromFile(string file){
var rootPath = Directory.GetParent(file);
return rootPath.FullName;
}
private static void CopyWaveFile(string destination, string source){
var sourceMemoryStream = new MemoryStream();
using (var fs = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
fs.CopyTo(sourceMemoryStream);
}
using (var fs = new FileStream(destination, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)){
sourceMemoryStream.WriteTo(fs);
}
}
private static void UpdateLatestWaveFileContent(string destinationFile, string sourceFile){
var sourceMemoryStream = new MemoryStream();
long offset = 0;
using (var fs = File.Open(destinationFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
offset = fs.Length;
}
using (var fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
fs.CopyTo(sourceMemoryStream);
}
var length = sourceMemoryStream.Length - offset;
var buffer = sourceMemoryStream.GetBuffer();
using (var fs = new FileStream(destinationFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)){
fs.Write(buffer, (int)offset, (int)length);
}
var bytes = new byte[45];
for (var i = 0; i < 45; i++){
bytes[i] = buffer[i];
}
ModifyHeaderDataLength(destinationFile, 0, bytes);
}
private static void ModifyHeaderDataLength(string filename, int position, byte[] data){
using (Stream stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
stream.Position = position;
stream.Write(data, 0, data.Length);
}
}
关于c# - 使用 Naudio 复制 Wave 文件 - 复制/附加最新的可用字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35335269/
Tôi đang viết một ứng dụng cho phép người dùng tạo "bảng câu hỏi" và sau đó thêm câu hỏi vào đó. Tôi đang sử dụng dữ liệu cốt lõi để lưu trữ thông tin. Tôi đã tạo thực thể Bảng câu hỏi và thiết lập mối quan hệ "một-nhiều" với thực thể Câu hỏi. Câu hỏi của tôi là liệu tôi có muốn cho phép người dùng sao chép (copy) toàn bộ giai điệu không
Có cách nào để sao chép hoặc sao chép SharedPreference không? Hay tôi cần lấy từng biến từ một biến và đặt chúng vào một biến khác? Câu trả lời hay nhất hãy thử đại loại như thế này: //sp1 là pref được chia sẻ
Có sự khác biệt nào giữa (A) và (B) dưới đây không? (Giả sử KHÔNG ARC, nếu quan trọng) // --- (A) --- @interface Zoo : NSObject{} @property (copy) Dog
Tôi đang cố lưu truy vấn CHỌN mysql vào một tệp như thế này: $result = mysqli_query($db,$sql); $out = fopen('tmp/csv.csv', 'w'
Tôi cần tạo một bản sao của CVPixelBufferRef để có thể sử dụng các giá trị trong bản sao để thao tác với bộ đệm pixel gốc theo chiều bit. Có vẻ như tôi không thể sử dụng CVPixelBufferCreate hoặc CVPixelBufferCr
Tôi có một bản ghi sóng hoạt động wave-file.wav trong thư mục Nguồn. Tôi cần sao chép tệp này vào thư mục Đích với tên mới wave-file-copy.wav.
Trong một dự án được xây dựng bằng GNU Autotools, tôi có một tập lệnh cần được sửa đổi thông qua make để bao gồm đường dẫn cài đặt. Đây là một ví dụ nhỏ: configure.ac: AC_INIT(foobar, 1.0) AC_PR
Tôi muốn sao chép các hàng SQL vào cùng một bảng. Nhưng trong bảng của tôi, tôi có cột "văn bản". Sử dụng SQL này: TẠO BẢNG TẠM THỜI produit2 ENGINE=MEMORY SELECT
Ai có thể giải thích cho tôi df2 = df1 df2 = df1.copy() df3 = df1.copy(deep=False) Tôi đã thử tất cả các tùy chọn và làm như sau: df1 = pd.DataFram
Hazelcast có bản sao tương tự như Ehcache không? http://www.ehcache.org/generated/2.9.0/pdf/Ehcache_Replication_Guide.
Tôi có cấu trúc liên kết sau đây. Ubuntu 16.04. Một phiên bản trên Amazon AWS chạy máy chủ MySQL toàn cầu của tôi. Tôi muốn sử dụng máy chủ này làm nô lệ cho nhiều máy chủ chính cục bộ (máy Windows, máy chủ MySQL)
Sử dụng SQLyog, tôi đang kiểm tra xem các giá trị chính xác có được đặt trong bảng hay không. Tôi đã thử SELECT type_service FROM service WHERE email='test@gmail.com' Vì vậy, chỉ có đầu ra
Ai đó có thể cung cấp một số hướng dẫn về cách định cấu hình ElasticSearch để sao chép. Tôi đang chạy ES trong Windows và tôi hiểu rằng nếu tôi chạy tệp bat nhiều lần trên cùng một máy chủ thì một phiên bản ES riêng biệt sẽ được khởi động và
Một lưu ý nhỏ về hai phương pháp sao chép luồng của ThreadGroup. public int enumerate(Thread list[]) // Sẽ sao chép tất cả các thread đang hoạt động trong ThreadGroup sang
Một điểm: ThreadGroup có hai phương pháp sao chép nhóm luồng. public int enumerate(ThreadGroup list[]) // liên quan đến enumerate(list,true) pu
Tài liệu hướng dẫn Cassandra: Cấu hình không gian khóa và tạo trung tâm dữ liệu mới: Sử dụng ALTER KEYSPAC
Câu hỏi này đã có câu trả lời tại đây: Làm thế nào để làm mịn trọng số theo hệ số tùy ý trong ggplot2? (2 bài viết)
Chúng tôi có một biểu mẫu để thể hiện sự quan tâm đến các câu lạc bộ khác nhau. Đầu ra ghi lại dữ liệu trong bảng tính Excel liệt kê tên, họ, email, đại từ ưa thích của họ và số "1" trong cột tương ứng cho câu lạc bộ mà họ quan tâm (mô hình bên dưới). Chúng tôi hy vọng rằng đối với câu lạc bộ
Câu hỏi này đã có câu trả lời ở đây: Đã đóng 8 năm trước. Có thể trùng lặp: Trong vim, làm thế nào để tôi có được
Làm cách nào để sao chép hình và ô chứa hình đó? Khi tôi sao chép thủ công, hình dạng sẽ theo ô, nhưng khi tôi sao chép bằng macro, tôi nhận được mọi thứ trừ hình dạng. Ô(sourceRow, sourceColumn).Sao chép C
Tôi là một lập trình viên xuất sắc, rất giỏi!