Re: [Csnd] fout

Hi Joachim!

What a pleasure to hear from you…!

Your patch works, thanks!

Now I wonder if it was also possible to make sure that, after generating some audio files with fout, after closing the frontend (in my case CsoundQt), in a subsequent work session the audio files - always generated with fout - can be saved starting from the numbering immediately following the last file previously generated.
This will avoid overwriting the new files generated in new work sessions, on the previous audio files created.
Enrico

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

There are a number of ways of achieving this depending on how automated you desire it to be.

If you know when your program has finished you can write a file defining a csound macro which records the number and you can define with either a #include or a CLI csound call defining a macro.

More automate methods include writing a C program (or whatever you refer) to scan the directory and extract the highest version number.

Apologies if this has already been suggested, but you can also use the date opcode to generate a unique file name each time.

And - as you mentioned CsoundQt - if you use the Record button in CsoundQt, an unique index will be added to the file name automatically.
Tarmo

P, 10. oktoober 2021 22:05 Rory Walsh <rorywalsh@ear.ie> kirjutas:

Apologies if this has already been suggested, but you can also use the date opcode to generate a unique file name each time.

There are a number of ways of achieving this depending on how automated
you desire it to be.

If you know when your program has finished you can write a file defining
a csound macro which records the number and you can define with either a
#include or a CLI csound call defining a macro.

More automate methods include writing a C program (or whatever you refer)
to scan the directory and extract the highest version number.

Hi Joachim!

What a pleasure to hear from you…!

Your patch works, thanks!

Now I wonder if it was also possible to make sure that, after generating some audio files with fout, after closing the frontend (in my case CsoundQt), in a subsequent work session the audio files - always generated with fout - can be saved starting from the numbering immediately following the last file previously generated.
This will avoid overwriting the new files generated in new work sessions, on the previous audio files created.
Enrico

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

In deed. The example for dates opcode in the manual does just that and ows how using substr one can get usr-non-hostile names.

excellent. i know the manual. and I know the opcodes, but not all of them by a mile.

  • cool

hi enrico -

i think this is one of the possible solutions which john mentioned. it looks for a file "state.txt" in the working directory. if not found, it will be created. if found, it will continue where it was left.

instr Init
  if filevalid("state.txt") == 1 then
   State, iline readfi "state.txt"
   giFilNum = strtod(State)
   prints "File 'state.txt' read. Continuing with file counter %d.\n", giFilNum
  else
   puts "File 'state.txt' created, starting with 1.", 1
   fprints("state.txt","1")
   giFilNum = 1
  endif
endin

instr Record
  S_filename sprintf "Out_REC_%02d.wav", giFilNum
  giFilNum += 1
  Scmd sprintf "printf %d > state.txt", giFilNum
  ires system_i 1, Scmd
  ;fprints("state.txt","%d",giFilNum)
  fout S_filename, 18, poscil:a(.2,random:i(400,800))
endin

and score e.g.
i "Init" 0 0
i "Record" 0 1
i . 1 1

rather than using the system_i opcode i would prefer to use fprints. but this always appends the string to the file content, and i could not figure out how just to replace the content. i don't know whether there is a hidden option in fprints, or any other opcode to do it.

  joachim

Hmm, fprints overwrites the existing file for me and a quick look at the source suggests the file is always opened in "w" so I think the intended behaviour is to overwrite.

I see many avenues open to accomplishing the same thing: very well!
Each solution adapts to specific needs.
I had neglected the Tarmo solution; on the other hand, the other proposals are valid reasons for in-depth analysis and study.

Thanks as usual for the great help.

thanks for looking in the code. for me, the result of this is definitely "1234", not "4":

indx = 1
istart = 0
while indx < 5 do
  schedule(1,istart,0,indx)
  istart += 1
  indx += 1
od
instr 1
  fprints "test.txt", "%d", p4
endin

  j

Actually, I was wrong, and I can't recreate what I originally said now...
I had a further look and it seems that fprints and fprintks keep the file open, so a subsequent call will use the existing open file and hence append. It seems that writing strings will always append to the file, but fiopen/ficlose could be used to overwrite a state file with a number and then retrieve it with fini. I had a play around with your example to try it:

gStateFile = "state.txt"
giFilNum init 0

opcode WriteState, 0, 0
  ihandle fiopen gStateFile, 0
  fouti ihandle, 0, 0, giFilNum
  ficlose ihandle
endop

instr Init
  if filevalid(gStateFile) == 1 then
   fini gStateFile, 0, 0, giFilNum
   prints "File 'state.txt' read. Continuing with file counter %d.\n", giFilNum
  else
   puts "File 'state.txt' created, starting with 1.", 1
  giFilNum = 1
  WriteState

  endif
endin

instr Record
  S_filename sprintf "Out_REC_%02d.wav", giFilNum
  giFilNum += 1
  WriteState
  fout S_filename, 18, poscil:a(.2,random:i(400,800))
endin