|
| 1 | +package tracksources |
| 2 | + |
| 3 | +//import "testing" |
| 4 | +// |
| 5 | +//import "os" |
| 6 | +// |
| 7 | +//var opusfilepath string = "/home/max/Downloads/music.opus" |
| 8 | +// |
| 9 | +//var oggFile OggOpusFile |
| 10 | +//func openfile(t *testing.T) { |
| 11 | +// loggFile, err := Gop_open_file(opusfilepath) |
| 12 | +// if err != nil { |
| 13 | +// t.Errorf("Error: %s\n", err) |
| 14 | +// } |
| 15 | +// oggFile = loggFile |
| 16 | +//} |
| 17 | +// |
| 18 | +//var totalSamplesPerChannel int64 |
| 19 | +//func getPcmTotal(t *testing.T) { |
| 20 | +// t.Run("open file", openfile) |
| 21 | +// |
| 22 | +// ltotalSamplesPerChannel, err := Gop_pcm_total(oggFile, -1) |
| 23 | +// if err != nil { |
| 24 | +// t.Errorf("Error: %s\n", err.Error()) |
| 25 | +// } |
| 26 | +// totalSamplesPerChannel = ltotalSamplesPerChannel |
| 27 | +//} |
| 28 | +// |
| 29 | +//var pcmBuffer []int16 |
| 30 | +//func readStereo(t *testing.T) { |
| 31 | +// t.Run("get total samples per channel", getPcmTotal) |
| 32 | +// //frame_size := 960 |
| 33 | +// //sample_rate := 48000 |
| 34 | +// //channels := 2 |
| 35 | +// //bitrate := 64000 |
| 36 | +// //max_frame_size := 9*960 |
| 37 | +// //max_packet_size := 3*1276 |
| 38 | +// |
| 39 | +// var pcmOffset int = 0 |
| 40 | +// |
| 41 | +// var pcm [120*48*2]int16 // 120ms of data at 48kHz per channel (11520 values total) |
| 42 | +// pcmBuffer = make([]int16, ((960*2) - totalSamplesPerChannel*2 % (960*2)) + totalSamplesPerChannel*2) |
| 43 | +// for { |
| 44 | +// readedSamples, err := Gop_read_stereo(oggFile, pcm[:], uint32(len(pcm))) |
| 45 | +// if err != nil { |
| 46 | +// t.Errorf("Error: %s\n", err.Error()) |
| 47 | +// } |
| 48 | +// copy(pcmBuffer[pcmOffset:], pcm[:readedSamples*2]) |
| 49 | +// pcmOffset += (readedSamples*2) |
| 50 | +// if pcmOffset == int(totalSamplesPerChannel*2) { |
| 51 | +// // readedSamples == 0 would work too, but this checks if our query before worked out too |
| 52 | +// t.Log("Success End-Of-File") |
| 53 | +// break; |
| 54 | +// } |
| 55 | +// if (pcmOffset > len(pcmBuffer)) { |
| 56 | +// t.Fatal("Buffer to small\n") |
| 57 | +// } |
| 58 | +// } |
| 59 | +// writeInt16InFile(t, "in.pcm", 0, pcmBuffer[:pcmOffset]) |
| 60 | +//} |
| 61 | +// |
| 62 | +// |
| 63 | +//func TestEncodeOpus(t *testing.T) { |
| 64 | +// t.Run("read Stereo", readStereo) |
| 65 | +// |
| 66 | +// outputFile, err := os.Create("testout.opus") // Removes the file if it already exists |
| 67 | +// outputFile.Close() |
| 68 | +// |
| 69 | +// if err != nil { |
| 70 | +// t.Fatal(err.Error()) |
| 71 | +// } |
| 72 | +// |
| 73 | +// opusenc, err := Gopus_encoder_create(48000, 2, -1) //TODO -1 is not right |
| 74 | +// if err != nil { |
| 75 | +// t.Fatalf("Gopus_encoder_create error: %s\n", err.Error()) |
| 76 | +// } |
| 77 | +// encOffset := 0 |
| 78 | +// for i := 0; i < len(pcmBuffer); i += 960*2 { |
| 79 | +// var opusPayload [4000]byte |
| 80 | +// //panic: runtime error: slice bounds out of range [:12414720] with capacity 12414336 |
| 81 | +// encoded, err := Gopus_encode(opusenc, pcmBuffer[i:i+960*2], 960, opusPayload[:], len(opusPayload)) |
| 82 | +// if err != nil { |
| 83 | +// t.Fatalf("Error while encoding opus: %s\n", err.Error()) |
| 84 | +// } |
| 85 | +// writeInFile(t, "testout.opus", int64(encOffset), opusPayload[:encoded]) |
| 86 | +// encOffset += encoded |
| 87 | +// } |
| 88 | +// t.Log("Now start testout.opus with media player to confirm success\n") |
| 89 | +//} |
| 90 | +// |
| 91 | +//// write buffer in file of path with offset |
| 92 | +//func writeInFile(t *testing.T, path string, offset int64, buffer []byte) { |
| 93 | +// file, err := os.OpenFile(path, os.O_RDWR, 0666) |
| 94 | +// if err != nil { |
| 95 | +// t.Fatal(err.Error()) |
| 96 | +// } |
| 97 | +// if _, err := file.WriteAt(buffer[:], offset); err != nil { |
| 98 | +// t.Fatal(err.Error()) |
| 99 | +// } |
| 100 | +// file.Close() |
| 101 | +//} |
| 102 | +// |
| 103 | +//// write int16 buffer in file of path with offset |
| 104 | +//func writeInt16InFile(t *testing.T, path string, offset int64, buffer []int16) { |
| 105 | +// file, err := os.Create(path) |
| 106 | +// if err != nil { |
| 107 | +// t.Fatal(err.Error()) |
| 108 | +// } |
| 109 | +// bufferByte := make([]byte, len(buffer)*2) |
| 110 | +// // Convert to little Endian ordering |
| 111 | +// for i := 0; i < len(buffer); i++ { |
| 112 | +// bufferByte[2*i] = byte(buffer[i] & 0xFF) |
| 113 | +// bufferByte[2*i+1] = byte((buffer[i] >> 8) & 0xFF) |
| 114 | +// } |
| 115 | +// |
| 116 | +// if _, err := file.WriteAt(bufferByte[:], offset); err != nil { |
| 117 | +// t.Fatal(err.Error()) |
| 118 | +// } |
| 119 | +//} |
0 commit comments